文档内容
淘宝店铺:洋码头东东宝贝
【程序1】
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又
生一对兔子,假如兔子都不死,问每个月的兔子对数为多少?
程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....
public class Prog1{
public static void main(String[] args){
int n = 10;
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】
题目:判断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;ik,但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=90分的同学用A表示,60-89分之间的用B表示,60
分以下的用C表示。淘宝店铺:洋码头东东宝贝
程序分析:(a>b)?a:b这是条件运算符的基本例子。
public class Prog5{
public static void main(String[] args){
int n = -1;
try{
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.println("输入无效");
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){
int m,n;
try{
m = Integer.parseInt(args[0]);
n = Integer.parseInt(args[1]);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("输入有误");
return;
}
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 list = new ArrayList();
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 scan = new Scanner(System.in).useDelimiter("\\s*");//以空格作为分隔符
int a = scan.nextInt();
int n = scan.nextInt();
scan.close();//关闭扫描器
System.out.println(expressed(2,5)+add(2,5));
}
//求和表达式
private static String expressed(int a,int n){
StringBuffer sb = new StringBuffer();
StringBuffer subSB = new StringBuffer();
for(int i=1;i1000000){
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 += prize*0.05;
}
if(profit>100000){
profit = profit_sub-100000;
profit_sub = 100000;
prize += profit*0.075;
}
prize += profit_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;iy则将x与y的值进行交换,然后
再用x与z进行比较,如果x>z则将x与z的值进行交换,这样能使x最小。
import java.util.Scanner;
public class Prog15{
public static void main(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();
System.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;j0;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 Prog18{
String a,b,c;//甲队成员
public static void main(String[] args){
String[] racer = {"x","y","z"};//乙队成员
ArrayList arrayList = new 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 = 5;
printStar(n);
}
//打印星星
private static void printStar(int n){
//打印上半部分
for(int i=0;i=n-i && j<=n+i)淘宝店铺:洋码头东东宝贝
System.out.print("*");
}
System.out.println();
}
//打印下半部分
for(int i=1;i=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 Prog20{
public static void main(String[] args){
double n1 = 1;
double n2 = 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);
}
}
【程序21】
题目:求1+2!+3!+...+20!的和
程序分析:此程序只是把累加变成了累乘。
public class Prog21{
public static void main(String[] args){
long sum = 0;
for(int i=0;i<20;i++)
sum += factorial(i+1);
System.out.println(sum);
}
//阶乘
private static long factorial(int n){
int mult = 1;
for(int i=1;i99999){
System.out.println("输入的不是5位数!");
return;
}else{
for(int i=0;i<5;i++){
a[i] = n%10;
n /= 10;
}
if(a[0]==a[4] && a[1]==a[3])
System.out.println(m+"是一个回文数");
else
System.out.println(m+"不是回文数");
}
}
}
【程序26】
题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续 判断第二个字母。
程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母。
import java.io.*;
public class Prog26{
public static void main(String[] args){
String str = new String();
BufferedReader bufIn = new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入星期的英文单词前两至四个字母):");
try{
str = bufIn.readLine();
}catch(IOException e){
e.printStackTrace();
}finally{
try{
bufIn.close();
}catch(IOException e){
e.printStackTrace();
}
}
week(str);
}
private static void week(String str){
int n = -1;
if(str.trim().equalsIgnoreCase("Mo") || str.trim().equalsIgnoreCase("Mon") ||
str.trim().equalsIgnoreCase("Mond"))
n = 1;
if(str.trim().equalsIgnoreCase("Tu") || str.trim().equalsIgnoreCase("Tue") ||
str.trim().equalsIgnoreCase("Tues"))
n = 2;
if(str.trim().equalsIgnoreCase("We") || str.trim().equalsIgnoreCase("Wed") ||
str.trim().equalsIgnoreCase("Wedn"))
n = 3;
if(str.trim().equalsIgnoreCase("Th") || str.trim().equalsIgnoreCase("Thu") ||
str.trim().equalsIgnoreCase("Thur"))
n = 4;
if(str.trim().equalsIgnoreCase("Fr") || str.trim().equalsIgnoreCase("Fri") ||
str.trim().equalsIgnoreCase("Frid"))淘宝店铺:洋码头东东宝贝
n = 5;
if(str.trim().equalsIgnoreCase("Sa") || str.trim().equalsIgnoreCase("Sat") ||
str.trim().equalsIgnoreCase("Satu"))
n = 2;
if(str.trim().equalsIgnoreCase("Su") || str.trim().equalsIgnoreCase("Sun") ||
str.trim().equalsIgnoreCase("Sund"))
n = 0;
switch(n){
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
case 0:
System.out.println("星期日");
break;
default:
System.out.println("输入有误!");
break;
}
}
}
【程序27】
题目:求100之内的素数
public class Prog27{
public static void main(String[] args){
int n = 100;
System.out.print(n+"以内的素数:");
for(int i=2;ia[j+1]){
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
for(int i=0;iA[j]){
int temp = min;
min = A[j];
A[j] = temp;
}淘宝店铺:洋码头东东宝贝
B[i] = min;
}
}
B[A.length-1] = A[A.length-1];
return B;
}
//打印
private static void print(int[] A){
for(int i=0;i0;i--)
if(a>A[i]){
B[i+1] = a;
for(int j=0;j<=i;j++)
B[j] = A[j];
for(int k=i+2;k1){
if(isIn[index]){
countNum++;
if(countNum==3){
countNum = 0;
isIn[index] = false;
inCount--;
}
}
index++;
if(index==n)
index = 0;
}
for(int i=0;i=1;i--){
for(int j=0;j<=i-1;j++){
if(str[j].compareTo(str[j+1])<0){
String temp = str[j];
str[j] = str[j+1];
str[j+1] = temp;
}
}
}
for(String subStr:str)
System.out.print(subStr+" ");
}
}
【程序41】
题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的
一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔
入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?
public class Prog41{
public static void main(String[] args){
int n;
n = fun(0);
System.out.println("原来有"+n+"个桃子");
}
private static int fun(int i){淘宝店铺:洋码头东东宝贝
if(i==5)
return 1;
else
return fun(i+1)*5+1;
}
}
【程序42】
题目:809*??=800*??+9*??+1
其中??代表的两位数,8*??的结果为两位数,9*??的结果为3位数。求??代表的两位数,及
809*??后的结果。
public class Prog42{
public static void main(String[] args){
int n = 0;
boolean flag = false;
for(int i=10;i<100;i++)
if(809*i==800*i+9*i+1){
flag = true;
n = i;
break;
}
if(flag)
System.out.println(n);
else
System.out.println("无符合要求的数!");
}
}
【程序43】
题目:求0—7所能组成的奇数个数。
public class Prog43{
public static void main(String[] args){
int count = 0;
//声明由数字组成的数
int n = 8;
//一位数
count = n/2;
//两位数
count += (n-1)*n/2;
//三位数
count += (n-1)*n*n/2;
//四位数
count += (n-1)*n*n*n/2;
//五位数
count += (n-1)*n*n*n*n/2;
//六位数
count += (n-1)*n*n*n*n*n/2;
//七位数
count += (n-1)*n*n*n*n*n*n/2;
System.out.println("0-7所能组成的奇数个数:"+count);
}
}
【程序44】
题目:一个偶数总能表示为两个素数之和。
import java.util.Scanner;
public class Prog44{
public static void main(String[] args){
System.out.print("请输入一个偶数:");
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
scan.close();
if(n%2!=0){淘宝店铺:洋码头东东宝贝
System.out.println("您输入的不是偶数!");
return;
}
twoAdd(n);
}
//偶数分解为素数之和
private static void twoAdd(int n){
for(int i=2;i8){
n /= 9;
count++;
}
System.out.println(l+"能被"+count+"个9整除。");
}
}
【程序46】
题目:两个字符串连接程序
public class Prog46{
public static void main(String[] args){
String str1 = "lao lee";
String str2 = "牛刀";
String str = str1+str2;
System.out.println(str);
}
}
【程序47】
题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。
import java.util.Scanner;
public class Prog47{
public static void main(String[] args){
System.out.print("请输入7个整数(1-50):");
Scanner scan = new Scanner(System.in);
int n1 = scan.nextInt();淘宝店铺:洋码头东东宝贝
int n2 = scan.nextInt();
int n3 = scan.nextInt();
int n4 = scan.nextInt();
int n5 = scan.nextInt();
int n6 = scan.nextInt();
int n7 = scan.nextInt();
scan.close();
printStar(n1);
printStar(n2);
printStar(n3);
printStar(n4);
printStar(n5);
printStar(n6);
printStar(n7);
}
static void printStar(int m){
System.out.println(m);
for(int i=0;i=0;i--){
a[i] = n%10;
n /= 10;
}
for(int i=0;i<4;i++)
System.out.print(a[i]);
System.out.println();
for(int i=0;i