当前位置:   article > 正文

蓝桥杯java组基础练习题50题_蓝桥杯java软件开发题目

蓝桥杯java软件开发题目

1.古典问题:有一对兔子,从出生后第 3 个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

//这是一个菲波拉契数列问题 

  1. public class test1 {
  2. public static void main(String args[]){
  3. int f1=2,f2=2,temp;
  4. System.out.printf("第1个月有2只兔子\n");
  5. System.out.printf("第2个月有2只兔子\n");
  6. for(int i=3;i<=24;i++){
  7. temp=f2;
  8. f2=f1+f2;
  9. f1=temp;
  10. System.out.printf("第%d个月有%d只兔子\n",i,f2);
  11. }
  12. }
  13. }

2.判断101-200 之间有多少个素数,并输出所有素数。

  1. public class test2 {
  2. public static void main(String args[]){
  3. int k=0;
  4. for(int i=101;i<=200;i++){
  5. if(check(i)==1){
  6. System.out.println(i);
  7. k++;
  8. }
  9. }
  10. System.out.printf("有%d个素数",k);
  11. }
  12. public static int check(int n){
  13. int flag=0;
  14. for(int i=2;i<n;i++){
  15. if(n%i==0){
  16. flag=0;
  17. break;
  18. }
  19. else{
  20. flag=1;
  21. }
  22. }
  23. return flag;
  24. }
  25. }

3.打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。例如:153 是一个 "水仙花数 ",因为 153=1 的三次方+5 的三次方+3 的三次方。

  1. public class test3 {
  2. public static void main(String args[]){
  3. for(int i=100;i<1000;i++){
  4. if(Math.pow((i/1%10),3)+Math.pow((i/100%10),3)+Math.pow((i/10%10),3)==i){
  5. System.out.println(i);
  6. }
  7. }
  8. }
  9. }

4.将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。

  1. public class test4 {
  2. public static void main(String args[]){
  3. Scanner sc=new Scanner(System.in);
  4. int n= sc.nextInt();
  5. System.out.printf("%d=",n);
  6. int k=2;
  7. while(k<=n){
  8. if(k==n){
  9. System.out.println(n);
  10. break;
  11. }
  12. else if(n%k==0){
  13. System.out.printf(k+"*");
  14. n=n/k;
  15. }
  16. else{
  17. k++;
  18. }
  19. }
  20. }
  21. }

5.利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B 表示,60 分以下的用C 表示。

  1. public class test5 {
  2. public static void main(String args[]){
  3. Scanner sc=new Scanner(System.in);
  4. char grade;
  5. int x=sc.nextInt();
  6. grade=x>=90?'A':x>=60?'B':'C';
  7. System.out.println("等级为:"+grade);
  8. }
  9. }

6.输入两个正整数m 和n,求其最大公约数和最小公倍数。

  1. public class test6 {
  2. public static void main(String args[]){
  3. Scanner sc=new Scanner(System.in);
  4. int a=sc.nextInt();
  5. int b=sc.nextInt();
  6. int max,n=a*b;
  7. int temp;
  8. if(a<b){ //把a当做大数
  9. temp=a;
  10. a=b;
  11. b=temp;
  12. }
  13. while(b!=0){
  14. if(a==b){
  15. max=a;
  16. }
  17. else{
  18. int k=a%b;
  19. a=b;
  20. b=k;
  21. }
  22. }
  23. max=a;
  24. int min=n/max;
  25. System.out.printf("最大公约数:%d,最小公倍数:%d",max,min);
  26. }
  27. }

7.输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

  1. public class test7 {
  2. public static void main(String args[]){
  3. Scanner sc=new Scanner(System.in);
  4. int shuzi=0,zimu=0,kongge=0,qita=0;
  5. char[] c=null;
  6. String s=sc.nextLine();
  7. c=s.toCharArray();
  8. for(int i=0;i<c.length;i++){
  9. if('0'<=c[i]&&c[i]<='9'){
  10. shuzi++;
  11. }
  12. else if(('a'<=c[i]&&c[i]<='z')||('A'<=c[i]&&c[i]<='Z')){
  13. zimu++;
  14. }
  15. else if(c[i]==' '){
  16. kongge++;
  17. }
  18. else{
  19. qita++;
  20. }
  21. }
  22. System.out.printf("数字:%d,字母:%d,空格:%d,其他:%d",shuzi,zimu,kongge,qita);
  23. }
  24. }

8.求s=a+aa+aaa+aaaa+aa...a 的值,其中a 是一个数字。例如2+22+222+2222+22222(此时共有5 个数相加),几个数相加有键盘控制。

  1. public class test8 {
  2. public static void main(String srgs[]){
  3. Scanner sc=new Scanner(System.in);
  4. System.out.printf("这个数是:");
  5. int a=sc.nextInt();
  6. System.out.printf("几个这个数相加:");
  7. int n=sc.nextInt();
  8. int sum=0,b=0;
  9. for(int i=1;i<=n;i++){
  10. b=b+a;
  11. sum=sum+b;
  12. a=a*10;
  13. System.out.println(b);
  14. }
  15. System.out.println(sum);
  16. }
  17. }

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

  1. public class test9 {
  2. public static void main(String args[]) {
  3. for(int i=1;i<1000;i++){
  4. int sum=0;
  5. for(int j=1;j<i;j++){
  6. if(i%j==0){
  7. sum=sum+j;
  8. }
  9. }
  10. if(sum==i){
  11. System.out.println(i+" ");
  12. }
  13. }
  14. }
  15. }

10.一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10 次落地时,共经过多少米?第10 次反弹多高?

  1. public class test10 {
  2. public static void main(String args[]){
  3. double a=100,c=100;
  4. for(int i=1;i<=10;i++){
  5. c=c+a;
  6. a=a/2;
  7. }
  8. System.out.printf("共经过"+c+"米\n");
  9. System.out.printf("第10次反弹"+a+"米");
  10. }
  11. }

11.有1、2、3、4四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

  1. public class test11 {
  2. public static void main(String args[]){
  3. int count=0;
  4. for(int i=1;i<5;i++){
  5. for(int j=1;j<5;j++){
  6. for(int k=1;k<5;k++){
  7. if(i!=j&&i!=k&&j!=k){
  8. System.out.println(i*100+j*10+k);
  9. count++;
  10. }
  11. }
  12. }
  13. }
  14. System.out.println("共有"+count+"个数");
  15. }
  16. }

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%提成,从键盘输入当月利润,求应发放奖金总数?

  1. public class test12 {
  2. public static void main(String args[]){
  3. double jiangjin=0;
  4. Scanner sc=new Scanner(System.in);
  5. double x=sc.nextDouble();
  6. if(10<=x){
  7. jiangjin=x*0.1;
  8. }
  9. if(10<x&&x<=20){
  10. jiangjin=10*0.1+(x-10)*0.075;
  11. }
  12. if(20<x&&x<=40){
  13. jiangjin=10*0.1+10*0.075+(x-20)*0.05;
  14. }
  15. if(40<x&&x<=60){
  16. jiangjin=10*0.1+10*0.075+20*0.05+(x-40)*0.03;
  17. }
  18. if(60<x&&x<=100){
  19. jiangjin=10*0.1+10*0.075+20*0.05+20*0.03+(x-60)*0.015;
  20. }
  21. if(100<x){
  22. jiangjin=10*0.1+10*0.075+20*0.05+20*0.03+40*0.015+(x-100)*0.01;
  23. }
  24. System.out.print(jiangjin);
  25. }
  26. }

13.一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平 方数,请问该数是多少?

//注意负数也是整数,循环从负数开始

  1. public class test13 {
  2. public static void main(String args[]){
  3. for(int i=-100;i<100000;i++){
  4. if(check(i+100)==1){
  5. if(check(i+268)==1) {
  6. System.out.println(i);
  7. }
  8. }
  9. }
  10. }
  11. public static int check(int n){
  12. int flag=0;
  13. if(Math.sqrt(n)%1==0){
  14. flag=1;
  15. }
  16. return flag;
  17. }
  18. }

14.输入某年某月某日,判断这一天是这一年的第几天? 

  1. public class test14 {
  2. public static void main(String args[]){
  3. Scanner sc=new Scanner(System.in);
  4. int year=sc.nextInt();
  5. int month=sc.nextInt();
  6. int day=sc.nextInt();
  7. int sum=0,sum1;
  8. if (year % 400 == 0 ||(year%4==0&&year%100==0)) {
  9. int[] ch29=new int[]{31,29,31,30,31,30,31,31,30,31,30,31};
  10. for(int i=0;i<month-1;i++){
  11. sum=sum+ch29[i];
  12. }
  13. sum1=sum+day;
  14. System.out.println(sum1);
  15. }
  16. else{
  17. int[] ch28=new int[]{31,28,31,30,31,30,31,31,30,31,30,31};
  18. for(int i=0;i<month-1;i++){
  19. sum=sum+ch28[i];
  20. }
  21. sum1=sum+day;
  22. System.out.println(sum1);
  23. }
  24. }
  25. }

15.x、y、z三个数排列大小

  1. public class test15 {
  2. public static void main(String args[]){
  3. int temp;
  4. Scanner sc=new Scanner(System.in);
  5. int x=sc.nextInt();
  6. int y=sc.nextInt();
  7. int z=sc.nextInt();
  8. if(x>y){
  9. temp=y;
  10. y=x;
  11. x=temp;
  12. }
  13. if(x>z){
  14. temp=z;
  15. z=x;
  16. x=temp;
  17. }
  18. if(y>z){
  19. temp=z;
  20. z=y;
  21. y=temp;
  22. }
  23. System.out.printf("%d ",x);
  24. System.out.printf("%d ",y);
  25. System.out.printf("%d",z);
  26. }
  27. }

16.打印九九乘法表

  1. public class test16 {
  2. public static void main(String args[]){
  3. for(int i=1;i<10;i++){
  4. System.out.println("\n");
  5. for(int j=1;j<=i;j++){
  6. System.out.printf(j+"*"+i+"="+i*j+" ");
  7. }
  8. }
  9. }
  10. }

17.猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第 10 天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。

//逆向思维,(后一天的桃子数+1)*2  得到前一天剩下的桃子数

  1. public class test17 {
  2. public static void main(String args[]){
  3. int x=1;
  4. for(int i=9;i>=1;i--){
  5. x=(x+1)*2; /*第一天的桃子数是第二天桃子数加1后的2倍*/
  6. }
  7. System.out.println(x);
  8. }
  9. }

18.两个乒乓球队进行比赛,各出三人。甲队为 a,b,c 三人,乙队为 x,y,z 三人。已抽签定比赛名单。有人向队员打听比赛的名单。a 说他不和 x 比,c 说他不和 x,z 比,请编程序找出三队赛手的名单。

  1. public class test18 {
  2. public static void main(String args[]){
  3. char[] ch1=new char[]{'a','b','c'};
  4. char[] ch2=new char[]{'x','y','z'};
  5. for(int i=0;i<ch1.length;i++){
  6. for(int j=0;j<ch2.length;j++){
  7. if(ch1[i]=='a'&&ch2[j]=='x'||ch1[i]=='a'&&ch2[j]=='y'){
  8. continue;
  9. }
  10. else if(ch1[i]=='b'&&ch2[j]=='y'||ch1[i]=='b'&&ch2[j]=='z'){
  11. continue;
  12. }
  13. else if(ch1[i]=='c'&&ch2[j]=='x'||ch1[i]=='c'&&ch2[j]=='z'){
  14. continue;
  15. }
  16. else{
  17. System.out.printf(ch1[i]+" vs "+ch2[j]+"\n");
  18. }
  19. }
  20. }
  21. }
  22. }

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

  1. public class test19 {
  2. public static void main(String args[]){
  3. int gao=7,kuan=7;//相同的奇数
  4. for(int i=1;i<=(gao+1)/2;i++){//包括最宽行以上面组成的三角形
  5. for(int j=0;j<=kuan/2-i;j++){
  6. System.out.printf(" ");
  7. }
  8. for(int k=0;k<i*2-1;k++){
  9. System.out.printf("*");
  10. }
  11. System.out.println();
  12. }
  13. for(int i=1;i<=gao/2;i++){//不包括最宽行以下面组成的三角形
  14. for(int j=0;j<i;j++){
  15. System.out.printf(" ");
  16. }
  17. for(int k=0;k<kuan-i*2;k++){
  18. System.out.printf("*");
  19. }
  20. System.out.println();
  21. }
  22. }
  23. }

20.有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前 20 项之和。

  1. public class test20 {
  2. public static void main(String args[]){
  3. double sum=0,fenzi=2,fenmu=1,t;
  4. for(int i=1;i<=20;i++){
  5. sum=sum+fenzi/fenmu;
  6. t=fenmu;
  7. fenmu=fenzi;
  8. fenzi=fenzi+t;
  9. }
  10. System.out.println(sum);
  11. }
  12. }

21.求1+2!+3!+...+20!的和

  1. public class test21 {
  2. public static void main(String args[]){
  3. long sum=0;
  4. for(int i=1;i<=20;i++){
  5. sum=check(i);
  6. }
  7. System.out.println(sum);
  8. }
  9. public static long check(int n){
  10. long a=1,sum=0;
  11. if(n==0){
  12. sum=1;
  13. }
  14. else {
  15. for (int i = 1; i <= n; i++) {
  16. a = a * i;
  17. sum = sum + a;
  18. }
  19. }
  20. return sum;
  21. }
  22. }

22.利用递归方法求 5! 

  1. public class test22 {
  2. public static void main(String args[]){
  3. System.out.println(check(5));
  4. }
  5. public static int check(int n){
  6. if(n==1){
  7. n=1;
  8. }
  9. else{
  10. n=n*(check(n-1));
  11. }
  12. return n;
  13. }
  14. }

23.有 5 个人坐在一起,问第五个人多少岁?他说比第 4 个人大 2 岁。问第 4 个人岁数,他说比第 3 个人大 2 岁。问第三个人,又说比第 2 人大两岁。问第 2 个人,说比第一个人大两岁。最后问第一个人,他说是 10 岁。请问第五个人多大?

  1. public class test23 {
  2. public static void main(String args[]){
  3. int a=10;
  4. for(int i=2;i<=5;i++){
  5. a=a+2;
  6. }
  7. System.out.println(a);
  8. }
  9. }

24.给一个不多于 5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。

  1. import java.util.Scanner;
  2. public class test24 {
  3. public static void main(String args[]){
  4. Scanner sc=new Scanner(System.in);
  5. long n=sc.nextLong();
  6. String s=Long.toString(n);
  7. char[] ch=s.toCharArray();
  8. if(ch.length<5&&n%1==0) {
  9. System.out.println("这个数是"+ch.length+"位数");
  10. for (int i = ch.length - 1; i >= 0; i--) {//注意i=ch.length-1,要用长度-1,否则会数组越界
  11. System.out.print(ch[i]);
  12. }
  13. }
  14. else{
  15. System.out.printf("请输入不多于5位的正整数");
  16. }
  17. }
  18. }

25.一个 5位数,判断它是不是回文数。即 12321是回文数,个位与万位相同,十位与千位相同。

  1. import java.util.Scanner;
  2. public class test25 {
  3. public static void main(String args[]){
  4. Scanner sc=new Scanner(System.in);
  5. int n=sc.nextInt();
  6. if(n>10000&&n<100000){
  7. if(n/1%10==n/10000%10&&n/10%10==n/1000%10){
  8. System.out.printf(n+"是回文数");
  9. }
  10. }
  11. else{
  12. System.out.printf("请输入一个5位正整数");
  13. }
  14. }
  15. }

26.请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母。

  1. import java.util.Scanner;
  2. public class test26 {
  3. public static void main(String args[]){
  4. System.out.printf("请输入第一个字母:");//默认第一个字母大写第二个字母小写
  5. Scanner sc=new Scanner(System.in);
  6. char n=sc.next().charAt(0);//charAt() 方法用于返回指定索引处的字符。索引范围为从 0 到 length() - 1。
  7. if(n=='M'){
  8. System.out.println("Monday");
  9. }
  10. else if(n=='W'){
  11. System.out.println("Wednesday");
  12. }
  13. else if(n=='F'){
  14. System.out.println("Friday");
  15. }
  16. else if(n=='T'){
  17. System.out.printf("请输入第二个字母:");
  18. Scanner sc1=new Scanner(System.in);
  19. char n1=sc1.next().charAt(0);
  20. if(n1=='u'){
  21. System.out.println("Tuesday");
  22. }
  23. if(n1=='h'){
  24. System.out.println("Thursday");
  25. }
  26. }
  27. else if(n=='S'){
  28. System.out.printf("请输入第二个字母:");
  29. Scanner sc1=new Scanner(System.in);
  30. char n1=sc1.next().charAt(0);
  31. if(n1=='a'){
  32. System.out.println("Saturday");
  33. }
  34. if(n1=='u'){
  35. System.out.println("Sunday");
  36. }
  37. }
  38. else{
  39. System.out.println("无此写法");
  40. }
  41. }
  42. }

27.求 100之内的素数 

  1. public class test27 {
  2. public static void main(String args[]){
  3. for(int i=2;i<100;i++){
  4. if(check(i)==1){
  5. System.out.printf(i+" ");
  6. }
  7. }
  8. }
  9. public static int check(int n){
  10. int flag=1;
  11. for(int i=2;i<=Math.sqrt(n);i++){
  12. if(n%i==0){
  13. flag=0;
  14. break;
  15. }
  16. }
  17. return flag;
  18. }
  19. }

28.对 10个数进行排序

  1. import java.util.Scanner;
  2. public class test28 {
  3. public static void main(String args[]){//默认升序(从小到大)
  4. Scanner sc=new Scanner(System.in);
  5. int[] a=new int[10];
  6. for(int i=0;i<10;i++){
  7. a[i]=sc.nextInt();
  8. }
  9. for(int i=0;i<a.length-1;i++){
  10. for(int j=0;j<a.length-1;j++){
  11. if(a[j]>a[j+1]){//把>改成<就是降序(从大到小)
  12. int t=a[j+1];
  13. a[j+1]=a[j];
  14. a[j]=t;
  15. }
  16. }
  17. }
  18. for(int k=0;k<=a.length-1;k++){
  19. System.out.printf(a[k]+" ");
  20. }
  21. }
  22. }

29.求一个 3*3矩阵对角线元素之和

//主副对角线的元素我都相加了

  1. import java.util.Scanner;
  2. public class test29 {
  3. public static void main(String args[]){
  4. int sum=0;
  5. Scanner sc=new Scanner(System.in);
  6. int[][] a=new int[3][3];
  7. for(int i=0;i<3;i++){
  8. for(int j=0;j<3;j++){
  9. a[i][j]=sc.nextInt();
  10. System.out.printf("a["+i+"]["+j+"]="+a[i][j]+"\n");
  11. }
  12. }
  13. for(int i=0;i<3;i++){
  14. for(int j=0;j<3;j++){
  15. System.out.printf(a[i][j]+" ");
  16. }
  17. System.out.println();
  18. }
  19. for(int i=0;i<3;i++){
  20. for(int j=0;j<3;j++){
  21. if(i==j||i+j==2) {
  22. sum=sum+a[i][j];
  23. }
  24. }
  25. }
  26. System.out.printf("对角线元素之和:"+sum);
  27. }
  28. }

30.有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中 

  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3. public class test30 {
  4. public static void main(String args[]){
  5. Scanner sc=new Scanner(System.in);
  6. int num=sc.nextInt();
  7. int[] a=new int[]{1,4,23,67,99};//默认升序
  8. int[] b=new int[a.length+1];
  9. int t=0;
  10. for(int i=0;i<a.length;i++){
  11. if(a[i]<num){
  12. b[i]=a[i];
  13. }
  14. else{
  15. if (t == 0) {
  16. b[i]=num;
  17. t++; //只添加一次num
  18. }
  19. b[i+1]=a[i];
  20. }
  21. }
  22. System.out.printf(Arrays.toString(b));
  23. }
  24. }

31.将一个数组逆序输出。

  1. import java.util.Scanner;
  2. public class test31 {
  3. public static void main(String args[]){
  4. Scanner sc=new Scanner(System.in);
  5. int i=0;
  6. int[] a=new int[9999];
  7. do{
  8. a[i]=sc.nextInt();
  9. i++;
  10. }while(a[i-1]!=-1);
  11. for(int j=i-2;j>=0;j--){ //注意j=i-2,不是j=a.length-1,否则没有输入数字的地方会输出0
  12. System.out.printf(a[j]+" ");
  13. }
  14. }
  15. }

32.取一个整数 a从右端开始的 4~7位。

  1. import java.util.Scanner;
  2. public class test32 {
  3. public static void main(String args[]){
  4. Scanner sc=new Scanner(System.in);
  5. long n=sc.nextLong();
  6. String s=Long.toString(n);
  7. char[] ch=s.toCharArray();
  8. int i=ch.length;
  9. if(ch.length>=7){
  10. System.out.printf("截取从右端开始的4~7位数是:"+ch[i-7]+ch[i-6]+ch[i-5]+ch[i-4]);
  11. //System.out.print(ch[i-7]);
  12. //System.out.print(ch[i-6]);
  13. //System.out.print(ch[i-5]);
  14. //System.out.print(ch[i-4]);
  15. }
  16. else{
  17. System.out.println("请输入大于7位的正整数");
  18. }
  19. }
  20. }

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

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

34.输入 3个数 a,b,c,按大小顺序输出 

  1. import java.util.Scanner;
  2. public class test34 {
  3. public static void main(String args[]){
  4. Scanner sc=new Scanner(System.in);
  5. System.out.printf("输入三个数:");
  6. int a=sc.nextInt(),b=sc.nextInt(),c=sc.nextInt();
  7. if(a>b){//默认升序
  8. int t=b;
  9. b=a;
  10. a=t;
  11. }
  12. if(a>c){
  13. int t1=c;
  14. c=a;
  15. a=t1;
  16. }
  17. if(b>c){
  18. int t2=c;
  19. c=b;
  20. b=t2;
  21. }
  22. System.out.printf(a+" "+b+" "+c);
  23. }
  24. }

35.输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。 

  1. import java.util.Scanner;
  2. public class test35 {
  3. public static void main(String args[]){
  4. int index1=0,index2=0;
  5. int[] ch=new int[8];//数组长度为8
  6. Scanner sc=new Scanner(System.in);
  7. for(int i=0;i<8;i++){
  8. ch[i]=sc.nextInt();
  9. }
  10. int max=0,min=999;
  11. for(int i=0;i<ch.length;i++){
  12. if(ch[i]>max){
  13. max=ch[i];
  14. index1=i;
  15. }
  16. }
  17. for(int i=0;i<ch.length;i++){
  18. if(ch[i]<min){
  19. min=ch[i];
  20. index2=i;
  21. }
  22. }
  23. if(index1!=0){
  24. int t=ch[index1];
  25. ch[index1]=ch[0];
  26. ch[0]=t;
  27. }
  28. if(index2!=ch.length-1){
  29. int t2=ch[index2];
  30. ch[index2]=ch[7];
  31. ch[7]=t2;
  32. }
  33. for(int i=0;i<ch.length;i++){
  34. System.out.printf(ch[i]+" ");
  35. }
  36. }
  37. }

36.有 n个整数,使其前面各数顺序向后移 m个位置,最后 m个数变成最前面的 m个数

  1. import java.util.Scanner;
  2. public class test36 {
  3. public static void main(String args[]){
  4. Scanner sc=new Scanner(System.in);
  5. System.out.printf("n个数:");
  6. int n=sc.nextInt();
  7. int[] a=new int[n];
  8. System.out.printf("分别是:");
  9. for(int i=0;i<n;i++){
  10. a[i]=sc.nextInt();
  11. }
  12. System.out.printf("向后移m个位置:");
  13. int m=sc.nextInt();
  14. int[] b=new int[m];
  15. for(int i=0;i<m;i++){
  16. b[i]=a[n-m+i];
  17. }
  18. for(int i=n-1;i>=m;i--){
  19. a[i]=a[i-2];
  20. }
  21. for(int i=0;i<m;i++){
  22. a[i]=b[i];
  23. }
  24. for(int i=0;i<a.length;i++){
  25. System.out.printf(a[i]+" ");
  26. }
  27. }
  28. }

37.有 n个人围成一圈,顺序排号。从第一个人开始报数(从 1到 3报数),凡报到 3的人退出圈子,问最后留下的是原来第几号的那位 

  1. import java.util.Scanner;
  2. public class test37 {
  3. public static void main(String args[]){
  4. Scanner sc=new Scanner(System.in);
  5. System.out.printf("有n个人围成圈:");
  6. int n=sc.nextInt();
  7. boolean[] a=new boolean[n];
  8. for(int i=0;i<n;i++){
  9. a[i]=true;
  10. }
  11. int renshu=n;
  12. int baoshu=0;
  13. int index=0;
  14. while(renshu>1){
  15. if(a[index]){
  16. baoshu++;
  17. if(baoshu==3){
  18. baoshu=0;
  19. renshu--;
  20. a[index]=false;
  21. }
  22. }
  23. index++;
  24. if(index==n){
  25. index=0;
  26. }
  27. }
  28. for(int i=0;i<n;i++){
  29. if(a[i]==true){
  30. System.out.printf("最后留下来的是原来的第"+(i+1)+"号\n");
  31. }
  32. }
  33. }
  34. }

38.写一个函数,求一个字符串的长度,在 main函数中输入字符串,并输出其长度。

  1. import java.util.Scanner;
  2. public class test38 {
  3. public static void main(String args[]){
  4. Scanner sc=new Scanner(System.in);
  5. String n=sc.nextLine();
  6. System.out.println(check(n));
  7. }
  8. public static int check(String n){
  9. return n.length();
  10. }
  11. }

39.编写一个函数,输入 n为偶数时,调用函数求 1/2+1/4+...+1/n,当输入 n为奇数时,调用函数 1/1+1/3+...+1/n(利用指针函数)

  1. import java.util.Scanner;
  2. public class test39 {
  3. public static void main(String args[]){
  4. Scanner sc=new Scanner(System.in);
  5. int n=sc.nextInt();
  6. System.out.println(check(n));
  7. }
  8. public static double check(int n){
  9. double ans=0;
  10. if(n%2==0){
  11. for(int i=2;i<n+2;i=i+2){
  12. ans=ans+1.0/i;
  13. }
  14. }
  15. else{
  16. for(int i=1;i<n+2;i=i+2){
  17. ans=ans+1.0/i;
  18. }
  19. }
  20. return ans;
  21. }
  22. }

40.字符串排序 。

  1. import java.util.Arrays;
  2. public class test40 {
  3. public static void main(String args[]){
  4. String[] str=new String[]{"zc","ABc","za","Abc","dcs"};
  5. Arrays.sort(str);
  6. for(int i=0;i<str.length;i++){
  7. System.out.printf(str[i]+" ");
  8. }
  9. }
  10. }

41.海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?

  1. public class test41 {
  2. public static void main(String args[]) {
  3. int sum=0;
  4. for(int i=1;;i++){
  5. sum=i;
  6. if((sum-1)%5==0){
  7. sum=(sum-1)/5*4;//先丢掉一个,然后分成五份,拿走一份还剩四份
  8. if((sum-1)%5==0){
  9. sum=(sum-1)/5*4;
  10. if((sum-1)%5==0){
  11. sum=(sum-1)/5*4;
  12. if((sum-1)%5==0){
  13. sum=(sum-1)/5*4;
  14. if((sum-1)%5==0){
  15. System.out.println(i);
  16. break;
  17. }
  18. }
  19. }
  20. }
  21. }
  22. }
  23. }
  24. }

42.809*??=800*??+9*??+1其中??代表的两位数,8*??的结果为两位数,9*??的结果为 3位数。求??代表的两位数,及 809*??后的结果。

  1. public class test42 {
  2. public static void main(String args[]){
  3. for(int i=10;i<100;i++){
  4. if(10<=8*i&&8*i<100){
  5. if(100<=9*i&&9*i<10000){
  6. if(809*i==800*i+9*i+1){
  7. System.out.printf("答案:"+i);
  8. }
  9. else{
  10. System.out.println("无解");
  11. }
  12. }
  13. }
  14. }
  15. }
  16. }

43.求 0—7所能组成的奇数个数。

//组成 1位数是 4个。

//组成 2位数是 7*4个。

//组成 3位数是 7*8*4个。

//组成 4位数是 7*8*8*4个。

//...... 

  1. public class test43 {
  2. public static void main(String args[]){
  3. long sum=32;
  4. System.out.printf("组成1位数的奇数:4个\n");
  5. System.out.printf("组成1位数的奇数:28个\n");
  6. for(int i=3;i<=8;i++){
  7. int n=7*((int)Math.pow(8,(i-2)))*4;
  8. sum=sum+n;
  9. System.out.printf("组成"+i+"位数的奇数:"+n+"个\n");
  10. }
  11. System.out.printf("和为:"+sum+"个\n");
  12. }
  13. }

44.一个偶数总能表示为两个素数之和。

  1. import java.util.Scanner;
  2. public class test44 {
  3. public static void main(String args[]){
  4. Scanner sc=new Scanner(System.in);
  5. int n=sc.nextInt();
  6. if(n%2==0){
  7. for(int i=2;i<n/2;i++){
  8. for(int j=i+1;j<n;j++){
  9. if((check(i))==1&&(check(j)==1)){
  10. if(i+j==n) {
  11. System.out.printf(n + "=" + i + "+" + j + "\n");
  12. }
  13. }
  14. }
  15. }
  16. }
  17. else{
  18. System.out.printf("请输入一个偶数\n");
  19. }
  20. }
  21. public static int check(int n){
  22. int flag=1;
  23. for(int i=2;i<=Math.sqrt(n);i++){
  24. if(n%i==0){
  25. flag=0;
  26. break;
  27. }
  28. }
  29. return flag;
  30. }
  31. }

45.判断一个素数能被几个 9整除 

  1. import java.util.Scanner;
  2. public class test45 {//能被9整除不就不是素数了吗...
  3. public static void main(String args[]){
  4. Scanner sc=new Scanner(System.in);
  5. int n=sc.nextInt();
  6. if(check1(n)==1){//判断是不是素数
  7. System.out.printf(n+"能被"+check2(n)+"个9整除\n");
  8. }
  9. else{
  10. System.out.printf("请输入一个素数");
  11. }
  12. }
  13. public static int check1(int n){
  14. int flag=1;
  15. for(int i=2;i<=Math.sqrt(n);i++){
  16. if(n%i==0){
  17. flag=0;
  18. break;
  19. }
  20. }
  21. return flag;
  22. }
  23. public static int check2(int n){//能被几个9整除
  24. int ans=0;
  25. if(n%9==0){
  26. ans++;
  27. n=n/9;
  28. }
  29. else{
  30. return ans;
  31. }
  32. return ans;
  33. }
  34. }

46.两个字符串连接程序 

  1. import java.util.Scanner;
  2. public class test46 {
  3. public static void main(String args[]){
  4. Scanner sc=new Scanner(System.in);
  5. System.out.printf("请输入两个字符串:\n字符串1:");
  6. String str1=sc.nextLine();
  7. System.out.printf("字符串2:");
  8. String str2=sc.nextLine();
  9. System.out.printf("连接这两个字符串:"+str1+str2);
  10. }
  11. }

47.读取 7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。 

  1. import java.util.Scanner;
  2. public class test47 {
  3. public static void main(String args[]){
  4. System.out.printf("请输入7个1~50内的整数:\n");
  5. Scanner sc=new Scanner(System.in);
  6. int i=1,n;
  7. while(i<=7){
  8. do{
  9. System.out.printf("第"+i+"个数:");
  10. n=sc.nextInt();
  11. }while(1>n||n>50);
  12. for (int j = 1; j <= n; j++) {
  13. System.out.printf("*");
  14. }
  15. i++;
  16. System.out.println();
  17. }
  18. }
  19. }

48.某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上 5,然后用和除以 10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。

  1. import java.util.Scanner;
  2. public class test48 {
  3. public static void main(String args[]){
  4. System.out.printf("请输入一个四位数:");
  5. Scanner sc=new Scanner(System.in);
  6. int[] a=new int[4];
  7. int n=sc.nextInt();
  8. if(1000<=n&&n<10000) {
  9. int ge=n/1%10,shi=n/10%10,bai=n/100%10,qian=n/1000%10;
  10. a[0]=qian;
  11. a[1]=bai;
  12. a[2]=shi;
  13. a[3]=ge;
  14. for (int i = 0; i < a.length; i++) {
  15. a[i]=(a[i]+5)%10;
  16. }
  17. int t1=a[0];
  18. a[0]=a[3];
  19. a[3]=t1;
  20. int t2=a[1];
  21. a[1]=a[2];
  22. a[2]=t2;
  23. for (int i = 0; i < a.length; i++) {
  24. System.out.printf(a[i] + " ");
  25. }
  26. }
  27. else{
  28. System.out.printf("您输入的数不是四位数!");
  29. }
  30. }
  31. }

49.计算字符串中某个子串出现的次数 

  1. import java.util.Scanner;
  2. public class test49 {
  3. public static void main(String args[]){
  4. int num=0;
  5. System.out.printf("输入一个字符串:");
  6. Scanner sc=new Scanner(System.in);
  7. String str1=sc.nextLine();
  8. System.out.printf("输入该字符串的子串:");
  9. String str2=sc.nextLine();
  10. if(str1.equals("")||str2.equals("")){
  11. System.out.printf("你没有输入字符串或子串!");
  12. }
  13. else{
  14. for (int i = 0; i <= str1.length()-str2.length(); i++) {
  15. if (str1.substring(i, i+str2.length()).equals(str2)) {
  16. num++;
  17. }
  18. }
  19. System.out.printf("子串"+str2+"在字符串"+str1+"中出现了"+num+"次");
  20. }
  21. }
  22. }

50.有五个学生,每个学生有 3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,把原有的数据和计算出的平均分数存放在磁盘文件 "stud"中。

  1. import java.io.*;
  2. import java.util.*;
  3. public class test50 {
  4. public static void main(String args[]){
  5. String s;
  6. int sum=0;
  7. float avg=0;
  8. Scanner sc=new Scanner(System.in);
  9. String[][] str=new String[5][6];
  10. for(int i=0;i<str.length;i++){
  11. System.out.printf("第"+(i+1)+"个学生学号:");
  12. str[i][0]=sc.nextLine();
  13. System.out.printf("第"+(i+1)+"个学生姓名:");
  14. str[i][1]=sc.nextLine();
  15. for(int j=2;j<5;j++){
  16. System.out.printf("第"+(j-1)+"个成绩:");
  17. str[i][j]=sc.nextLine();
  18. }
  19. System.out.println();
  20. }
  21. //计算平均数
  22. for(int i=0;i<str.length;i++){
  23. sum=0;
  24. for(int j=2;j<5;j++){
  25. sum=sum+Integer.parseInt(str[i][j]);
  26. }
  27. avg=(float)(sum)/3;
  28. str[i][5]=String.valueOf(avg);
  29. System.out.printf("%.2f",avg);
  30. }
  31. try{
  32. File file=new File("D://java//untitled3//src//file.txt");
  33. if(file.exists()){
  34. System.out.printf("这个文件已存在!");
  35. }
  36. else{
  37. System.out.printf("文件创建成功!");
  38. file.createNewFile();
  39. }
  40. BufferedWriter out=new BufferedWriter(new FileWriter(file));
  41. for(int i=0;i<str.length;i++){
  42. for(int j=0;j<6;j++){
  43. s=str[i][j];
  44. out.write(s+"\r\n");
  45. }
  46. }
  47. out.close();
  48. System.out.printf("数据已存入"+file.getPath());
  49. }
  50. catch(Exception e){
  51. System.out.println(e.getMessage());
  52. }
  53. }
  54. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/48717
推荐阅读
相关标签
  

闽ICP备14008679号