Basic programming questions (3)

21. There are 5 acres of grass, and a sheep eats 1.2 square meters of grass per hour. Please calculate how long it takes to eat all the grass

package cutestFox;

public class Test007 {
	public static void main(String[] args) {
		double squareMeter = 0.0002471;
		int acre = 5;
		double speed = 1.2;
		double time = (acre/squareMeter)/speed;
		System.out.println("需要"+time+"小时");
	}
}

22. Output odd numbers of 1-100 (output 6 per line)

package cutestFox;

public class Test008 {
	public static void main(String[] args) {
		int k = 1;
		for(int i=1;i<=100;i++) {
			if(i%2==0)
				continue;//取出奇数,continue跳出此次循环
				System.out.print(i+"\t"); //"\t"换行输出
				if(k++%6==0)//控制输出个数
					System.out.println();
				
			}
		}
	}

23.1-100 Summation (for while and do/while writing)

package cutestFox;
//for语句求1+2+3+....+100的和

public class Test009 {
	public static void main(String[] args) {
		int res = 0;
		for(int i= 0;i<101;i++)
			res +=i; 
		System.out.println("1+2+3+....+100="+res);
	}
	
	
}


package cutestFox;
//while语句求1+2+3+....+100的和
public class Test010 {
	public static void main(String[] args) {
		int res = 0;
		int kk = 1;
		while(kk<=100) {
			res=res+kk;
			kk++;
		}
		System.out.println("1+2+3+....+100="+res);;
	}

}


package cutestFox;
//do while语句求1+2+3+....+100的和
public class Test011 {
	public static void main(String[] args) {
		int res = 0;
		int kk=0;
		do {
			res +=kk;
			kk++;	
		} while (kk<101);
		System.out.println("1+2+3+....+100="+res);
	}


}

24.1-100 Sum of odd numbers

package cutestFox;

//1-100奇数求和
public class Test012 {
	public static void main(String[] args) {
		int res = 0;
		for (int i = 1; i < 101; i++)
			res += i++;
		System.out.println(res);
	}

}

25.1~100 is a number divisible by 3

package cutestFox;
//1~100可以被3整除的数

public class Test013 {
	public static void main(String[] args) {
		int kk=1;
		for(int i=1;i<=100;i++)
			if(i%3==0)
				System.out.println(i);
	}

}

26. Find all numbers within 100 that are divisible by 3 but not divisible by 5

package cutestFox;

//求100 以内所有能被3 整除但不能被5 整除的个数
public class Test014 {
	public static void main(String[] args) {
		int xx = 0;
		for (int i = 0; i < 101; i++)
			if (i % 3 == 0 && i % 5 != 0)
				xx++;
		System.out.println("100以内所有能被3整但不能被5整除的个数为:" + xx + "个");

	}
}

27. Print out all the daffodil numbers (Narcissistic number (Narcissistic number) is also known as the pluperfect digital invariant, PPDI), narcissistic number, self-power number, Armstrong number or Armstrong number (Armstrong number), the daffodil number refers to a 3 digit number, and the sum of the power of 3 of each digit is equal to itself (for example: 1^3 + 5^3+ 3^3 = 153))

package cutestFox;
//打印水仙花数
public class Test015 {
	public static void main(String[] args) {
		for(int k=100;k<1000;k++) {
			int a = k/100;//获得百位数
			int b = (k-100*a)/10;//获得十位数
			int c = k%10;//获得个位数
			if(a*a*a+b*b*b+c*c*c==k)
				System.out.println(k);
		}
	}

}

28. Determine whether a number is prime

package cutestFox;

import java.util.Scanner;

//判断一个数是否质数

public class Test016 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入一个你心中的数字:");
		int primeNumber = sc.nextInt();
	System.out.println(prime(primeNumber));
}
	public static boolean prime(int x) {
		boolean res = true;
		for(int i = 2;i<=x;i++) {
			if(x%i==0) {
				res = false;
			break;
			}
		}
		return res;
	}
}

29. Programming to find all prime numbers in natural numbers 101-205

package cutestFox;
public class Test017 {
	public static void main(String[] args) {
		int total = 0;
		outer: for (int i = 101; i < 206; i++) {
			for (int k = 2; k < i / 2; k++) {
				if (i % k == 0) {
					continue outer;
				}
			}
			System.out.print(i + "\t");
			total++;
			if (total % 5 == 0)
				System.out.println();
		}
	}

}

30. Enter two positive integers m and n, and find their greatest common divisor and least common multiple

package cutestFox;

import java.util.Scanner;
public class Test018 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入正整数:n");
		int n = sc.nextInt();
		System.out.println("请输入正整数:m");
		int m = sc.nextInt();
		System.out.println(n + "和" + m + "最大公约数为:" + GeCommonDivisor(n, m));
		System.out.println(n + "和" + m + "最小公倍数为:" + GeCommonMultiple(n, m));
	}

	public static int GeCommonDivisor(int n, int m) {
		int min = Math.min(n, m);// 获取两个数的最小值
		for (int k = min; k > 1; k--) {
			if (n % k == 0 && m % k == 0) {// 当两个数都可以被整除时,此时就是最大公约数
				return k;
			}
		}
		return -1;// 都不满足上面的返回-1;比如说两个数互为质数
	}

	public static int GeCommonMultiple(int n, int m) {
		int max = Math.max(n, m);// 获取两个中最大的值
		if (max % n == 0 && max % m == 0) // max肯定能除尽他本身,但是不一定除尽另一个数,所以当既可以除尽它本身,又可以除尽领一个数时。max就是最小公倍数
			return max;

		for (int k = max; k <= m * n; k++) {
			if (k % n == 0 && k % m == 0)
				return k;
			break;
		}
		return m * n;
	}
}    

Guess you like

Origin blog.csdn.net/qq_45874107/article/details/112774457