总题链接:http://blog.sina.com.cn/s/blog_60fafdda0100wb21.html
有空的话可以做下,不建议全部做完,这些只能熟悉下java的基础语法。
【程序1】 TestRabbit.java
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....
public class practice01 {
<span style="white-space:pre"> </span>/**
<span style="white-space:pre"> </span> * @param args
<span style="white-space:pre"> </span> */
<span style="white-space:pre"> </span>public static void main(String[] args) {
<span style="white-space:pre"> </span>int f1 = 1, f2 = 1, f, m = 24;
<span style="white-space:pre"> </span>System.out.println("第" + 1 + "个月的兔子总数为:" + f1);
<span style="white-space:pre"> </span>for(int i = 2; i < m; i++) {
<span style="white-space:pre"> </span>f = f2;
<span style="white-space:pre"> </span>f1 = f;
<span style="white-space:pre"> </span>f2 = f1 + f2;
<span style="white-space:pre"> </span>System.out.println("第" + i + "个月的兔子总数为:" + f);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
}
【程序2】 FindPrimeNumber.java
题目:判断101-200之间有多少个素数,并输出所有素数。
1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,
则表明此数不是素数,反之是素数。
public class practice2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int ans = 0;
for(int i=101; i<=200; i+=2) {
boolean flag = true;
for(int j=2; j<=Math.sqrt(i); j++) {
if(i % j == 0) {
flag = false;
break;
}
}
if(flag == true) {
System.out.println(i);
ans++;
}
}
System.out.println(ans);
}
}
【程序3】FindDaffodilNumber.java
题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:
153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。
public class practice3 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
boolean b = false;
for(int i = 100; i <= 999; i++) {
int j = i, cmp=0;
while (j != 0) {
cmp += ((j%10)*(j%10)*(j%10));
j /= 10;
}
if(cmp == i) {
System.out.println(i);
}
}
}
}
【程序4】FenJie.java
题目:将一个正整数分解质因数。例如:输入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的值,重复执行第一步。
import java.util.Scanner;
public class practice04 {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Please input a positive integer:");
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.close();
int i = 2;
while(i <= n) {
if(i == n) {
System.out.print(i);
break;
}
else if(n % i == 0) {
System.out.print(i + "*");
n /= i;
}
else {
i++;
}
}
}
}
【程序5】 ConditionOperator.java
题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
1.程序分析:(a>b)?a:b这是条件运算符的基本例子。
import java.util.Scanner;
public class practice05 {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Please input score:");
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
scanner.close();
System.out.println((a>=90)?"A":((a<60)?"C":"B"));
}
}
【程序6】Test1.java GcdTest.java后者是辗转相除法
题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
1.程序分析:利用辗除法。
import java.util.Scanner;
public class practice06 {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Please input m and n:");
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
int n = scanner.nextInt();
scanner.close();
Help help = new Help();
int gcd = help.Gcd(m, n);
int lcm = (m*n)/gcd;
System.out.println("The GCD is "+gcd+",the lcm is "+lcm);
}
}
class Help{
public int Gcd(int m, int n) {
return n==0?m:Gcd(n, m%n);
}
}
【程序7】 StChar.java
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
1.程序分析:利用while语句,条件为输入的字符不为'\n'.
import java.util.Scanner;
public class practice07 {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Please input line:");
int numberCnt = 0, charCnt = 0, blankCnt = 0, otherCnt = 0;
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
scanner.close();
char ch[] = s.toCharArray();
for (int i = 0; i < ch.length; i++) {
if((ch[i] <= '9')&&(ch[i] >= '0'))
numberCnt++;
else if(((ch[i]<='z')&&(ch[i]>='a'))||((ch[i]<='Z')&&(ch[i]>='A')))
charCnt++;
else if(ch[i] == ' ')
blankCnt++;
else
otherCnt++;
}
System.out.println("numberCnt:" + numberCnt + ",charCnt+blankCnt:" + charCnt + ",blankCnt:" + blankCnt+", OtherCnt:"+otherCnt);
}
}
【程序8】 TestAdd.java
题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
1.程序分析:关键是计算出每一项的值。
import java.util.Scanner;
public class practice08 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Please input a and time:");
int a, flagA, time, flagT, ans = 0;
Scanner scanner = new Scanner(System.in);
a = scanner.nextInt();
flagA = a;
time = scanner.nextInt();
flagT = time;
scanner.close();
while(time > 0) { //2+22+222+...+222222=2*6+20*5+...+200000*1
ans += a*time;
time--;
a *= 10;
}
System.out.println("a is:" + flagA + ",time is:" + flagT + ",answer is " + ans);
}
}
【程序9】 WanShu.java
题目:一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3.编程 找出1000以内的所有完数。
public class practice09 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(1);
for(int i = 1; i < 1000; i++) {
int facSum = 0;
for(int j = 1; j <= i/2; j++) {
if(i % j == 0)
facSum += j;
}
if(facSum == i)
System.out.println(i);
}
}
}
【程序10】TestBall.java
题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?
public class practice10 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int time = 10;
double height = 100, ans = 0;
ans += height;
time --;
while(time > 0) {
height /= 2;
ans += 2 * height;
time --;
}
height /= 2;
System.out.println("The distance is " + ans +",The height is " + height);
}
}
【程序11】 TestTN.java
题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。
public class practice11 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[] = {1,2,3,4}, cnt = 0;
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
if(a[j] == a[i])
continue;
for(int k = 0; k < 4; k++) {
if( (a[k] == a[j]) || (a[k] == a[i]) )
continue;
cnt ++;
System.out.println(a[i]*100 + a[j]*10 + a[k]);
}
}
}
System.out.println("The total number is " + cnt);
}
}
【程序12】 MoneyAward.java
题目:企业发放的奖金根据利润提成。利润(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.程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。
import java.util.Scanner;
public class practice12 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
double Profit, ans;
System.out.println("Please input the profit:");
Scanner scanner = new Scanner(System.in);
Profit = scanner.nextDouble();
scanner.close();
if( Profit <= 10 ) {
ans = Profit * 0.1;
} else if (Profit > 10 && Profit < 20) {
ans = (Profit - 10) * 0.075 + 10 * 0.1;
} else if (Profit >= 20 && Profit <= 40) {
ans = (Profit - 20) * 0.05 + 10 * 0.1 + 10 * 0.75;
} else {
ans = -1;
}
System.out.println("Bonus is " + ans);
}
}
【程序13】FindNumber.java
题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
1.程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后的结果满足如下条件,即是结果。请看具体分析:
public class practice13 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i = 0; i < 1000000; i++) {
if( Math.sqrt(i + 100) % 1 == 0 && Math.sqrt(i + 268) % 1 == 0 )
System.out.println(i);
}
}
}