zoukankan      html  css  js  c++  java
  • 这么简单错两次 菜的出奇

    编写Java程序,求13-23+33-43+…+973-983+993-1003的值。

    第一次:

    public class jiafa{
    	public static void main(String[] args){
    		int i,sum=0;
    		int flag=1;
    		for(i=13;i<=1003;i+=10){
    			sum+=(flag*i);
    			flag*=-1;
    			
    		}
    		System.out.println(sum);
    	}
    }
    

    虽然编译器没有报错,不过我是真的菜的真实,事实证明平时代码不能写的太嗨,会飘!!!

    PS:
    1.虽然每次flag自乘一个-1改变符号,可是这个-23没有在for循环的范围内。。。
    不对,我好像对了哈哈

    第二次 打印输出所有的“水仙花数”,所谓“水仙花数”是指一个3位数,其中各位数字立方和等于该数本身。例如,153是一个“水仙花数”。

    public class shuixianhua {
    
    	public static void main(String args[]) {
    		// TODO Auto-generated method stub
    		int a,b,c,i,n;
    		a=n%10;
    		b=n/10%10;
    		c=n/100%10;
    		if(n==Math.pow(a,3)+Math.pow(b,3)+Math.pow(c,3)) {
    			System.out.println(n);
    		}
    		}
    }
    }
    

    蜜汁操作

    错误:1.没有把三位数这个得分点表示出来
    2.n来的莫名其妙,没有得到具体n的初始化

    代码改正:

    public class shuixianhua {
    
    	public static void main(String args[]) {
    		// TODO Auto-generated method stub
    		int a,b,c,i;
    		for(int n=100;n<=999;n++){
    		a=n%10;
    		b=n/10%10;
    		c=n/100%10;
    		if(n==Math.pow(a,3)+Math.pow(b,3)+Math.pow(c,3)) {
    			System.out.println(n);
    		}
    		}
    }
    }
    

    收获:

    Java的立方格式为Math.pow(底数,次方数); PS:MathM大写

  • 相关阅读:
    bzoj3884: 上帝与集合的正确用法(数论)
    洛谷10月月赛R2·浴谷八连测R3题解
    bzoj5055: 膜法师(BIT)
    bzoj2213: [Poi2011]Difference(思维题)
    bzoj1016: [JSOI2008]最小生成树计数(kruskal+dfs)
    一模 (2) day2
    一模 (2) day1
    Prime Palindromes
    常州培训 day5 解题报告
    一模 (1) day2
  • 原文地址:https://www.cnblogs.com/husiyu/p/12452076.html
Copyright © 2011-2022 走看看