Java基础算法练习五题——(2)

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

package com.myd.ex2;

import java.util.*;

public class Zzxc {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int x = sc.nextInt();
		int y = sc.nextInt();
		Calc a = new Calc();
		System.out.println("最大公约数是:"+a.gcd(x, y));
		System.out.println("最小公倍数是:"+a.lcm(x, y));
	}

}

class Calc {
	public int gcd(int m, int n) {
		if (m < n) {
			Swap(m, n);
		}
		if (m % n == 0) {
			return n;
		} else {
			m %= n;
			return gcd(n, m);
		}
	}

	public int lcm(int m, int n) {
		int i = 1;
		if (m < n) {
			Swap(m, n);
		}
		int lcm = m;
		while (lcm % n != 0) {
			lcm = m * i;
			i++;
		}
		return lcm;
	}

	void Swap(int m, int n) {
		int temp = m;
		m = n;
		n = temp;
	}
}
7. 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 

package com.myd.ex2;
import java.util.*;
public class CountChar {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		String str=sc.nextLine();
		int digit=0,space=0,letter=0,other=0;
		char a[]=str.toCharArray();
		for(int i=0;i<a.length;i++) {
			if(Character.isDigit(a[i])) {
				digit++;
			}else if(Character.isSpaceChar(a[i])) {
				space++;
			}else if(Character.isLetter(a[i])) {
				letter++;
			}else other++;
		}
		System.out.println("字母有:"+letter+"个");
		System.out.println("空格有:"+space+"个");
		System.out.println("数字有:"+digit+"个");
		System.out.println("其他字符有:"+other+"个");
	}

}

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

package com.myd.ex2;

import java.util.*;

public class CalcSeq {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		long sum = 0;
		long x[] = new long[b + 1];
		for (int i = 1; i < b + 1; i++) {
			x[i] = Plus(i, a);
			sum += x[i];
		}
		System.out.println(sum);
	}

	public static long Plus(int i, int a) {
		long temp = 0;
		for (int n = i; n > 0; n--) {
			temp = (int) (a * Math.pow(10, n - 1)) + temp;
		}
		return temp;
	}

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

package com.myd.ex2;

public class FullNum {
	public static void main(String[] args) {
		int x = 1000;
		int sum, j, i;
		for (i = 1; i <= x; i++) {
			sum = 0;
			for (j = 1; j < i; j++) {
				if (i % j == 0) {
					sum += j;
				}
			}
			if (sum == i) {
				System.out.println(sum);
			}
		}
	}
}
10. 一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多 少米?第10次反弹多高?

 

package com.myd.ex2;

public class ballJump {

	public static void main(String[] args) {
		double height=100,sum=0;
		for(int i=0;i<10;i++) {
			sum+=height;
			height/=2;
			sum+=height;
		}
		System.out.println(sum-height);
		System.out.println(height);
	}

}

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值