第3章 循环语句
笔记
while循环
可以简单理解为循环版的if语句。if
语句是判断一次,如果条件成立,则执行后面的语句;while
是每次判断,如果成立,则执行循环体中的语句,否则停止。
int i = 0;
while (i < 10) {
System.out.println(i);
i ++;
}
死循环:循环永久执行,无法结束。我们要避免写出死循环。
int x = 1;
while (x == 1)
System.out.println("!");
do while循环
do while
循环不常用。
do while
语句与while
语句非常相似。唯一的区别是,do while
语句限制性循环体后检查条件。不管条件的值如何,我们都要至少执行一次循环。
int y = 1;
do {
System.out.println("y!");
} while (y < 1);
for循环
基本思想:把控制循环次数的变量从循环体中剥离。
for (init-statement; condition; expression) {
statement
}
init-statement
可以是声明语句、表达式、空语句,一般用来初始化循环变量;
condition
是条件表达式,和while
中的条件表达式作用一样;可以为空,空语句表示true
;
expression
一般负责修改循环变量,可以为空。
for (int i = 0; i < 10; i ++ ) { // 循环体中只有一条语句时,可以不加大括号
System.out.println(i);
}
init-statement
可以定义多个变量,expression
也可以修改多个变量。
跳转语句
break
可以提前从循环中退出,一般与if
语句搭配。
continue
可以直接跳到当前循环体的结尾。作用与if
语句类似。
多层循环
for (int i = 0; i < 10; i ++ ) {
for (int j = 0; j <= i; j++) {
System.out.printf("%d ", i + j);
}
System.out.println();
}
习题
AcWing 708. 偶数
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 100; i++)
if (i % 2 == 0) System.out.println(i);
}
}
AcWing 709. 奇数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();
scanner.close();
for (int i = 1; i <= x; i++)
if (i % 2 != 0) System.out.println(i);
}
}
AcWing 712. 正数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int cnt = 0;
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 6; i++) {
double x = scanner.nextDouble();
if (x > 0) cnt++;
}
scanner.close();
System.out.println(cnt + " positive numbers");
}
}
AcWing 714. 连续奇数的和 1
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt(), y = scanner.nextInt();
scanner.close();
if (x > y) {
int tmp = x;
x = y;
y = tmp;
}
int sum = 0;
for (int i = x + 1; i < y; i++)
if (i % 2 != 0) sum += i;
System.out.println(sum);
}
}
AcWing 716. 最大数和它的位置
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int max = -1, pos = -1;
for (int i = 1; i <= 100; i++) {
int x = scanner.nextInt();
if (x > max) {
max = x;
pos = i;
}
}
scanner.close();
System.out.println(max);
System.out.println(pos);
}
}
AcWing 721. 递增序列
import java.util.Scanner;
public class Main {
public static void print(int x) {
for (int i = 1; i <= x; i++)
System.out.printf(i + " ");
System.out.println();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(true) {
int x = scanner.nextInt();
if (x == 0) break;
else print(x);
}
scanner.close();
}
}
AcWing 720. 连续整数相加
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int n = scanner.nextInt();
while(n <= 0) n = scanner.nextInt();
scanner.close();
int sum = 0;
for (int i = a; i < a + n; i++) sum += i;
System.out.println(sum);
}
}
AcWing 724. 约数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.close();
for (int i = 1; i <= n; i++)
if (n % i == 0) System.out.println(i);
}
}
AcWing 723. PUM
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
int n = scanner.nextInt();
scanner.close();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n - 1; j++) {
System.out.printf("%d ", i * n + j + 1);
}
System.out.println("PUM");
}
}
}
AcWing 715. 余数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.close();
for (int i = 2; i < 10000; i++)
if (i % n == 2) System.out.println(i);
}
}
AcWing 710. 六个奇数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();
scanner.close();
if (x % 2 == 0) x++;
for (int i = 0; i < 6; i++) {
System.out.println(x);
x += 2;
}
}
}
AcWing 711. 乘法表
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.close();
for (int i = 1; i <= 10; i++)
System.out.println(i + " x " + n + " = " + (i * n));
}
}
AcWing 718. 实验
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int coney = 0, rat = 0, frog = 0;
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
int num = scanner.nextInt();
String type = scanner.next();
if (type.equals("C")) coney += num;
else if (type.equals("R")) rat += num;
else frog += num;
}
scanner.close();
int total = coney + rat + frog;
System.out.printf("Total: %d animals\n", total);
System.out.printf("Total coneys: %d\n", coney);
System.out.printf("Total rats: %d\n", rat);
System.out.printf("Total frogs: %d\n", frog);
System.out.printf("Percentage of coneys: %.2f %%\n", coney * 100.0 / total);
System.out.printf("Percentage of rats: %.2f %%\n", rat * 100.0 / total);
System.out.printf("Percentage of frogs: %.2f %%\n", frog * 100.0 / total);
}
}
AcWing 713. 区间 2
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int cnt = 0;
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
int x = scanner.nextInt();
if (x >= 10 && x <= 20) cnt++;
}
scanner.close();
System.out.println(cnt + " in");
System.out.println((n - cnt) + " out");
}
}
AcWing 719. 连续奇数的和 2
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
int x = scanner.nextInt();
int y = scanner.nextInt();
if (x > y) {
int tmp = x;
x = y;
y = tmp;
}
int sum = 0;
for (int j = x + 1; j < y; j++)
if (j % 2 != 0) sum += j;
System.out.println(sum);
}
scanner.close();
}
}
AcWing 717. 简单斐波那契
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.close();
int[] a = {0, 1, 1};
for (int i = 0; i < Math.min(3, n); i++)
System.out.printf(a[i] + " ");
for (int i = 3; i < n; i++) {
a[0] = a[1];
a[1] = a[2];
a[2] = a[0] + a[1];
System.out.printf(a[2] + " ");
}
System.out.println("");
}
}
AcWing 722. 数字序列和它的和
import java.util.Scanner;
public class Main {
public static void print(int m, int n) {
if (m > n) {
int tmp = m;
m = n;
n = tmp;
}
int sum = 0;
for (int i = m; i <= n; i++) {
System.out.printf(i + " ");
sum += i;
}
System.out.println("Sum=" + sum);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(true) {
int m = scanner.nextInt();
int n = scanner.nextInt();
if (m <= 0 || n <= 0) break;
print(m, n);
}
scanner.close();
}
}
AcWing 725. 完全数
import java.util.Scanner;
public class Main {
public static boolean judge(int x) {
int sum = -x;
for (int i = 1; i <= x / i; i++) {
if (x % i == 0) {
sum += i;
if (i != x / i) sum += x / i;
}
}
return sum == x;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
int x = scanner.nextInt();
if (judge(x)) System.out.println(x + " is perfect");
else System.out.println(x + " is not perfect");
}
scanner.close();
}
}
笔记
约数成对出现。如果$i$是$x$的约数,那么$\frac{x}{i}$亦是$x$的约数。因此不必从$1$遍历到$n$,而只需从$1$遍历到$\sqrt{x}$。但要注意
- 如果$\sqrt{x}$是$x$的约数时,只能计算一次;
- $1$和$x$都是$x$的约数,遍历到$1$时,会把$x$也加入到总和中,因此可把总和初值设为$-x$,抵消掉约数$x$
AcWing 726. 质数
import java.util.Scanner;
public class Main {
public static boolean judge(int x) {
for (int i = 2; i * i <= x; i++)
if (x % i == 0) return false;
return true;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
int x = scanner.nextInt();
if (judge(x)) System.out.println(x + " is prime");
else System.out.println(x + " is not prime");
}
scanner.close();
}
}
笔记
利用约数成对出现的特点,优化找约数的时间复杂度。
AcWing 727. 菱形
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.close();
int x0 = n / 2, y0 = n / 2;
for (int y = 0; y < n; y++) {
for (int x = 0; x < n; x++)
if (Math.abs(x - x0) + Math.abs(y - y0) <= n / 2)
System.out.printf("*");
else
System.out.printf(" ");
System.out.println("");
}
}
}
笔记
用曼哈顿距离描述何时输出*
。假设菱形中心坐标为$(x_0,y_0)$,当位置$(x,y)$满足$|x-x_0|+|y-y_0| = d \leqslant \frac{a}{2}$时,输出*
,否则输出空格。其中$a$是菱形对角线长度。
注意本题限制$n$为奇数,因此可用曼和顿距离描述。