当前位置:网站首页>循环结构 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);
}
边栏推荐
- DOM event flow event bubble event capture event delegate
- Embedded sharing collection 20
- CLM陆面过程模式
- Security permission management details
- 嵌入式分享合集21
- Getaverse,走向Web3的远方桥梁
- CMD操作命令
- Embedded development notes, practical knowledge sharing
- Map making of environmental impact assessment based on remote sensing interpretation and GIS technology
- 新人如何做好功能测试,学会这几项够用了
猜你喜欢

Compilation method of flood control evaluation report and flood modeling under the new guidelines

使用Ansible中的playbook

I talked with the interviewer about MySQL optimization in five dimensions

FPGA刷题——序列检测

Recommend 12 academic websites for free literature search, and suggest to like and collect!

ALV程序收集

The first positive number missing in question 41 of C language. Two methods, preprocessing, fast sorting and in situ hashing
![[pytorch] install torch 1.8.1 and check whether torch version and GPU are available](/img/97/078c72729a29675939a84895b5ab86.png)
[pytorch] install torch 1.8.1 and check whether torch version and GPU are available

Week 6 Learning Representation: Word Embedding (symbolic →numeric)

C language function
随机推荐
Shell read read console input, use of read
Excel vba: saving multiple worksheets as new files
I talked with the interviewer about MySQL optimization in five dimensions
JVM Lecture 2: class loading mechanism
Okaleido上线聚变Mining模式,OKA通证当下产出的唯一方式
LeetCode链表问题——203.移除链表元素(一题一文学会链表)
Mathematical modeling and optimization analysis based on general optimization software gams
C语言力扣第41题之缺失的第一个正数。两种方法,预处理快排与原地哈希
JVM Lecture 6: how to solve the frequent FGC in online environment?
Nacos 介绍和部署
【pytorch】torch1.8.1安装、查看torch版本、GPU是否可用
Embedded sharing collection 21
C语言详解系列——函数的认识(3)形参,实参,嵌套调用和链式访问
Shell流程控制(重点)、if 判断、case 语句、let用法、for 循环中有for (( 初始值;循环控制条件;变量变化 ))和for 变量 in 值 1 值 2 值 3… 、while 循环
Embedded sharing collection 20
How to conduct test case review
C language - Advanced pointer
Distance between bus stops: simple simulation problem
How many holes have you stepped on in BigDecimal?
Simulation of future air pollution changes