当前位置:网站首页>循环结构 practice
循环结构 practice
2022-07-26 05:14:00 【陌 年】
1、输出100以内的所有素数,每行显示5个;并求和
public static void main(String[] args) {
//输出数字个数为0
int count = 0;
//输出数字的和
int sum = 0;
//1不是素数,从2开始.i循环一个,j循环一遍
for (int i = 2; i <= 100; i++) {
//做个标记,便于输出;当i迭代之后,重新定义flag的值
boolean flag = true;
for (int j = 2; j < i; j++) {
//当i%j==0时,说明该数不是素数,素数是只能被1和它本身整除的数
if (i % j == 0) {
//此时改变flag的值为false
flag = false;
//结束循环,执行for循环后面的代码
break;
}
}
//根据flag的值来判断,若为true则执行{}里的代码,若是false,则执行if结构后的代码
if (flag) {
System.out.print(i + "\t");
count++;
if (count % 5 == 0) {
System.out.println();
}
}
sum+=i;
}
System.out.println("一百以内的素数和="+sum);
System.out.println("程序执行完毕");
}
}
2、求两个整数的最大公约数和最小公倍数
public static void main(String[] args) {
int x, y, max, min, a, remainder, c;
Scanner sc = new Scanner(System.in);
System.out.println("请输入俩个整数:");
x = sc.nextInt();
y = sc.nextInt();
if (x > y) { // 将输入的两个值,大的赋给max,小的赋给min.
max = x;
min = y;
} else {
max = y;
min = x;
}
a = max * min;
remainder = max % min; // 求最大公约数算法:
while (remainder != 0) { // 1.max 对 min
// 进行求余,余数用remainder表示(reminder=max%min)
max = min; // 2.如果remainder等于0则min就是 max和min的最大公约数
min = remainder; // 3.如果reminder不等于0,则将min赋给max,将reminder赋给min。
remainder = max % min; // 4.进行1.2.两步最后min即为最大公约数
}
c = a / min; // 求最小公倍数的的算法:
System.out.println("最大公约数是" + min); // 最小公倍数 = (max * min)/最大公约数。
System.out.println("最小公倍数是" + c);
}
边栏推荐
- Shell process control (emphasis), if judgment, case statement, let usage, for ((initial value; loop control condition; variable change)) and for variable in value 1 value 2 value 3..., while loop
- 安装NCCL\mpirun\horovod\nvidia-tensorflow(3090Ti)
- Getting started with ALV
- 真正的科学减肥
- nacos注册中心
- C语言函数
- CLM陆面过程模式
- ALV程序收集
- Why is the value represented by a negative number greater than an integer by 1?
- Migrate the server and reconfigure the database (the database has no monitoring, and the monitoring starts with tns-12545, tns-12560, tns-00515 errors)
猜你喜欢

Please elaborate on the implementation principle of synchronized and related locks

Okaleido上线聚变Mining模式,OKA通证当下产出的唯一方式

Okaleido上线聚变Mining模式,OKA通证当下产出的唯一方式

Uniapp applet framework - a set of code, multi segment coverage

I talked with the interviewer about MySQL optimization in five dimensions

MySQL基础学习

Real scientific weight loss

Common modules in ansible

Five simple and practical daily development functions of chrome are explained in detail. Unlock quickly to improve your efficiency!

Unity scene jump script
随机推荐
When AQS wakes up the thread, I understand why it traverses from the back to the front
如何优雅的复现YOLOv5官方历程(二)——标注并训练自己的数据集
security权限管理详解
SWAT模型在水文水资源、面源污染模拟中的实践技术
@Principle of Autowired annotation
遥感、GIS和GPS技术在水文、气象、灾害、生态、环境及卫生等领域中的应用
Black eat black? The man cracked the loopholes in the gambling website and "collected wool" for more than 100000 yuan per month
Real scientific weight loss
Why is the value represented by a negative number greater than an integer by 1?
[acwing] 2983. Toys
Improve reduce parallelism in shuffle operation
C language function
NPM operation instruction
CMD operation command
MySQL基础学习
Unity scene jump script
Practical technology of SWAT Model in simulation of hydrology, water resources and non-point source pollution
[acwing] 1268. Simple questions
517. 超级洗衣机
mysql如果计算本月变动/本月增幅/同比变动/同比增幅?