赞
踩
1.古典问题:有一对兔子,从出生后第 3 个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
//这是一个菲波拉契数列问题
- public class test1 {
- public static void main(String args[]){
- int f1=2,f2=2,temp;
- System.out.printf("第1个月有2只兔子\n");
- System.out.printf("第2个月有2只兔子\n");
- for(int i=3;i<=24;i++){
- temp=f2;
- f2=f1+f2;
- f1=temp;
- System.out.printf("第%d个月有%d只兔子\n",i,f2);
- }
- }
- }
2.判断101-200 之间有多少个素数,并输出所有素数。
- public class test2 {
- public static void main(String args[]){
- int k=0;
- for(int i=101;i<=200;i++){
- if(check(i)==1){
- System.out.println(i);
- k++;
- }
- }
- System.out.printf("有%d个素数",k);
- }
- public static int check(int n){
- int flag=0;
- for(int i=2;i<n;i++){
- if(n%i==0){
- flag=0;
- break;
- }
- else{
- flag=1;
- }
- }
- return flag;
- }
- }

3.打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。例如:153 是一个 "水仙花数 ",因为 153=1 的三次方+5 的三次方+3 的三次方。
- public class test3 {
- public static void main(String args[]){
- for(int i=100;i<1000;i++){
- if(Math.pow((i/1%10),3)+Math.pow((i/100%10),3)+Math.pow((i/10%10),3)==i){
- System.out.println(i);
- }
- }
- }
- }
4.将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
- public class test4 {
- public static void main(String args[]){
- Scanner sc=new Scanner(System.in);
- int n= sc.nextInt();
- System.out.printf("%d=",n);
- int k=2;
- while(k<=n){
- if(k==n){
- System.out.println(n);
- break;
- }
- else if(n%k==0){
- System.out.printf(k+"*");
- n=n/k;
- }
- else{
- k++;
- }
- }
- }
- }

5.利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B 表示,60 分以下的用C 表示。
- public class test5 {
- public static void main(String args[]){
- Scanner sc=new Scanner(System.in);
- char grade;
- int x=sc.nextInt();
- grade=x>=90?'A':x>=60?'B':'C';
- System.out.println("等级为:"+grade);
- }
- }
6.输入两个正整数m 和n,求其最大公约数和最小公倍数。
- public class test6 {
- public static void main(String args[]){
- Scanner sc=new Scanner(System.in);
- int a=sc.nextInt();
- int b=sc.nextInt();
- int max,n=a*b;
- int temp;
- if(a<b){ //把a当做大数
- temp=a;
- a=b;
- b=temp;
- }
- while(b!=0){
- if(a==b){
- max=a;
- }
- else{
- int k=a%b;
- a=b;
- b=k;
- }
- }
- max=a;
- int min=n/max;
- System.out.printf("最大公约数:%d,最小公倍数:%d",max,min);
- }
- }

7.输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
- public class test7 {
- public static void main(String args[]){
- Scanner sc=new Scanner(System.in);
- int shuzi=0,zimu=0,kongge=0,qita=0;
- char[] c=null;
- String s=sc.nextLine();
- c=s.toCharArray();
- for(int i=0;i<c.length;i++){
- if('0'<=c[i]&&c[i]<='9'){
- shuzi++;
- }
- else if(('a'<=c[i]&&c[i]<='z')||('A'<=c[i]&&c[i]<='Z')){
- zimu++;
- }
- else if(c[i]==' '){
- kongge++;
- }
- else{
- qita++;
- }
- }
- System.out.printf("数字:%d,字母:%d,空格:%d,其他:%d",shuzi,zimu,kongge,qita);
- }
- }

8.求s=a+aa+aaa+aaaa+aa...a 的值,其中a 是一个数字。例如2+22+222+2222+22222(此时共有5 个数相加),几个数相加有键盘控制。
- public class test8 {
- public static void main(String srgs[]){
- Scanner sc=new Scanner(System.in);
- System.out.printf("这个数是:");
- int a=sc.nextInt();
- System.out.printf("几个这个数相加:");
- int n=sc.nextInt();
- int sum=0,b=0;
- for(int i=1;i<=n;i++){
- b=b+a;
- sum=sum+b;
- a=a*10;
- System.out.println(b);
- }
- System.out.println(sum);
- }
- }

9.一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3.编程找出1000 以内的所有完数。
- public class test9 {
- public static void main(String args[]) {
- for(int i=1;i<1000;i++){
- int sum=0;
- for(int j=1;j<i;j++){
- if(i%j==0){
- sum=sum+j;
- }
- }
- if(sum==i){
- System.out.println(i+" ");
- }
- }
- }
- }
10.一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10 次落地时,共经过多少米?第10 次反弹多高?
- public class test10 {
- public static void main(String args[]){
- double a=100,c=100;
- for(int i=1;i<=10;i++){
- c=c+a;
- a=a/2;
- }
- System.out.printf("共经过"+c+"米\n");
- System.out.printf("第10次反弹"+a+"米");
- }
- }
11.有1、2、3、4四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
- public class test11 {
- public static void main(String args[]){
- int count=0;
- for(int i=1;i<5;i++){
- for(int j=1;j<5;j++){
- for(int k=1;k<5;k++){
- if(i!=j&&i!=k&&j!=k){
- System.out.println(i*100+j*10+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%提成,从键盘输入当月利润,求应发放奖金总数?
- public class test12 {
- public static void main(String args[]){
- double jiangjin=0;
- Scanner sc=new Scanner(System.in);
- double x=sc.nextDouble();
- if(10<=x){
- jiangjin=x*0.1;
- }
- if(10<x&&x<=20){
- jiangjin=10*0.1+(x-10)*0.075;
- }
- if(20<x&&x<=40){
- jiangjin=10*0.1+10*0.075+(x-20)*0.05;
- }
- if(40<x&&x<=60){
- jiangjin=10*0.1+10*0.075+20*0.05+(x-40)*0.03;
- }
- if(60<x&&x<=100){
- jiangjin=10*0.1+10*0.075+20*0.05+20*0.03+(x-60)*0.015;
- }
- if(100<x){
- jiangjin=10*0.1+10*0.075+20*0.05+20*0.03+40*0.015+(x-100)*0.01;
- }
- System.out.print(jiangjin);
- }
- }

13.一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平 方数,请问该数是多少?
//注意负数也是整数,循环从负数开始
- public class test13 {
- public static void main(String args[]){
- for(int i=-100;i<100000;i++){
- if(check(i+100)==1){
- if(check(i+268)==1) {
- System.out.println(i);
- }
- }
- }
- }
- public static int check(int n){
- int flag=0;
- if(Math.sqrt(n)%1==0){
- flag=1;
- }
- return flag;
- }
- }

14.输入某年某月某日,判断这一天是这一年的第几天?
- public class test14 {
- public static void main(String args[]){
- Scanner sc=new Scanner(System.in);
- int year=sc.nextInt();
- int month=sc.nextInt();
- int day=sc.nextInt();
- int sum=0,sum1;
- if (year % 400 == 0 ||(year%4==0&&year%100==0)) {
- int[] ch29=new int[]{31,29,31,30,31,30,31,31,30,31,30,31};
- for(int i=0;i<month-1;i++){
- sum=sum+ch29[i];
- }
- sum1=sum+day;
- System.out.println(sum1);
- }
- else{
- int[] ch28=new int[]{31,28,31,30,31,30,31,31,30,31,30,31};
- for(int i=0;i<month-1;i++){
- sum=sum+ch28[i];
- }
- sum1=sum+day;
- System.out.println(sum1);
- }
- }
- }

15.x、y、z三个数排列大小
- public class test15 {
- public static void main(String args[]){
- int temp;
- Scanner sc=new Scanner(System.in);
- int x=sc.nextInt();
- int y=sc.nextInt();
- int z=sc.nextInt();
- if(x>y){
- temp=y;
- y=x;
- x=temp;
- }
- if(x>z){
- temp=z;
- z=x;
- x=temp;
- }
- if(y>z){
- temp=z;
- z=y;
- y=temp;
- }
- System.out.printf("%d ",x);
- System.out.printf("%d ",y);
- System.out.printf("%d",z);
- }
- }

16.打印九九乘法表
- public class test16 {
- public static void main(String args[]){
- for(int i=1;i<10;i++){
- System.out.println("\n");
- for(int j=1;j<=i;j++){
- System.out.printf(j+"*"+i+"="+i*j+" ");
- }
- }
- }
- }
17.猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第 10 天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
//逆向思维,(后一天的桃子数+1)*2 得到前一天剩下的桃子数
- public class test17 {
- public static void main(String args[]){
- int x=1;
- for(int i=9;i>=1;i--){
- x=(x+1)*2; /*第一天的桃子数是第二天桃子数加1后的2倍*/
- }
- System.out.println(x);
- }
- }
18.两个乒乓球队进行比赛,各出三人。甲队为 a,b,c 三人,乙队为 x,y,z 三人。已抽签定比赛名单。有人向队员打听比赛的名单。a 说他不和 x 比,c 说他不和 x,z 比,请编程序找出三队赛手的名单。
- public class test18 {
- public static void main(String args[]){
- char[] ch1=new char[]{'a','b','c'};
- char[] ch2=new char[]{'x','y','z'};
- for(int i=0;i<ch1.length;i++){
- for(int j=0;j<ch2.length;j++){
- if(ch1[i]=='a'&&ch2[j]=='x'||ch1[i]=='a'&&ch2[j]=='y'){
- continue;
- }
- else if(ch1[i]=='b'&&ch2[j]=='y'||ch1[i]=='b'&&ch2[j]=='z'){
- continue;
- }
- else if(ch1[i]=='c'&&ch2[j]=='x'||ch1[i]=='c'&&ch2[j]=='z'){
- continue;
- }
- else{
- System.out.printf(ch1[i]+" vs "+ch2[j]+"\n");
- }
- }
- }
- }
- }

19.打印出如下图案(菱形)

- public class test19 {
- public static void main(String args[]){
- int gao=7,kuan=7;//相同的奇数
- for(int i=1;i<=(gao+1)/2;i++){//包括最宽行以上面组成的三角形
- for(int j=0;j<=kuan/2-i;j++){
- System.out.printf(" ");
- }
- for(int k=0;k<i*2-1;k++){
- System.out.printf("*");
- }
- System.out.println();
- }
- for(int i=1;i<=gao/2;i++){//不包括最宽行以下面组成的三角形
- for(int j=0;j<i;j++){
- System.out.printf(" ");
- }
- for(int k=0;k<kuan-i*2;k++){
- System.out.printf("*");
- }
- System.out.println();
- }
- }
- }

20.有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前 20 项之和。
- public class test20 {
- public static void main(String args[]){
- double sum=0,fenzi=2,fenmu=1,t;
- for(int i=1;i<=20;i++){
- sum=sum+fenzi/fenmu;
- t=fenmu;
- fenmu=fenzi;
- fenzi=fenzi+t;
- }
- System.out.println(sum);
- }
- }
21.求1+2!+3!+...+20!的和
- public class test21 {
- public static void main(String args[]){
- long sum=0;
- for(int i=1;i<=20;i++){
- sum=check(i);
- }
- System.out.println(sum);
- }
- public static long check(int n){
- long a=1,sum=0;
- if(n==0){
- sum=1;
- }
- else {
- for (int i = 1; i <= n; i++) {
- a = a * i;
- sum = sum + a;
- }
- }
- return sum;
- }
- }

22.利用递归方法求 5!
- public class test22 {
- public static void main(String args[]){
- System.out.println(check(5));
- }
- public static int check(int n){
- if(n==1){
- n=1;
- }
- else{
- n=n*(check(n-1));
- }
- return n;
- }
- }
23.有 5 个人坐在一起,问第五个人多少岁?他说比第 4 个人大 2 岁。问第 4 个人岁数,他说比第 3 个人大 2 岁。问第三个人,又说比第 2 人大两岁。问第 2 个人,说比第一个人大两岁。最后问第一个人,他说是 10 岁。请问第五个人多大?
- public class test23 {
- public static void main(String args[]){
- int a=10;
- for(int i=2;i<=5;i++){
- a=a+2;
- }
- System.out.println(a);
- }
- }
24.给一个不多于 5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
- import java.util.Scanner;
-
- public class test24 {
- public static void main(String args[]){
- Scanner sc=new Scanner(System.in);
- long n=sc.nextLong();
- String s=Long.toString(n);
- char[] ch=s.toCharArray();
- if(ch.length<5&&n%1==0) {
- System.out.println("这个数是"+ch.length+"位数");
- for (int i = ch.length - 1; i >= 0; i--) {//注意i=ch.length-1,要用长度-1,否则会数组越界
- System.out.print(ch[i]);
- }
- }
- else{
- System.out.printf("请输入不多于5位的正整数");
- }
- }
- }

25.一个 5位数,判断它是不是回文数。即 12321是回文数,个位与万位相同,十位与千位相同。
- import java.util.Scanner;
-
- public class test25 {
- public static void main(String args[]){
- Scanner sc=new Scanner(System.in);
- int n=sc.nextInt();
- if(n>10000&&n<100000){
- if(n/1%10==n/10000%10&&n/10%10==n/1000%10){
- System.out.printf(n+"是回文数");
- }
- }
- else{
- System.out.printf("请输入一个5位正整数");
- }
- }
- }

26.请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母。
- import java.util.Scanner;
-
- public class test26 {
- public static void main(String args[]){
- System.out.printf("请输入第一个字母:");//默认第一个字母大写第二个字母小写
- Scanner sc=new Scanner(System.in);
- char n=sc.next().charAt(0);//charAt() 方法用于返回指定索引处的字符。索引范围为从 0 到 length() - 1。
- if(n=='M'){
- System.out.println("Monday");
- }
- else if(n=='W'){
- System.out.println("Wednesday");
- }
- else if(n=='F'){
- System.out.println("Friday");
- }
- else if(n=='T'){
- System.out.printf("请输入第二个字母:");
- Scanner sc1=new Scanner(System.in);
- char n1=sc1.next().charAt(0);
- if(n1=='u'){
- System.out.println("Tuesday");
- }
- if(n1=='h'){
- System.out.println("Thursday");
- }
- }
- else if(n=='S'){
- System.out.printf("请输入第二个字母:");
- Scanner sc1=new Scanner(System.in);
- char n1=sc1.next().charAt(0);
- if(n1=='a'){
- System.out.println("Saturday");
- }
- if(n1=='u'){
- System.out.println("Sunday");
- }
- }
- else{
- System.out.println("无此写法");
- }
- }
- }

27.求 100之内的素数
- public class test27 {
- public static void main(String args[]){
- for(int i=2;i<100;i++){
- if(check(i)==1){
- System.out.printf(i+" ");
- }
- }
- }
- public static int check(int n){
- int flag=1;
- for(int i=2;i<=Math.sqrt(n);i++){
- if(n%i==0){
- flag=0;
- break;
- }
- }
- return flag;
- }
- }

28.对 10个数进行排序
- import java.util.Scanner;
-
- public class test28 {
- public static void main(String args[]){//默认升序(从小到大)
- Scanner sc=new Scanner(System.in);
- int[] a=new int[10];
- for(int i=0;i<10;i++){
- a[i]=sc.nextInt();
- }
- for(int i=0;i<a.length-1;i++){
- for(int j=0;j<a.length-1;j++){
- if(a[j]>a[j+1]){//把>改成<就是降序(从大到小)
- int t=a[j+1];
- a[j+1]=a[j];
- a[j]=t;
- }
- }
- }
- for(int k=0;k<=a.length-1;k++){
- System.out.printf(a[k]+" ");
- }
- }
- }

29.求一个 3*3矩阵对角线元素之和
//主副对角线的元素我都相加了
- import java.util.Scanner;
-
- public class test29 {
- public static void main(String args[]){
- int sum=0;
- Scanner sc=new Scanner(System.in);
- int[][] a=new int[3][3];
- for(int i=0;i<3;i++){
- for(int j=0;j<3;j++){
- a[i][j]=sc.nextInt();
- System.out.printf("a["+i+"]["+j+"]="+a[i][j]+"\n");
- }
- }
- for(int i=0;i<3;i++){
- for(int j=0;j<3;j++){
- System.out.printf(a[i][j]+" ");
- }
- System.out.println();
- }
- for(int i=0;i<3;i++){
- for(int j=0;j<3;j++){
- if(i==j||i+j==2) {
- sum=sum+a[i][j];
- }
- }
- }
- System.out.printf("对角线元素之和:"+sum);
- }
- }

30.有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中
- import java.util.Arrays;
- import java.util.Scanner;
-
- public class test30 {
- public static void main(String args[]){
- Scanner sc=new Scanner(System.in);
- int num=sc.nextInt();
- int[] a=new int[]{1,4,23,67,99};//默认升序
- int[] b=new int[a.length+1];
- int t=0;
- for(int i=0;i<a.length;i++){
- if(a[i]<num){
- b[i]=a[i];
- }
- else{
- if (t == 0) {
- b[i]=num;
- t++; //只添加一次num
- }
- b[i+1]=a[i];
- }
- }
- System.out.printf(Arrays.toString(b));
- }
- }

31.将一个数组逆序输出。
- import java.util.Scanner;
-
- public class test31 {
- public static void main(String args[]){
- Scanner sc=new Scanner(System.in);
- int i=0;
- int[] a=new int[9999];
- do{
- a[i]=sc.nextInt();
- i++;
- }while(a[i-1]!=-1);
- for(int j=i-2;j>=0;j--){ //注意j=i-2,不是j=a.length-1,否则没有输入数字的地方会输出0
- System.out.printf(a[j]+" ");
- }
- }
- }

32.取一个整数 a从右端开始的 4~7位。
- import java.util.Scanner;
-
- public class test32 {
- public static void main(String args[]){
- Scanner sc=new Scanner(System.in);
- long n=sc.nextLong();
- String s=Long.toString(n);
- char[] ch=s.toCharArray();
- int i=ch.length;
- if(ch.length>=7){
- System.out.printf("截取从右端开始的4~7位数是:"+ch[i-7]+ch[i-6]+ch[i-5]+ch[i-4]);
- //System.out.print(ch[i-7]);
- //System.out.print(ch[i-6]);
- //System.out.print(ch[i-5]);
- //System.out.print(ch[i-4]);
- }
- else{
- System.out.println("请输入大于7位的正整数");
- }
- }
- }

33.打印出杨辉三角形(要求打印出 10行,如下图 )

- public class test33 {
- public static void main(String args[]){
- int[][] ch=new int[10][];//10行,列数未知
- for(int i=0;i<ch.length;i++){
- ch[i]=new int[i+1];//ch[i]代表第几行
- for(int j=0;j<ch[i].length;j++){
- if(j==0||j==ch[i].length-1){
- ch[i][j]=1;
- }
- else{
- ch[i][j]=ch[i-1][j-1]+ch[i-1][j];
- }
- }
- }
- for(int i=0;i<ch.length;i++){
- for(int j=0;j<ch[i].length;j++){
- System.out.print(ch[i][j]+"\t");//“\t”为“转义字符“,代表的是一个tab,也就是8个空格。
- } // \t是补全当前字符串长度到8的整数倍
- System.out.println();
- }
- }
- }

34.输入 3个数 a,b,c,按大小顺序输出
- import java.util.Scanner;
-
- public class test34 {
- public static void main(String args[]){
- Scanner sc=new Scanner(System.in);
- System.out.printf("输入三个数:");
- int a=sc.nextInt(),b=sc.nextInt(),c=sc.nextInt();
- if(a>b){//默认升序
- int t=b;
- b=a;
- a=t;
- }
- if(a>c){
- int t1=c;
- c=a;
- a=t1;
- }
- if(b>c){
- int t2=c;
- c=b;
- b=t2;
- }
- System.out.printf(a+" "+b+" "+c);
- }
- }

35.输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
- import java.util.Scanner;
-
- public class test35 {
- public static void main(String args[]){
- int index1=0,index2=0;
- int[] ch=new int[8];//数组长度为8
- Scanner sc=new Scanner(System.in);
- for(int i=0;i<8;i++){
- ch[i]=sc.nextInt();
- }
- int max=0,min=999;
- for(int i=0;i<ch.length;i++){
- if(ch[i]>max){
- max=ch[i];
- index1=i;
- }
- }
- for(int i=0;i<ch.length;i++){
- if(ch[i]<min){
- min=ch[i];
- index2=i;
- }
- }
- if(index1!=0){
- int t=ch[index1];
- ch[index1]=ch[0];
- ch[0]=t;
- }
- if(index2!=ch.length-1){
- int t2=ch[index2];
- ch[index2]=ch[7];
- ch[7]=t2;
- }
- for(int i=0;i<ch.length;i++){
- System.out.printf(ch[i]+" ");
- }
- }
- }

36.有 n个整数,使其前面各数顺序向后移 m个位置,最后 m个数变成最前面的 m个数
- import java.util.Scanner;
-
- public class test36 {
- public static void main(String args[]){
- Scanner sc=new Scanner(System.in);
- System.out.printf("n个数:");
- int n=sc.nextInt();
- int[] a=new int[n];
- System.out.printf("分别是:");
- for(int i=0;i<n;i++){
- a[i]=sc.nextInt();
- }
- System.out.printf("向后移m个位置:");
- int m=sc.nextInt();
- int[] b=new int[m];
- for(int i=0;i<m;i++){
- b[i]=a[n-m+i];
- }
- for(int i=n-1;i>=m;i--){
- a[i]=a[i-2];
- }
- for(int i=0;i<m;i++){
- a[i]=b[i];
- }
- for(int i=0;i<a.length;i++){
- System.out.printf(a[i]+" ");
- }
- }
- }

37.有 n个人围成一圈,顺序排号。从第一个人开始报数(从 1到 3报数),凡报到 3的人退出圈子,问最后留下的是原来第几号的那位
- import java.util.Scanner;
-
- public class test37 {
- public static void main(String args[]){
- Scanner sc=new Scanner(System.in);
- System.out.printf("有n个人围成圈:");
- int n=sc.nextInt();
- boolean[] a=new boolean[n];
- for(int i=0;i<n;i++){
- a[i]=true;
- }
- int renshu=n;
- int baoshu=0;
- int index=0;
- while(renshu>1){
- if(a[index]){
- baoshu++;
- if(baoshu==3){
- baoshu=0;
- renshu--;
- a[index]=false;
- }
- }
- index++;
- if(index==n){
- index=0;
- }
- }
- for(int i=0;i<n;i++){
- if(a[i]==true){
- System.out.printf("最后留下来的是原来的第"+(i+1)+"号\n");
- }
- }
- }
- }

38.写一个函数,求一个字符串的长度,在 main函数中输入字符串,并输出其长度。
- import java.util.Scanner;
-
- public class test38 {
- public static void main(String args[]){
- Scanner sc=new Scanner(System.in);
- String n=sc.nextLine();
- System.out.println(check(n));
- }
- public static int check(String n){
- return n.length();
- }
- }
39.编写一个函数,输入 n为偶数时,调用函数求 1/2+1/4+...+1/n,当输入 n为奇数时,调用函数 1/1+1/3+...+1/n(利用指针函数)
- import java.util.Scanner;
-
- public class test39 {
- public static void main(String args[]){
- Scanner sc=new Scanner(System.in);
- int n=sc.nextInt();
- System.out.println(check(n));
- }
- public static double check(int n){
- double ans=0;
- if(n%2==0){
- for(int i=2;i<n+2;i=i+2){
- ans=ans+1.0/i;
- }
- }
- else{
- for(int i=1;i<n+2;i=i+2){
- ans=ans+1.0/i;
- }
- }
- return ans;
- }
- }

40.字符串排序 。
- import java.util.Arrays;
-
- public class test40 {
- public static void main(String args[]){
- String[] str=new String[]{"zc","ABc","za","Abc","dcs"};
- Arrays.sort(str);
- for(int i=0;i<str.length;i++){
- System.out.printf(str[i]+" ");
- }
- }
- }
41.海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?
- public class test41 {
- public static void main(String args[]) {
- int sum=0;
- for(int i=1;;i++){
- sum=i;
- if((sum-1)%5==0){
- sum=(sum-1)/5*4;//先丢掉一个,然后分成五份,拿走一份还剩四份
- if((sum-1)%5==0){
- sum=(sum-1)/5*4;
- if((sum-1)%5==0){
- sum=(sum-1)/5*4;
- if((sum-1)%5==0){
- sum=(sum-1)/5*4;
- if((sum-1)%5==0){
- System.out.println(i);
- break;
- }
- }
- }
- }
- }
- }
- }
- }

42.809*??=800*??+9*??+1其中??代表的两位数,8*??的结果为两位数,9*??的结果为 3位数。求??代表的两位数,及 809*??后的结果。
- public class test42 {
- public static void main(String args[]){
- for(int i=10;i<100;i++){
- if(10<=8*i&&8*i<100){
- if(100<=9*i&&9*i<10000){
- if(809*i==800*i+9*i+1){
- System.out.printf("答案:"+i);
- }
- else{
- System.out.println("无解");
- }
- }
- }
- }
- }
- }

43.求 0—7所能组成的奇数个数。
//组成 1位数是 4个。
//组成 2位数是 7*4个。
//组成 3位数是 7*8*4个。
//组成 4位数是 7*8*8*4个。
//......
- public class test43 {
- public static void main(String args[]){
- long sum=32;
- System.out.printf("组成1位数的奇数:4个\n");
- System.out.printf("组成1位数的奇数:28个\n");
- for(int i=3;i<=8;i++){
- int n=7*((int)Math.pow(8,(i-2)))*4;
- sum=sum+n;
- System.out.printf("组成"+i+"位数的奇数:"+n+"个\n");
- }
- System.out.printf("和为:"+sum+"个\n");
- }
- }
44.一个偶数总能表示为两个素数之和。
- import java.util.Scanner;
-
- public class test44 {
- public static void main(String args[]){
- Scanner sc=new Scanner(System.in);
- int n=sc.nextInt();
- if(n%2==0){
- for(int i=2;i<n/2;i++){
- for(int j=i+1;j<n;j++){
- if((check(i))==1&&(check(j)==1)){
- if(i+j==n) {
- System.out.printf(n + "=" + i + "+" + j + "\n");
- }
- }
- }
- }
- }
- else{
- System.out.printf("请输入一个偶数\n");
- }
- }
- public static int check(int n){
- int flag=1;
- for(int i=2;i<=Math.sqrt(n);i++){
- if(n%i==0){
- flag=0;
- break;
- }
- }
- return flag;
- }
- }

45.判断一个素数能被几个 9整除
- import java.util.Scanner;
-
- public class test45 {//能被9整除不就不是素数了吗...
- public static void main(String args[]){
- Scanner sc=new Scanner(System.in);
- int n=sc.nextInt();
- if(check1(n)==1){//判断是不是素数
- System.out.printf(n+"能被"+check2(n)+"个9整除\n");
- }
- else{
- System.out.printf("请输入一个素数");
- }
- }
- public static int check1(int n){
- int flag=1;
- for(int i=2;i<=Math.sqrt(n);i++){
- if(n%i==0){
- flag=0;
- break;
- }
- }
- return flag;
- }
- public static int check2(int n){//能被几个9整除
- int ans=0;
- if(n%9==0){
- ans++;
- n=n/9;
- }
- else{
- return ans;
- }
- return ans;
- }
- }

46.两个字符串连接程序
- import java.util.Scanner;
-
- public class test46 {
- public static void main(String args[]){
- Scanner sc=new Scanner(System.in);
- System.out.printf("请输入两个字符串:\n字符串1:");
- String str1=sc.nextLine();
- System.out.printf("字符串2:");
- String str2=sc.nextLine();
- System.out.printf("连接这两个字符串:"+str1+str2);
- }
- }
47.读取 7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。
- import java.util.Scanner;
-
- public class test47 {
- public static void main(String args[]){
- System.out.printf("请输入7个1~50内的整数:\n");
- Scanner sc=new Scanner(System.in);
- int i=1,n;
- while(i<=7){
- do{
- System.out.printf("第"+i+"个数:");
- n=sc.nextInt();
- }while(1>n||n>50);
- for (int j = 1; j <= n; j++) {
- System.out.printf("*");
- }
- i++;
- System.out.println();
- }
- }
- }

48.某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上 5,然后用和除以 10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。
- import java.util.Scanner;
-
- public class test48 {
- public static void main(String args[]){
- System.out.printf("请输入一个四位数:");
- Scanner sc=new Scanner(System.in);
- int[] a=new int[4];
- int n=sc.nextInt();
- if(1000<=n&&n<10000) {
- int ge=n/1%10,shi=n/10%10,bai=n/100%10,qian=n/1000%10;
- a[0]=qian;
- a[1]=bai;
- a[2]=shi;
- a[3]=ge;
- for (int i = 0; i < a.length; i++) {
- a[i]=(a[i]+5)%10;
- }
- int t1=a[0];
- a[0]=a[3];
- a[3]=t1;
- int t2=a[1];
- a[1]=a[2];
- a[2]=t2;
- for (int i = 0; i < a.length; i++) {
- System.out.printf(a[i] + " ");
- }
- }
- else{
- System.out.printf("您输入的数不是四位数!");
- }
- }
- }

49.计算字符串中某个子串出现的次数
- import java.util.Scanner;
-
- public class test49 {
- public static void main(String args[]){
- int num=0;
- System.out.printf("输入一个字符串:");
- Scanner sc=new Scanner(System.in);
- String str1=sc.nextLine();
- System.out.printf("输入该字符串的子串:");
- String str2=sc.nextLine();
- if(str1.equals("")||str2.equals("")){
- System.out.printf("你没有输入字符串或子串!");
- }
- else{
- for (int i = 0; i <= str1.length()-str2.length(); i++) {
- if (str1.substring(i, i+str2.length()).equals(str2)) {
- num++;
- }
- }
- System.out.printf("子串"+str2+"在字符串"+str1+"中出现了"+num+"次");
- }
- }
- }

50.有五个学生,每个学生有 3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,把原有的数据和计算出的平均分数存放在磁盘文件 "stud"中。
- import java.io.*;
- import java.util.*;
-
- public class test50 {
- public static void main(String args[]){
- String s;
- int sum=0;
- float avg=0;
- Scanner sc=new Scanner(System.in);
- String[][] str=new String[5][6];
- for(int i=0;i<str.length;i++){
- System.out.printf("第"+(i+1)+"个学生学号:");
- str[i][0]=sc.nextLine();
- System.out.printf("第"+(i+1)+"个学生姓名:");
- str[i][1]=sc.nextLine();
- for(int j=2;j<5;j++){
- System.out.printf("第"+(j-1)+"个成绩:");
- str[i][j]=sc.nextLine();
- }
- System.out.println();
- }
- //计算平均数
- for(int i=0;i<str.length;i++){
- sum=0;
- for(int j=2;j<5;j++){
- sum=sum+Integer.parseInt(str[i][j]);
- }
- avg=(float)(sum)/3;
- str[i][5]=String.valueOf(avg);
- System.out.printf("%.2f",avg);
- }
- try{
- File file=new File("D://java//untitled3//src//file.txt");
- if(file.exists()){
- System.out.printf("这个文件已存在!");
- }
- else{
- System.out.printf("文件创建成功!");
- file.createNewFile();
- }
- BufferedWriter out=new BufferedWriter(new FileWriter(file));
- for(int i=0;i<str.length;i++){
- for(int j=0;j<6;j++){
- s=str[i][j];
- out.write(s+"\r\n");
- }
- }
- out.close();
- System.out.printf("数据已存入"+file.getPath());
- }
- catch(Exception e){
- System.out.println(e.getMessage());
- }
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。