【程序1】
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子对数为多少?
程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....
1.时间复杂度O(2^n)
public class Prog1 {
public static void main(String[] args){
System.out.println("第一个月有一对兔子,请输入月份:");
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
System.out.println("第"+n+"个月兔子总数为"+fun(n));
}
private static int fun (int n){//递归实现
if(n==1 || n==2)
return 1;
else
return fun(n-1)+fun(n-2);
}
}
2.时间复杂度O(n)
public long fibonacci(int n){
long last = 1;//上个月的兔子的对数
long current = 1;//当月的兔子的对数
long total = 1;
if(n<=0){
return 0;
}
if(n==1 || n==2){
return 1;
}
for(int i = 3; i <= n; i++){
total = last+ current;
last = current;
current = total;
}
return total;
}
【程序2】
题目:判断101-200之间有多少个素数,并输出所有素数。
程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。
public class Prog2 {
public static void main(String[] args){
int m = 1;
int n = 1000;
int count = 0;
//统计素数个数
for(int i = m;i
count++;
System.out.println(i+" ");
if(count%10==0){
System.out.println();
}
}
}
}
System.out.println();
System.out.println("在"+m+"和"+n+"之间共有+count+"个素数");
}
//判断素数
private static boolean isPrime(int n){
boolean flag = true;
if(n==1)
flag = false;
else{
for(int i = 2;i<=Math.sqrt(n);i++){
if((n%i)==0 || n==1){
flag = false;
break;
}
}
return flag;
}
}
【程序3】
题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。
public class Prog3{
public static void main(String[] args){
for(int i = 100; i < 1000; i++){
if(ifLotus(i))
System.out.print(i + " ");
}
System.out.println();
}
//判断水仙花数
private static boolean isLotus(int lotus){
int m = 0;
int n = lotus;
int sum = 0;
m = n/100;
n -= m*100;
sum = m*m*m;
m = n/10;
n -= m*10;
sum += m*m*m + n*n*n;
if(sum == lotus)
return true;
else
t=return false;
}
}
【程序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 Prog4 {
public static void main (String[] args){
int n = 13;
decompose(n);
}
private static void decompose(int n){
System.out.print(n + "=");
for(int i = 2; i < n+1; i++){
n/=i;
System.out.print(i+"i");
}
if(n==i){
System.out.println(i);
break;
}
}
}
【程序5】
题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
程序分析:(a>b)?a:b这是条件运算符的基本例子。
public class Prog5{
public static void main(String[] args){
int n = -1;
try {
//将数组里的第一个数据转换成Int型
n = Integer.parseInt(args[0]);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("请输入成绩");
return;
}
grade(n);
}
//成绩等级计算
private static void grade(int n){
if(n > 100 || n < 0)
System.out.print("输入无效");
else{
String str = (n>=90)?"分,属于A等":((n>60)?"分,属于B等":"分,属于C等");
System.out.println(n+str);
}
}
}
【程序6】
题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
程序分析:利用辗除法。
public class Prog6{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("请输入第一个整数:");
int m = sc.nextInt();
System.out.print("请输入第二个整数:");
int n = sc.nextInt();
max_min(m,n);
}
//求最大公约数和最小公倍数
private static void max_min(int m,int n){
int temp = 1;
int yshu = 1;
int bshu = m*n;
if(n < m){
temp = n;
n = m;
m = temp;
}
while(m!=0){//辗转相除法
temp = n%m;
n = m;
m = temp;
}
yshu = n;
//两个数的乘积等于这两个数的最大公约数与最小公倍数的积
bshu /= n;
System.out.print(m+"和"+n+"的最大公约数为"+yshu);
System.out.print(m+"和"+n+"的最小公约数为"+bshu);
}
}
//最大公约数
public int max(int m, int n){
if(m < n){ //保证m>n
int temp = n;
n = m;
m = temp;
}
if(m%n == 0){
return n;
}
return max(n,m%n);
}
//最小公倍数
public int min(int m, int n){
return m*n/max(m,n);
}
【程序7】
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
程序分析:利用while语句,条件为输入的字符不为'\n'.
import java.util.Scanner;
public class Prog7_1 {
public static void main(String[] args){
System.out.print("请输入一串字符:");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();//读一行转为字符串
sc.close();
count(str);
}
//统计输入的字符数
private static void count(String str){
String E1 = "[\u4e00-\u9fa5]";//汉字
String E2 = "[a-zA-z]";
String E3 = "[0-9]";
String E4 = "\\s";//空格
int countChinese = 0;
int countLetter = 0;
int countNumber = 0;
int countSpace = 0;
int countOther = 0;
char[] array_Char = str.toCharArray();//将字符串转化为字符数组
String[] array_String = new String [array_Char.length];//汉字只能作为字符串处理
for(int i =0;i
//遍历字符串数组中的元素
for(String s : array_String){
if(s.matches(E1))
countChinese++;
else if(s.matches(E2))
countLetter++;
else if(s.matches(E3))
countNumber++;
else if(s.matches(E4))
countSpace++;
else
countOther++;
}
System.out.println("输入的汉字个数:"+countChinese);
System.out.println("输入的字母个数:"+countLetter);
System.out.println("输入的数字个数:"+countNumber);
System.out.println("输入的空格个数:"+countSpace);
System.out.println("输入的其它字符个数:"+countSpace);
}
}
import java.util.*;
public class Prog7_2{
public static void main(String[] args){
System.out.println("请输入一行字符");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
sc.close();
count(str);
}
//统计输入的字符
private static void count(String str){
List
char[] array_Char = str.toCharArray();
for(char c : array_Char)
list.add(String.valueOf(c));//将字符作为字符串添加到list表中
Collections.sort(list);//排序
for (String s : list){
int begin = list.indexOf(s);
int end = list.lastIndexOf(s);
//索引结束统计字符数
if(list.get(end)==s)
System.out.println(“字符‘”+s+“’有”+(end-begin+1)+“个”);
}
}
}
【程序8】
题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
程序分析:关键是计算出每一项的值。
import java.util.Scanner;
public class Prog8{
public static void main(String[] args){
System.out.print("求s=a+aa+aaa+aaaa+...的值,请输入a的值:");
Scanner sc = new Scanner(System.in).useDelimiter("\\s*");//以空格作为分隔符
int a = sc.nextInt();
int n = sc.nextInt();
sc.close();//关闭扫描器
System.out.println(expressed(a,n)+add(a,n));
}
//求和表达式
private static String expressed(int a,int n){
StringBuffer sb = new StringBuffer();
StringBuffer subSB = new StringBuffer();
for(int i = 1;i < n+1; i++){
subSB = subSB.append(a);
sb = sb.append(subSB);
if(i
}
sb.append("=");
return sb.toString();
}
//求和表达式
private static long add(int a,int n){
long sum = 0;
long subSUM = 0;
for(int i = 1; i < n+1; i++){
subSUM = subSUM*10+a;
sum = sum + subSUM;
}
return sum;
}
}
【程序9】
题目:一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3.编程找出1000以内的所有完数。
public class Prog9{
public static void main(String[] args){
int n = 1000;
compNumber(n);
}
//求完数
private static void compNumber(int n){
int count = 0;
System.out.println(n+"以内的完数:");
for(int i = 1;i
for(int j = 1; j < i/2+1;j++){
if((i%j)==0){
sum+=j;
if(sum == i){
System.out.print(i+"");
if((count++)%5==0)
System.out.println();
}
}
}
}
}
}
【程序10】
题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高?
import java.util.Scanner;
public class Prog10{
public static void main(String[] args){
System.out.print("请输入小球落地时的高度和求解的次数:");
Scanner sc = new Scanner(System.in).useDelimiter("\\s");
int h = sc.nextInt();
int n = sc.nextInt();
sc.close();
distance(h,n);
}
//小球从h高度落下,经过n次反弹后经过的距离和反弹的高度
private static void distance(int h,int n){
double length = 0;
for(int i =0;i
h /= 2.0;
}
System.out.println("经过第"+n+"次反弹后,小球共经过"+(length-100)+"米,第n次反弹高度为"+h+”米“);
}
}
【程序11】
题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。
public class Prog11 {
public static void main(String[] args){
int count = 0;//保存数字个数
int n = 0;
//i保存百位上的数
//j保存十位上的数
//k保存个位上的数
for (int i = 0; i < 5; i++){
for (int j = 0; j < 5; j++){
if(j == i)
contine;
for (int k = 1; k < 5; k++){
if (k != i && k != j){
n = i*100+j*10+k;
System.out.print(n+" ");
if ((++count)%5==0)
System.out.println();
}
}
}
}
System.out.println();
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,求应发放奖金总数?
程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。
import java.io.*
public class Prog12 {
public static void mian(String[] args){
System.out.print("请输入当前利润:");
//java.lang.Long.parseLong(String s)方法解析的字符串参数s作为一个符号的十进制长。
long profit = Long.parseLong(key_Input());
System.out.println("应发奖金:"bonus(profit));
}
//接受从键盘输入的内容
private static String key_Input(){
String str = null;
BufferReader bufIn = new BufferReader(new InputStreamReader(System.in));
try {
str = bufIn.readLine();
}catch(IOException e){
e.printStackTrace();
}finally{
try{
bufIn.close();
}catch(){
e.printStackTrace();
}
}
return str;
}
//计算奖金
private static long bonus(long profit){
long prize = 0;
long profit_sub = profit;
if (profit > 1000000){
profit = profit_sub-1000000;
profit_sub = 1000000;
prize += profit*0.01;
}
if (profit > 600000){
profit = profit_sub-600000;
profit_sub = 600000;
prize += profit*0.015;
}
if (profit > 400000){
profit = profit_sub-400000;
profit_sub = 400000;
prize += profit*0.03;
}
if (profit > 200000){
profit = profit_sub-200000;
profit_sub = 200000;
prize += profit*0.05;
}
if (profit > 100000){
profit = profit_sub-100000;
profit_sub = 100000;
prize += profit*0.075;
}
prize += prifit_sub*0.1;
return prize;
}
}
【程序13】
题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后的结果满足如下条件,即是结果。
public class Prog13 {
public static void main(String[] args){
int n = 0;
for(int i = 0; i <100001; i++){
if(isCompSqrt(i+100) && isCompSqrt(i+268)){
n = i;
break;
}
}
system.out.println("所求的数是:"+n);
}
//判断完全平方数
private static boolean isCompSqrt(int n){
boolean isComp = false;
for(int i = 1; i < Math.sqrt(n)+1; i++){
if(n==Math.pow(i,2)){
isComp = true;
break;
}
}
return isComp;
}
}
【程序14】
题目:输入某年某月某日,判断这一天是这一年的第几天?
程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。
import java.util.Scanner;
public class Prog14 {
public static void main(String[] args){
Scanner scan = new Scanner(System.in).useDelimiter("\\D");//匹配数字 \d表示0-9任意一个数字
System.out.print("请输入当前日期(年-月-日):");
int year = scan.nextInt();
int month = scan.nextInt();
int date = scan.nextInt();
scan.close();
System.out.println("今天是"+year+"年的第"+analysis(year,month,date)+"天");
}
//判断天数
private static int analysis(int year, int month, int date){
int n = 0;
int[] month_date = new int[] {0,31,28,31,30,31,30,31,31,30,31,30};
if((year%400)==0 || ((year%4)==0)&&((year%100)!=0))
month_date[2] = 29;
for(int i = 0; i < month; i++)
n += month_date[i];
return n+date;
}
}
【程序15】
题目:输入三个整数x,y,z,请把这三个数由小到大输出。
程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,然后再用x与z进行比较,如果x>z则将x与z的值进行交换,这样能使x最小。
import java.util.Scanner;
public class Prog15 {
public static void mian(String[] args){
Scanner scan = new Scanner(System.in).useDelimiter("\\D");
System.out.print("请输入三个数:");
int x = scan.nextInt();
int y = scan.nextInt();
int z = scan.nextInt();
scan.close();
Syetem.out.println("排序结果:"+sort(x,y,z));
}
//比较两个数的大小
private static String sort(int x, int y, int z){
String s = null;
if(x>y){
int t = x ;
x = y;
y = t;
}
if(x>z){
int t = x ;
x = z;
z = t;
}
if(y>z){
int t = z ;
z = y;
y = t;
}
s = x+" "+y+" "+z;
return s;
}
}
【程序16】
题目:输出9*9口诀。
程序分析:分行与列考虑,共9行9列,i控制行,j控制列。
public class Prog16 {
public static void main(String[] args){
for(int i = 1; i < 10; i++){
for(int j = 1; j < i+1; j++)
System.out.print(j+"*"+i+"="+(j*i)+" ");
System.out.println();
}
}
}
【程序17】
题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
程序分析:采取逆向思维的方法,从后往前推断。
public class Prog17 {
public static void main(String[] args){
int m = 1;
for(int i = 10; i >0; i--)
m = 2*m + 2;
System.out.println("小猴子共摘了"+m+"桃子");
}
}
【程序18】
题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。
import java.util.ArrayList;
public class Porg18 {
String a, b, c;//甲队队员
public static void main(String [] args){
String[] racer = {"x","y","z"};//乙对队员
ArrayList
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
for(int k = 0; k < 3; k++) {
Prog18 prog18 = new Prog18(racer[i],racer[j],racer[k]);
if(!prog18.a.equals(prog18.b) && !prog18.a.equals(prog18.c)) && !prog18.b.equals(prog18.c)
&& !prog18.a.equals("x") !prog18.c.equals("x") && !prog18.c.equals("z")
arrayList.add(prog18);
}
for(Object obj:arrayList)
System.out.println(obj);
}
//构造方法
private Prog18(String a, String b, String c){
this.a = a;
this.b = b;
this.c = c;
}
public String toString(){
return "a的对手是"+a+" "+"b的对手是"+b+" "+"c的对手是"+c+" ";
}
}
【程序19】
题目:打印出如下图案(菱形)
*
***
*****
*******
*****
***
*
程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重 for循环,第一层控制行,第二层控制列。
public class Prog19{
public static void main(String[] args){
int n = 4;
printStar(n);
}
//打印星星
private static void printStar(int n){
//打印上半部分
for(int i=0;i
if(j
if(j>=n-i && j<=n+i)
System.out.print("*");
}
System.out.println();
}
//打印下半部分
for(int i=1;i
for(int j=0;j<2*n-i;j++){
if(j System.out.print(" ");
if(j>=i && j<2*n-i-1)
System.out.print("*");
}
System.out.println();
}
}
}
【程序20】
题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。
程序分析:请抓住分子与分母的变化规律。
public class Prog19{
public static void main(String[] args){
double n1 = 1;
double n1 = 1;
double fraction = n1/n2;
double Sn = 0;
for (int i = 0; i < 20; i++){
double t1 = n1;
double t2 = n2;
n1 = t1+t2;
n2 = t1;
fraction = n1/n2;
Sn += fraction;
}
System.out.print(Sn);
}
}