【程序1】
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
1.程序分析:兔子的规律为数列1,1,2,3,5,8,13,21....
public static void main(String[]args)
{
Scanner sc=new Scanner(System.in);
System.out.println("请输入月数:");
int month=sc.nextInt();
int a=0,b=1,i,t;
for(i=2;i<=month;i++)
{
t=b;
b=a+b;
a=t;
}
System.out.println("第"+month+"个月有"+b+"对兔子!");
}
【程序2】
题目:判断101-200之间有多少个素数,并输出所有素数。
1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,
则表明此数不是素数,反之是素数。
public static void main(String args[])
{
int i,n,flag;
for(n=101;n<200;n++)
{
flag=0;
for(i=2;i { if(n%i==0) { flag=1; break; } } if(flag==0) { System.out.println(i); } } } public static void main(String[]args)throws Exception { int i,j,count=0; for(i=101;i<200;i++) { for(j=2;j<(int)(Math.sqrt(i)+1);j++) { if(i%j==0) break; } if(j>(int)Math.sqrt(i)) { System.out.println(i); count++; } } System.out.println("201到200之间共有"+count+"个素数!"); } 【程序3】 题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。 1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。public static void main(String args[]) { int a,b,c; int n; for(n=100;n<1000;n++) { a=n/100; b=(n-a*100)/10; c=n%10; if(n==a*a*a+b*b*b+c*c*c) { System.out.println(n); } } } 【程序4】 题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。 程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成: (1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。 (2)如果n<>k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,重复执行第一步。 (3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。 public static void main(String[]args) { Scanner sc=new Scanner(System.in); System.out.println("请输入一个整数n:"); int n=sc.nextInt(); System.out.print(n+"="); for(int k=2;k { if(n%k==0) { System.out.print(k+"*"); n=n/k; } else k++; } System.out.println(n); } 【程序5】 题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。 1.程序分析:(a>b)?a:b这是条件运算符的基本例子。 public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("请输入学生成绩score:"); int score=sc.nextInt(); char c=(score>=90)?'A':((score<60)?'C':'B'); System.out.println("该学生分数是:"+score+"等级是:"+c); } 【程序6】 题目:输入两个正整数m和n,求其最大公约数和最小公倍数。 1.程序分析:利用辗除法。 public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("输入两个整数:a和b"); int a=sc.nextInt(); int b=sc.nextInt(); int na=a,nb=b;//令na和nb分别保存a和b的初始值 int t;//定义中间变量t if(a { t=a; a=b; b=t; } t=a%b; while(t!=0) { a=b; b=t; t=a%b; } System.out.println("最大公约数是:"+b); System.out.println("最小公倍数是:"+na*nb/b); } 【程序7】 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 1.程序分析:利用while语句,条件为输入的字符不为'\n'. public static void main(String[]args) { Scanner sc=new Scanner(System.in); String str=sc.nextLine(); int letterCount=0,numCount=0,spaceCount=0,otherCount=0; for(int i=0;i { char c=str.charAt(i); if((c>='a'&&c<='z')||(c>='A'&&c<='Z')) letterCount++; else if(c>='0'&&c<='9') numCount++; else if(c=='') spaceCount++; else otherCount++; } System.out.println("字母有"+letterCount+"个"); System.out.println("数字有"+numCount+"个"); System.out.println("空格有"+spaceCount+"个"); System.out.println("其它有"+otherCount+"个"); } 【程序8】 题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。 1.程序分析:关键是计算出每一项的值。 public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("输入一个数字a和项数n:"); int a=sc.nextInt(); int n=sc.nextInt(); String str=""; int s=0,sum=0; int i; for(i=0;i { s=s+(int)Math.pow(10,i)*a; sum=sum+s; str+=s+"+"; } System.out.println("则有:"+str.substring(0, str.length()-1)+"="+sum); } 【程序9】 题目:一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3.编程找出1000以内的所有完数。 public static void main(String args[]) { int i,n,sum=0; System.out.println("输出1~1000的所有完数:"); for(n=1;n<=1000;n++) { for(i=1;i<=n/2;i++) { if(n%i==0) { sum=sum+i; } } if(sum==n) { System.out.println(n); } sum=0; } } 【程序10】 题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高? public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("请输入初始高度height和反弹起的次数n:"); int height=sc.nextInt(); int n=sc.nextInt(); double fheight=height/2; double distance=height; for(int i=2;i<=n;i++) { fheight=(double)height/(double)Math.pow(2,i); distance=distance+(double)height/(double)Math.pow(2, i-1)*2; } System.out.println("第"+n+"次落地时共经过"+distance+"米"); System.out.println("第"+n+"次弹起的高度是:"+fheight); } 【程序11】 题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去掉 不满足条件的排列。 public static void main(String args[]) { int[]a={1,2,3,4}; int count=0; for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { for(int k=0;k<4;k++) { if(a[i]!=a[j]&&a[j]!=a[k]&&a[k]!=a[i]) { System.out.println(a[i]+""+a[j]+""+a[k]); count++; } } } } System.out.println("共有"+count+"个数"); } 【程序12】 题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万 元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;20万到40万之间时,高于20万元的部 分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可 提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数? 1.程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。 public class A2 { public static void main(String[]args) { Scanner sc=new Scanner(System.in); System.out.println("请输入利润profit:"); long profit=sc.nextLong();//输入利润数 double prize=0;//实发奖金数 if(profit<=10&&profit>=0)//利润<=10W,提10% prize=profit*0.1; else if(profit>10&&profit<=20)//利润<=20W,低于10W的部分按10%提,高于10W的部分按7.5%提 prize=10*0.1+(profit-10)*0.075; else if(profit>20&&profit<=40)//利润<=40W,低于10W提10%,10W—20W 之间提7.5%,高于20W提5% prize=10*0.1+10*0.075+(profit-20)*0.05; else if(profit>40&&profit<=60)//利润<=60W,高于40W提3% prize=10*0.1+10*0.075+20*0.05+(profit-40)*0.03; else if(profit>60&&profit<=100)//利润<100W,高于60W提1.5% prize=10*0.1+10*0.075+20*0.05+20*0.03+(profit-60)*0.015; else if(profit>100)//利润>100W,高于100W提1% prize=10*0.1+10*0.075+20*0.05+20*0.03+40*0.015+(profit-100)*0.01; System.out.println("应发奖金为:"+prize+"万元!"); } } 【程序13】 题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问 该数是多少? 1.程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后的结果满足如下条件,即是结果。请看具体分析: public static void main(String args[]) { int a=1; while(a<100000) { int i=1; while(i { int j=1;