文档库 最新最全的文档下载
当前位置:文档库 › java经典程序题1

java经典程序题1

有的程序不是按分析做的
【程序1】
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一 对兔子,假如兔子都不死,问每个月的兔子总数为多少?
1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....
import java.util.Scanner;

public class Fibonacci {

public int f(int n) {
if (n == 1)
return 1;
if (n == 2)
return 1;
return f(n - 1) + f(n - 2);
}

public static void main(String[] args) {
System.out.println("请输入您需要查询第几个月的兔子数量?");
Scanner s = new Scanner(System.in);
int n = s.nextInt();

Fibonacci fi = new Fibonacci();
for (int i = 1; i <= n; i++)
System.out.print(fi.f(i) + " ");
}
}

【程序2】
题目:判断101-200之间有多少个素数,并输出所有素数。
1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,
则表明此数不是素数,反之是素数。
public class PrimeNumber {

public boolean isPrimeNumber(int n) {
boolean flag = true;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
flag = false;
break;
}
}
return flag;
}

public static void main(String[] args) {
PrimeNumber p = new PrimeNumber();
int count = 0;
for (int n = 101; n <= 200; n++) {
if (p.isPrimeNumber(n)) {
System.out.print(n + " ");
count++;
if (count % 10 == 0) {
System.out.println();
}
}
}
}
}

【程序3】
题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如: 153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。
public class NarcissisticNumber {

public boolean isNarcissisticNumber(int n) {
boolean flag = false;
int unitsDigit = n % 10;
int tensDigit = (n / 10) % 10;
int hundredsDigit = n / 100;
if (f(unitsDigit) + f(tensDigit) + f(hundredsDigit) == n) {
flag = true;
}
return flag;
}

public int f(int n) {
return n * n * n;
}

public static void main(String[] args) {
NarcissisticNumber nn = new NarcissisticNumber();
for(int i = 100;i < 1000;i++){
if(nn.isNarcissisticNumber(i)){
System.out.print(i+" ");
}
}
}
}

【程序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 class Fenjie {

public void fenjie(int n) {
for (int i = 2; i <= n; i++) {
if (n % i == 0) {
if (n != i) {
System.out.print(i + "*");
} else {
System.out.print(i);
}
n = n / i;
i = 2;
}
}
}

public static void main(String[] args) {
int n = 90;
System.out.print(n + "=");
new Fenjie().fenjie(n);
}
}

【程序5】
题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下 的用C表示。
1.程序分析:(a>b)?a:b这是条件运算符的基本例子。
public class Scroe {

public static void main(String[] args) {
System.out.println("请输入一个1~100之间的分数:");
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
System.out.println((n < 60) ? "C" : ((n >= 90) ? "A" : "B"));
}

}

【程序6】
题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
1.程序分析:利用辗除法。
import java.util.Scanner;

public class NianChuFa {

/**
* 最大公约数
* @param m
* @param n
* @return
*/
public int gcd(int m, int n) {
if (n == 0) {
return m;
} else
return gcd(n, m % n);
}

/**
* 最小公倍数
* @param m
* @param n
* @return
*/
public int scp(int m, int n) {
return m * n / gcd(m, n);
}

public static void main(String[] args) {
System.out.println("请输入两个非负整数:");
Scanner scan = new Scanner(System.in);
int x = scan.nextInt();
int y = scan.nextInt();

System.out.println("最大公约数为:"+new NianChuFa().gcd(x, y));
System.out.println("最小公倍数为:"+new NianChuFa().scp(x, y));
}

}

【程序7】
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
1.程序分析:利用while语句,条件为输入的字符不为'\n'.
import java.util.Scanner;

/**
* 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数
*
* @author Administrator
*
*/
public class Test7 {

public static void main(String[] args) {
int count1 = 0; // 统计空格
int count2 = 0; // 统计字母
int count3 = 0; // 统计数字
int count4 = 0; // 统计其他

Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
char[] a = str.toCharArray();
for (int i = 0; i < a.length; i++) {
if (((int) a[i]) == 32) {
count1++;
} else if (((int) a[i] <= 90 && (int) a[i] >= 65)
|| ((int) a[i] <= 122 && (int) a[i] >= 97)) {
count2++;
} else if (((int) a[i] <= 57 && (int) a[i] >= 48)) {
count3++;
} else {
count4++;

}
}
System.out.println("空格的数目:" + count1);
System.out.println("字母的数目:" + count2);
System.out.println("数字的数目:" + count3);
System.out.println("其他的数目:" + count4);
}

}
【程序8】
题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加), 几个数相加有键盘控制。1.程序分析:关键是计算出每一项的值。
import java.util.Scanner;

public class Sum {

public static void main(String[] args) {
int s = 0;
int t = 0;
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
for (int i = 1; i <= n; i++) {
t = t * 10 + n;
s += t;
if (i != n) {
System.out.print(t + " + ");
}else{
System.out.print(t + " = ");
}
}
System.out.println(s);
}
}

【程序9】
题目:一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3.编程 找出1000以内的所有完 数。
public class Wanshu {

public boolean isWanshu(int n) {
boolean flag = false;
int sum = 0;
for (int i = 1; i < n; i++) {
if (n % i == 0) {
sum += i;
}
}
if (sum == n)
flag = true;
return flag;
}

public static void main(String[] args) {
for (int i = 1; i < 1000; i++) {
if (new Wanshu().isWanshu(i)) {
System.out.print(i + " ");
}
}
}

}

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

public static void main(String[] args) {
double hn = 100;
double sn = 100;

for(n=2;n<=10;n++){
sn=sn+2*hn;/*第n次落地时共经过的米数*/
hn=hn/2; /*第n次反跳高度*/
}
System.out.println("h = " + hn + "\n" + "s = " + sn);
}
}

【程序11】
题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。
public class Pailie {

public static void main(String[] args) {
for(int a = 1;a <= 4;a++){
for(int b = 1;b <= 4;b ++){
for(int c = 1;c <= 4;c ++){
if(a!=b&&b!=c&&a!=c){
System.out.println(a*100+b*10+c);
}
}
}
}
}
}

【程序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.程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。

import java.util.Scanner;

public class Profit {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double n = scan.nextInt();
double m = 0;

if (n <= 100000) {
m = n * 0.1;
} else if (n <= 200000) {
m = 10 * 0.1 + (n - 10) * 0.075;
} else if (n <= 400000) {
m = 10 * 0.1 + 10 * 0.075 + (n - 20) * 0.05;
} else if (n <= 600000) {
m = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (n - 40) * 0.03;
} else if (n <= 1000000) {
m = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + (n - 60)
* 0.015;
} else {
m = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + 40 * 0.015
+ (n - 100) * 0.01;
}

System.out.println("奖金为:" + m);
}

}


【程序13】
题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? 1.程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后的结果满足 如下条件,即是结果。请看具体分析:
public class SearchValue {

public boolean isSquare(int num) {
int i, sum;
for (i = 1, sum = 0; sum < num; ++i)
sum += (2 * i - 1);
if (sum == num)
return true;
return false;
}

public static void main(String[] args) {

for( int i = 0;i<100000;i++){
if(new SearchValue().isSquare(i+100)&&new SearchValue().isSquare(i+168)){
System.out.println(i);
}
}
}
}

【程序14】
题目:输入某年某月某日,判断这一天是这一年的第几天?
1.程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且 输入月份大于3时需考虑多加一天。
public class Test {

public static void main(String[] args) {

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date now;
try {
now = df.parse("2004-03-02");
java.util.Date date = df.parse("2004-01-01");
long l = now.getTime() - date.getTime();
long day = l / (24 * 60 * 60 * 1000);

System.out.println("" + day + "天");
} catch (ParseException e) {
e.printStackTrace();
}
}

}

【程序15】
题目:输入三个整数x,y,z,请把这三个数由小到大输出。
1.程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,然后再用x 与z进行比较,如果x>z则将x与z的值进行交换,这样能使x最小。
public class Compare {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int x = scan.nextInt();
int y =

scan.nextInt();
int z = scan.nextInt();
int temp;

if(x > y){
temp = x;
x = y;
y = temp;
}
if(x > z){
temp = x;
x = z;
z =temp;
}
if(y > z){
temp = y;
y = z;
z = temp;
}
System.out.println(x+" "+y+" "+z);
}

}

相关文档