基础题六:
/*
题目:
输入两个正整数m和n,求其最大公约数和最小公倍数。
程序分析:
利用辗除法。先利用辗除法求出两个数的最大公约数,
最小公倍数 = (数1 × 数2)/ 最大公约数
*/
import java.util.Scanner;
public class Program06{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Please input two number:");
int num1 = sc.nextInt();
int num2 = sc.nextInt();
max_min(num1, num2);
}
public static void max_min(int num1, int num2){
int temp = 0;
//两个数的乘积一定是这两个数的公倍数
int beishu = num1 * num2;
int yueshu = 0;
if(num1 > num2){
temp = num2;
num2 = num1;
num1 = temp;
}
while(num1 != 0){
temp = num2%num1;
num2 = num1;
num1 =temp;
}
yueshu = num2;
beishu /= num2;
System.out.println(num1 + "和" + num2 + "的最大公约数是:" + yueshu);
System.out.println(num1 + "和" + num2 + "的最小公倍数是:" + beishu);
}
}
基础题七:
/**
* 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
*/
import java.util.Scanner;
public class Program07 {
public static void main(String[] args) {
System.out.println("请输入字符串:");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
Count(str);
}
public static void Count(String str) {
String E1 = "[\u4e00-\u9fa5]"; //汉字正则表达式
String E2 = "[a-zA-Z]"; //字母正则表达式
String E3 = "[0-9]"; //数字正则表达式
String E4 = "\\s"; //空格
int countChinese = 0;
int countLetter = 0;
int countNumber = 0;
int countSpace = 0;
int countOther = 0;
char[] charArray = str.toCharArray();
String[] stringArray = new String[charArray.length];
for (int i = 0; i < charArray.length; i++) {
stringArray[i] = String.valueOf(charArray[i]);
}
for (String s:stringArray) {
if(s.matches(E1)){
countChinese++;
} else if(s.matches(E2)){
countLetter++;
}else if(s.matches(E3)){
countNumber++;
}else if(s.matches(E4)){
countSpace++;
}else {
countOther++;
}
}
System.out.println("输入的汉字个数:" + countChinese);
System.out.println("输入的字母个数:" + countLetter);
System.out.println("输入的数字个数:" + countNumber);
System.out.println("输入的空格个数:" + countSpace);
System.out.println("输入的其他字符个数:" + countOther);
}
}
基础题八:
import java.util.Scanner;
/**
* 求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。
* 例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
*/
public class Program08 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入a的值:");
int a = sc.nextInt();
System.out.println("请输入a的个数n:");
int n = sc.nextInt();
System.out.println(expression(a, n) + getSum(a, n));
}
public static String expression(int a, int n) {
StringBuilder tempString = new StringBuilder();
StringBuilder str = new StringBuilder();
for (int i = 0; i < n; i++) {
tempString.append(a);
str.append(tempString);
if (i < n - 1)
str.append("+");
}
return str.append(" = ").toString();
}
public static long getSum(int a, int n) {
long sum = 0;
long tempSum = 0;
for (int i = 0; i < n; i++) {
tempSum = tempSum * 10 + a;
sum += tempSum;
}
return sum;
}
}
基础题九:
/**
* 一个数如果恰好等于它的因子之和,这个数就称为"完数"。
* 例如: 6 = 1+2+3.编程找出1000以内的所有完数。
*/
public class Program09 {
public static void main(String[] args) {
int n = 1000;
compNumber(n);
}
public static void compNumber(int n) {
int count = 0;
for (int i = 1; i < n; i++) {
int sum = 0;
for (int j = 1; j < i / 2 + 1; j++) {
if (i % j == 0){
sum += j;
}
if (i == sum){
System.out.print(i + " ");
if (++count % 5 == 0) {
System.out.println();
}
break;
}
}
}
}
}
基础题十:
import java.util.Scanner;
/**
* 一球从100米高度自由落下,每次落地后反跳回原高度的一半;
* 再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?
*/
public class Program10 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入小球落地时的高度:");
int hight = sc.nextInt();
System.out.println("请输入求解的次数:");
int n = sc.nextInt();
distance(hight, n);
}
public static void distance(int hight, int n) {
double meter = 0;
double h = hight;
for (int i = 1; i <= n; i++) {
if (i == 1) {
meter = h;
} else {
h /= 2.0;
meter += h * 2;
}
}
System.out.println("小球第" + n + "次落地时,共经过" + meter + "米,第"+ n +"次反弹" + (h / 2.0) + "米");
}
}
上一节:Java基础练习:1–5