java编程题50道(2)
程序6
题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
题目思路:先构造函数,主函数运用再输出
通过辗转相除法解决最大公约数,再利用公式法两数相乘除以最大公约数求得最大公倍数
1 package test; 2 3 import java.util.Scanner; 4 5 public class hello { 6 public static int big_number(int a, int b) {//使用辗转相除法,除数被除数余数互相替换 7 int c; 8 if (a%b!=0) { 9 while(a%b!=0) { 10 c=a%b; 11 a=b; 12 b=c; 13 } 14 } 15 return b; 16 } 17 public static int min_number(int x,int y) { 18 int c; 19 c=(x*y)/big_number(x, y);//最小公倍数为两数相乘除最大公约数 20 return c; 21 } 22 public static void main(String[] args) { 23 System.out.print("输入数字:");// 24 Scanner in =new Scanner(System.in); 25 int x =in.nextInt(); 26 int y =in.nextInt(); 27 System.out.print("最大公约数为"+big_number(x, y)+"最小公倍数为"+min_number(x, y));//构造函数后,用主函数输出 28 } 29 30 }
程序7
题目:输入一行字符,分别统计出其英文字母、空格、数字和其它字符的个数。
题目思路:
先用scanner获取字符串,再将字符串赋值数组,数组从中逐个取出,进行统计
1 package test; 2 3 import java.util.Scanner; 4 public class hello { 5 public static void main(String[] args) { 6 System.out.print("输入:字符"); 7 Scanner in = new Scanner(System.in); 8 String a =in.nextLine();//获取字符串 9 char[]zifuchuan=a.toCharArray();//将字符串赋值数组 10 int x=0;//统计空格 字母 数字 其他字符个数 11 int y=0; 12 int z=0; 13 int c=0; 14 for (int i = 0; i < zifuchuan.length; i++) { 15 if(zifuchuan[i] ==32) { 16 x++; 17 } 18 else if (zifuchuan[i]<=57&&zifuchuan[i]>=48) { 19 y++; 20 } 21 else if (zifuchuan[i]>=65&&zifuchuan[i]<=90||zifuchuan[i]>=97&&zifuchuan[i]<=122) { 22 z++; 23 } 24 else { 25 c++; 26 } 27 } 28 System.out.print("空格为"+x+" 数字为"+y+" 字母为"+z+" 其他字符为"+c); 29 } 30 31 }
程序8
题目:求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
题目思路:
通过循环,将输入的值每次乘以十后再加上初始值并输出,并累计数值
1 package test; 2 3 import java.util.Scanner; 4 public class hello { 5 public static void main(String[] args) { 6 System.out.print("输入:"); 7 Scanner in = new Scanner(System.in); 8 int a = in.nextInt();//获取数字及次数 9 int b = in.nextInt(); 10 int c=a;//设计累加和初始值 11 int sum=a; 12 System.out.print(a); 13 for (int i = 1; i <b; i++) { 14 a=a*10+c; 15 sum=sum+a; 16 System.out.print("+"+a);//输出每次的值 17 } 18 System.out.print("="+sum);//输出累加值 19 } 20 21 }
程序9
题目:一个数如果恰好等于它的因子之和,这个数就称为”完数”。例如6=1+2+3.编程找出1000以内的所有完数。
题目思路:
先通过函数判断完数,若是完数,因数相加之和等于完数。最后再以主函数以构造函数逐个确定,再输出
1 package test; 2 3 import java.util.Scanner; 4 public class hello { 5 public static boolean wanshu(int a) {//函数判断是否是完数 6 int sum = 0; 7 for (int i = 1; i < a; i++) { 8 if (a%i==0) { 9 sum+=i; 10 } 11 } 12 if (sum==a) {//若是函数因数和等于原数 13 return true; 14 } 15 return false; 16 } 17 public static void main(String[] args) { 18 System.out.print("输入:"); 19 Scanner in = new Scanner(System.in); 20 int sum =0; 21 for (int i = 1; i <1000; i++) { 22 if (wanshu(i)) { 23 System.out.print(i+" ");//输出每个完数 24 sum++; 25 } 26 } 27 System.out.print("共有"+sum);//输出累加值 28 } 29 30 }
程序10
题目:一球从h米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第n次落地时,共经过多少米?第n次反弹多高?
题目思路:
每次高度均为上次一半 h=h/2 经过则是每次高度乘2,以sum累加 sum+=h*2
1 package test; 2 3 import java.util.Scanner; 4 public class hello { 5 6 public static void main(String[] args) { 7 System.out.print("输入:"); 8 Scanner in = new Scanner(System.in); 9 int h =in.nextInt();//输入高度,以及反弹次数 10 int n =in.nextInt(); 11 int sum=h; 12 for (int i = 2; i <=n; i++) { 13 h=h/2;//确定反弹高度 14 sum+=h*2;//累计经过长度 15 } 16 System.out.print("在第"+n+"次落地时,共经过"+sum+"米 第"+n+"次反弹"+h);//输出 17 } 18 19 }

浙公网安备 33010602011771号