2017.8.6每天五个编程题(二)

第六题:输入两个正整数m和n,求其最大公约数和最小公倍数。

package test11;

import java.util.Scanner;

public class Test6 {
	/**
	 * 题目:输入两个正整数m和n,求其最大公约数和最小公倍数。 在循环中,只要除数不等于0,用较大数除以较小的数,将小的一个数作为下一轮循环的大数,
	 * 取得的余数作为下一轮循环的较小的数,如此循环直到较小的数的值为0,返 回较大的数,此数即为最大公约数,最小公倍数为两数之积除以最大公约数。
	 */

	public static void main(String[] args) {
		System.out.println("输入一个整数:");
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		System.out.println("请再输入一个整数:");
		int b = sc.nextInt();
		deff de = new deff();
		int m=de.deff(a, b);
		int n=a*b/m;
		System.out.println(a+"和"+b+"的最小公倍数是:"+n+"。最大公约数是:"+m);

	}

		static class deff {
		public int deff(int x, int y) {
			int num;
			if (x < y) {
				num = x;
				x = y;
				y = num;
			}
			while (y != 0) {
				if (x == y) {
					return x;
				} else {
					int k = x % y;
					x = y;
					y = k;
				}
			}
			return x;
		}
	}

}

运行结果:


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

package test11;

import java.util.Scanner;

public class Test7 {

	public static void main(String[] args) {
		int digital = 0;
		int character = 0;
		int other = 0;
		int blank = 0;
		System.out.println("请输入内容:");
		Scanner scanner = new Scanner(System.in);
		String s = scanner.nextLine();
		char[] cs = s.toCharArray();
		for (int i = 0; i < cs.length; i++) {
			if (cs[i]>='0'&&cs[i]<='9') {
				digital++;
			}else if (cs[i]>='a'&&cs[i]<='z') {
				character++;
			}else if (cs[i]==' ') {
				blank++;
			}else {
				other++;
			}
			
		}
		System.out.println("这串字符串共有数字"+digital+"个,字母"+
		character+"个,空格"+blank+"个,其他字符"+other+"个。");
		
	}

}



运行结果:


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

package test11;

import java.util.Scanner;

public class Test8 {
/*
 * 题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。
 * 例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。   
 **/
	public static void main(String[] args) {
		Scanner scanner =new Scanner(System.in);
		int sum = 0;
		int sum1 = 0;
		System.out.println("请输入一个数字:");
		int n = scanner.nextInt();
		System.out.println("请输入他要加多少次:");
		int m = scanner.nextInt();
		int i =1;
		while (i<=m) {
			int j=0;
			j+=n;
			sum+=j;
			sum1+=sum;
			n*=10;
			++i;
		}
		System.out.println(sum1);
	}

}

运行结果:


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

package test11;

public class Test9 {

	public static void main(String[] args) {
		
		for (int i = 1; i <= 1000; i++) {
			int t = 0;
			for (int j = 1; j <= i/2; j++) {
				if (i%j==0) {
					t+=j;
				}
			}
			if (t==i) {
				System.out.println(" "+i);
			}
		}

	}

}

运行结果:


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

package test11;

public class Test10 {

	public static void main(String[] args) {
		int s =100;
		int h = 100;
		for (int i = 0; i < 10; i++) {
			s+=h;
			h=h/2;
		}
		System.out.println("路程为"+s+"高度为"+h);
	}

}

运行结果:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张小五丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值