Java程序设计编程题40题(二)_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Java程序设计编程题40题(二)

Java程序设计编程题40题(二)

 2013/9/25 1:04:25  天上掉下个喜欢编程的榕哥哥  程序员俱乐部  我要评论(0)
  • 摘要:6、题目:一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3.编程找出1000以内的所有完数。思路:在1000内通过循环判断每个数是否完数,这里又可以利用一个数最大被它的1/2整除,只需判断每个数的前一半数。publicclassWanShu{publicstaticvoidmain(String[]args){for(intx=1;x<1000;x++){if(isWanShu(x)==true){System.out.println(x+"");}}
  • 标签:程序 Java 编程 设计

6、题目:一个数如果恰好等于它的因子之和,这个数就称为 "完数 "。例如6=123.编程?? 找出1000以内的所有完数。

?

? ? 思路:在1000内通过循环判断每个数是否完数,这里又可以利用一个数最大被它的1/2整除,只需判断每

? ? ? ? ? ? ? 个数的前一半数。

?

class="java" name="code">public class WanShu {
	public static void main(String[] args) {
		for(int x=1;x<1000;x++){
			if(isWanShu(x)==true){
				System.out.println(x+" ");
			}
		}
	}

	public static boolean isWanShu(int x) {
		int sumYinZi=0;//所有因子之和
		boolean flag = false;
		for (int i = 1; i < x/2; i++) {
			if(x%i==0){
				sumYinZi+=i;
			}
		}
		if(sumYinZi==x){
			flag=true;
		}
		return flag;
	}
}

?

7、题目:一球从100高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高??

? ?

? ?思路:这道题可以抓住某个时刻来解题,比如我们以球每次落地那一时刻为参照;除了第一次落地时经过

? ? ? ? ? ? ? ?的距离就是100米,其他落地时刻经过的距离都是前面已经计算的总距离加上2倍前一次反弹的高度

? ? ? ? ? ? ?(可以自己画图就清楚了)

? ?

public class Pro7{
 
   public static void main(String[] args){
   
      double sum = 0;
	  float height =100;
	  for(int i = 0;i<10;i++){
	     if(i==0){
	           sum +=height;
	      }else{
		   sum +=2*height;
	     }
	     height/=2;
	  }
      System.out.println("滴10次落地时,共经过:"+sum+" 第10次反弹高是:"+height);
     
   }
   
}

?

8、题目:有1234个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?? ??

? ?

? ?思路:构造三位数用最直接的方法,用嵌套循环利用4个数字构造出三位数,并把每次构造的三位

? ? ? ? ? ? ? ?数进行判断是否有数字重复,没有重复就放进一个list集合里

? ? ? ? ? ? ? ?判断是否重复:将构造的整数转换成字符串然后通过字符串的取字串方法分别取出三位数并

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 做判断。 ??

import java.util.*;
public class Pro8{
 
   public static void main(String[] args){
   
         int[] ary={1,2,3,4};
	 List<Integer> numlist = new ArrayList<Integer>();
	 for(int i = 1;i<=4;i++){
	    for(int j = 1;j<=4;j++){
		    for(int k=1;k<=4;k++){
			     int x = i*100+j*10+k;
			     if(!isChongfu(x)){
			     numlist.add(x);
			    }
		   }
	   }
	 }
	 
	 for(int a:numlist){
	    System.out.println(a);
	 }
   }
   
   public static boolean isChongfu(int x){
          boolean rtn = false;
          String str = x+"";
	  String bai =str.substring(0,1);
	  String shi =str.substring(1,2);
	  String ge =str.substring(2);
	  if(bai.equals(shi) || bai.equals(ge) || shi.equals(ge)){
	        rtn = true;
	  }
	  return rtn;
   }
   
}

?

?

9、题目:输出9*9口诀

?

public class Pro9{
 
   public static void main(String[] args){
        for(int i = 1;i<=9;i++){
	     for(int j = 1; j<=i;j++){
		    System.out.print(j+"×"+i+"="+j*i+"\t");
	     }
		    System.out.println();
       }
   }
   
}

?

10、题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个,

? ? ? ? ? 二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一

? ? ? ? ? 半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。? ?

?

? ? ? ?思路:采取逆向思维的方法,从后往前推断。?

?

? ? ? ?

import java.util.*;
public class Pro10{
 
   public static void main(String[] args){
     
	  System.out.print("输入天数:");
	  int day = new Scanner(System.in).nextInt();
	  System.out.print("这天有桃子的个数为:"+total(day));
      
   }
   
   public static int total(int day){
       if(day == 10){
	      return 1;
       }else{
	      return (total(day+1)+1)*2;
       }
   }
   
}

?

发表评论
用户名: 匿名