= f
cpare(s);
if(grade == 1) {
System.out.print('A');
} else if(grade == 2) {
System.out.print('B');
} else {
3
System.out.println('C');
}
}
public int compare(int s) {
return s > 90 ? 1
: s > 60 ? 2
:3;
}
}
/*【程序6】
作者 若水飞天
题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
1.程序分析:利用辗除法。 */
/*
* 在循环中,只要除数不等于0,用较大数除以较小的数,将小的一个数作为下一轮循环的大数,取得的余数作为下一轮循环的较小的数,如此循环直到较小的数的值为0,返回
* 较大的数,此数即为最小公约数,最小公倍数为两数之积除以最小公倍数。
* */
package cn.flywater.FiftyAlgorthm;
import java.util.Scanner;
public class SixthCommonDiviser {
public static void main(String[] args) {
int a, b;
Scanner s1 = new Scanner(System.in);
Scanner s2 = new Scanner(System.in);
a = s1.nextInt();
b = s2.nextInt();
SixthCommonDiviser scd = new SixthCommonDiviser();
int m = scd.division(a, b);
int n = a * b / m;
System.out.println("最大公约数: " + m);
System.out.println("最小公倍数: " + n);
}
public int division(int x, int y) {
int t;
if(x < y) {
t = x;
x = y;
y = t;
}
while(y != 0) {
if(x == y) return 1;
else {
4
int k = x % y; x = y; y = k; } } return x; } } /*【程序7】 作者 若水飞天 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 1.程序分析:利用while语句,条件为输入的字符不为 '\n '. */ package cn.flywater.FiftyAlgorthm;
import java.util.*;
public class SeventhCharacterStatistics {
static int digital = 0;
static int character = 0;
static int other = 0;
static int blank = 0;
public static void main(String[] args) {
char[] ch = null;
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
ch = s.toCharArray();
for(int i=0; i
= '0' &;&; ch[i] <= '9') { digital ++; } else if((ch[i] >= 'a' &;&; ch[i] <= 'z') || ch[i] > 'A' &;&; ch[i] <= 'Z') { character ++; } else if(ch[i] == ' ') { blank ++; } else { other ++; } } System.out.println("数字个数: " + digital); System.out.println("英文字母个数: " + character); System.out.println("空格个数: " + blank); System.out.println("其他字符个数:" + other ); }
5
}
/*【程序8】
作者 若水飞天
题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
*/
/*
* 算法: 定义一个变量b, 赋初值为0;定义一变量sum, 赋初值为0,
* 进入循环后,将a + b 的值赋给b,将sum + b 的值赋给sum;
* 同时,将a 增加十倍, ++ i; 继续循环;
* 循环结束后,输出sum 的值。
*/
package cn.flywater.FiftyAlgorthm;
import java.util.Scanner;
public class EightPlus {
static long a = 2, b = 0;
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int i = 0;
long sum = 0;
while(i < n) {
b = b + a;
sum = sum + b;
a = a * 10;
++ i;
}
System.out.println("input number: " + n);
System.out.println(sum);
}
}
/*【程序9】
题目:一个数如果恰好等于它的因子之和,这个数就称为 "完数 "。例如6=1+2+3.编程
找出1000以内的所有完数。
*/
package cn.flywater.FiftyAlgorthm;
public class NinthWanshu {
public static void main(String[] args) {
System.out.println("1到1000的完数有: ");
for(int i=1; i<1000; i++) {
int t = 0;
for(int j=1; j<= i/2; j++) {
if(i % j == 0) {
6
t = t + j;
}
}
if(t == i) {
System.out.print(i + " ");
}
}
}
}
/*【程序10】
作者 若水飞天
题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,
求它在 第10次落地时,共经过多少米?第10次反弹多高?
*/
p