循环语句
1.偶数
编写一个程序,输出 1 到 100 之间(包括 1 和 100)的全部偶数。
输入格式
无输入。
输出格式
输出全部偶数,每个偶数占一行。
输入样例
No input
输出样例
2
4
6
…
100
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
for(int i=1;i<=100;i++)
{
if(i%2==0)
{
System.out.printf("%d\n",i);
}
}
}
}
2.奇数
输入一个整数 X,输出 1 到 X 之间(包括 1 和 X)的全部奇数。
输入格式
一个整数 X。
输出格式
输出所有满足条件的奇数,每个数占一行。
数据范围
1≤X≤1000
输入样例:
8
输出样例:
1
3
5
7
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
for(int i=1;i<=x;i++)
{
if(i%2!=0) System.out.printf("%d\n",i);
}
}
}
3.正数
输入 6 个实数,它们要么是正数,要么是负数。
请你统计并输出正数的个数。
输入格式
六个数字,每个占一行。
输出格式
输出格式为 x positive numbers,其中 x 为正数的个数。
数据范围
输入数字的绝对值不超过 100。
输入样例:
7
-5
6
-3.4
4.6
12
输出样例:
4 positive numbers
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int x=0;//正数的个数输入一个整数 X,输出 1 到 X 之间(包括 1 和 X)的全部奇数。
for(int i=0;i<6;i++)//六个数字
{
double a=sc.nextDouble();
if(a>0)
{
x++;
}
}
System.out.printf("%d positive numbers",x);
}
}
4.连续奇数的和1
给定两个整数 X 和 Y,输出在他们之间(不包括 X 和 Y)的所有奇数的和。
输入格式
第一行输入 X,第二行输入 Y。
输出格式
输出一个整数,表示所有满足条件的奇数的和。
数据范围
−100≤X,Y≤100
输入样例1:
6
-5
输出样例1:
5
输入样例2:
15
12
输出样例2:
13
输入样例3:
12
12
输出样例3:
0
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int x=sc.nextInt(),y=sc.nextInt();
int sum=0;
if(x>y)//如果x>y,交换x与y的位置,为确保x<y;
{
int t=x;
x=y;
y=t;
}
for(int i=x+1;i<y;i++)
{
if(i%2!=0)
{
sum+=i;
}
}
System.out.printf("%d",sum);
}
}
5.最大数和他的位置
给定 100 个整数,请你找出其中最大的数字,以及它的输入位置(位置从 1 开始)。
输入格式
共 100 行,每行包含一个整数。
输出格式
第一行输出最大的数字。
第二行输出该数字的输入位置。
数据范围
1≤输入数字≤50000,
保证输入数字互不相同。
输入样例:
22229
48558
24992
4755
11923
…
20213
输出样例:
48558
2
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=-1,b=-1;//初始化最大数和位置
for(int i=1;i<=100;i++)//位置从1开始
{
int x=sc.nextInt();//读入当前数
if(a<x)
{
a=x;
b=i;
}
}
System.out.printf("%d\n%d\n",a,b);
}
}
6.递增序列
读取一系列的整数 X,对于每个 X,输出一个 1,2,…,X 的序列。
输入格式
输入文件中包含若干个整数,其中最后一个为 0,其他的均为正整数。
每个整数占一行。
对于输入的正整数,按题目要求作输出处理。
对于最后一行的整数 0,不作任何处理。
输出格式
对于每个输入的正整数 X,输出一个从 1 到 X 的递增序列,每个序列占一行。
数据范围
1≤X≤100
输入样例:
5
10
3
0
输出样例:
1 2 3 4 5
1 2 3 4 5 6 7 8 9 10
1 2 3
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
while (true)
{
int x=sc.nextInt();
if(x==0) break;
for(int i=1;i<=x;i++)
{
System.out.printf("%d ",i);
}
System.out.printf("\n");
}
}
}
7.连续整数相加
读入两个整数值 A 和 N,计算从 A 开始的 N 个连续整数的和。
注意,如果读入的 N 为 0 或负数,则继续读取数字直至读入 N 值为正整数为止。
输入格式
共一行,包含整数 A 和若干个整数 N(不超过 100 个)。
输出格式
一个整数,表示从 A 开始的 N 个连续整数的和。
数据范围
1≤A≤100,
−100≤N≤100
输入样例1:
3 2
输出样例1:
7
输入样例2:
3 -1 0 -2 2
输出样例2:
7
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt(),n=0;
while(n<=0) n=sc.nextInt();//如果n为0或负数则读入下一个数字
int sum=0;
for(int i=a;i<a+n;i++)
{
sum+=i;
}
System.out.printf("%d",sum);
}
}
8.约数
输入一个整数 N,按照从小到大的顺序输出它的全部约数。
输入格式
一个整数 N。
输出格式
输出全部约数,每个约数占一行。
数据范围
1≤N≤1000
输入样例:
6
输出样例:
1
2
3
6
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=1;i<=n;i++)
{
if(n%i==0)//余数为0则说明i是n的约数
{
System.out.printf("%d\n",i);
}
}
}
}
9.PUM
输入两个整数 N 和 M,构造一个 N 行 M 列的数字矩阵,矩阵中的数字从第一行到最后一行,按从左到右的顺序依次为 1,2,3,…,N×M。
矩阵构造完成后,将每行的最后一个数字变为 PUM。
输出最终矩阵。
输入格式
共一行,包含两个整数 N 和 M。
输出格式
输出最终矩阵,具体形式参照输出样例。
数据范围
1≤N,M≤20
输入样例:
7 4
输出样例:
1 2 3 PUM
5 6 7 PUM
9 10 11 PUM
13 14 15 PUM
17 18 19 PUM
21 22 23 PUM
25 26 27 PUM
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(),m=sc.nextInt();
for(int i=1,k=1;i<=n;i++)//n是输入数字的行数
{
for(int j=1;j<=m;j++,k++)//j是输出数字的个数,k是输出数字的大小
{
if(j==m)//如果输出数字的个数j等于输入数字的列数m
{
System.out.println("PUM");//输出PUM
}
else
{
System.out.printf("%d ",k);//输出数字
}
}
}
}
}
10.余数
输入一个整数 N,请按顺序输出 1 到 10000 之间(不包括 1 和 10000)的所有除以 N 余 2 的数字。
输入格式
一个整数 N。
输出格式
输出所有满足条件的数字,从小到大每行输出一个。
数据范围
2<N<10000
输入样例:
13
输出样例:
2
15
28
41
…
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=2;i<10000;i++)
{
if(i%n==2)//除以n余为2
{
System.out.println(i);
}
}
}
}
11.六个奇数
读取一个整数 X,输出 X 之后的 6 个奇数,如果 X 也是奇数,那么它也算作 6 个奇数之一。
输入格式
一个整数 X。
输出格式
所有满足条件的奇数,每个占一行。
数据范围
1≤X≤100
输入样例:
9
输出样例:
9
11
13
15
17
19
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
if(x%2==0) x++;//如果x不是奇数,加一让他成为奇数
for(int i=0;i<6;i++)//i指的是x后面数字的个数
{
System.out.println(x+i*2);//x后面的五个奇数计算方式:假设x=5,则5+1*2=7,5+2*2=9,5+3*2=11...
}
}
}
12.乘法表
输入一个整数 N,输出 N 的乘法表,如下:
1 x N = N
2 x N = 2N
…
10 x N = 10N
输入格式
一个整数 N。
输出格式
输出 N 的乘法表,具体形式参照输出样例。
数据范围
1<N<1000
输入样例:
140
输出样例:
1 x 140 = 140
2 x 140 = 280
3 x 140 = 420
4 x 140 = 560
5 x 140 = 700
6 x 140 = 840
7 x 140 = 980
8 x 140 = 1120
9 x 140 = 1260
10 x 140 = 1400
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=1;i<=10;i++)
{
System.out.printf("%d x %d = %d\n",i,n,i*n);
}
}
}
13.实验
医学部一共进行了 N 场动物实验。
共有三种小动物可用来实验,分别是青蛙、老鼠和兔子。
每次实验都会选取其中一种动物来参与实验,选取数量若干。
现在请你统计一下医学部一共用了多少小动物,每种分别用了多少,每种动物使用数量占总量的百分比分别是多少。
输入格式
第一行包含整数 N,表示实验次数。
接下来 N 行,每行包含一个整数 A(表示一次实验使用的小动物的数量)和一个字符 T(表示一次实验使用的小动物的类型,C 表示兔子(coney),R 表示老鼠(rat),F 表示青蛙(frog))。
输出格式
请你参照输出样例,输出所用动物总数,每种动物的数量,以及每种动物所占百分比。
注意输出百分比时,保留两位小数。
数据范围
1≤N≤100,
1≤A≤15
输入样例:
10
10 C
6 R
15 F
5 C
14 R
9 C
6 R
8 F
5 C
14 R
输出样例:
Total: 92 animals
Total coneys: 29
Total rats: 40
Total frogs: 23
Percentage of coneys: 31.52 %
Percentage of rats: 43.48 %
Percentage of frogs: 25.00 %
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int cnt1=0,cnt2=0,cnt3=0;
for(int i=0;i<n;i++)
{
int a=sc.nextInt();
char t=sc.next().charAt(0);
if(t=='C') cnt1+=a;
if(t=='R') cnt2+=a;
if(t=='F') cnt3+=a;
}
int tot=cnt1+cnt2+cnt3;
System.out.printf("Total: %d animals\n",tot);
System.out.printf("Total coneys: %d\n",cnt1);
System.out.printf("Total rats: %d\n",cnt2);
System.out.printf("Total frogs: %d\n",cnt3);
System.out.printf("Percentage of coneys: %.2f %%\n",cnt1*1.0/tot*100);
System.out.printf("Percentage of rats: %.2f %%\n",cnt2*1.0/tot*100);
System.out.printf("Percentage of frogs: %.2f %%",cnt3*1.0/tot*100);
}
}
14.区间2
读取 N 个整数 X1,X2,…,XN,判断这 N 个整数中有多少个在 [10,20] 的范围内,有多少个在范围外。
输入格式
第一行包含整数 N,表示共有 N 个整数需要进行判断。
接下来 N 行,每行包含一个整数 Xi。
输出格式
第一行输出 x in,其中 x 为在范围内的整数的数量。
第二行输出 y out,其中 y 为在范围外的整数的数量。
数据范围
1≤N≤10000,
−107<Xi<107
输入样例:
4
14
123
10
-25
输出样例:
2 in
2 out
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(),t=0,f=0;
for(int i=1;i<=n;i++)
{
int x=sc.nextInt();
if(x>=10&&x<=20) t++;
else f++;
}
System.out.printf("%d in\n%d out"t,f);
}
}
15.连续奇数的和2
输入 N 对整数对 X,Y,对于每对 X,Y,请你求出它们之间(不包括 X 和 Y)的所有奇数的和。
输入格式
第一行输入整数 N,表示共有 N 对测试数据。
接下来 N 行,每行输入一组整数 X 和 Y。
输出格式
每对 X,Y 输出一个占一行的奇数和。
数据范围
1≤N≤100,
−1000≤X,Y≤1000
输入样例:
7
4 5
13 10
6 4
3 3
3 5
3 4
3 8
输出样例:
0
11
5
0
0
0
12
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
while(n-->0){//n-->0表示循环n次
int x=sc.nextInt(),y=sc.nextInt();
if(x>y)
{
int t=x;
x=y;
y=t;
}
int sum=0;
for(int i=x+1;i<y;i++)
{
if(i%2!=0)
{
sum+=i;
}
}
System.out.println(sum);
}
}
}
16.简单斐波那契
以下数列 0 1 1 2 3 5 8 13 21 … 被称为斐波纳契数列。
这个数列从第 3 项开始,每一项都等于前两项之和。
输入一个整数 N,请你输出这个序列的前 N 项。
输入格式
一个整数 N。
输出格式
在一行中输出斐波那契数列的前 N 项,数字之间用空格隔开。
数据范围
0<N<46
输入样例:
5
输出样例:
0 1 1 2 3
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a=0,b=1;
for(int i=0;i<n;i++)//第一项 第二项 第三项 第四项
// a b a+b
// a b a+b
{
System.out.printf("%d ",a);//输出所有的a即为最终数列
int c=a+b;
a=b;
b=c;
}
}
}
17.数字序列和他的和
输入若干个整数对 M,N,对于每个数对,输出以这两个数为最大值和最小值的公差为 1 的等差数列。
注意,当输入整数对中,任意一个数为 0 或负整数时,立即停止输入,且该组数对无需作任何处理。
输入格式
输入共若干行,每行包含两个整数。
最后一行的两个整数中,至少有一个是非正整数。
输出格式
对于每组需作处理的数对,输出一个结果,每个结果占一行。
结果包含从最小值到最大值的数字序列以及数字序列各数字之和。
具体格式请参照输出样例。
数据范围
M,N≤100
输入样例:
2 5
6 3
5 0
输出样例:
2 3 4 5 Sum=14
3 4 5 6 Sum=18
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
while(true)
{
int n=sc.nextInt(),m=sc.nextInt();
if(n<=0||m<=0) break;
if(n>m)
{
int t=n;
n=m;
m=t;
}
int sum=0;
for(int i=n;i<=m;i++)
{
System.out.printf("%d ",i);//从n开始依次输出i直到i=m;
sum+=i;
}
System.out.printf("Sum=%d\n",sum);
}
}
}
18.完全数
一个整数,除了本身以外的其他所有约数的和如果等于该数,那么我们就称这个整数为完全数。
例如,6 就是一个完全数,因为它的除了本身以外的其他约数的和为 1+2+3=6。
现在,给定你 N 个整数,请你依次判断这些数是否是完全数。
输入格式
第一行包含整数 N,表示共有 N 个测试用例。
接下来 N 行,每行包含一个需要你进行判断的整数 X。
输出格式
每个测试用例输出一个结果,每个结果占一行。
如果测试数据是完全数,则输出 X is perfect,其中 X 是测试数据。
如果测试数据不是完全数,则输出 X is not perfect,其中 X 是测试数据。
数据范围
1≤N≤100,
1≤X≤108
输入样例:
3
6
5
28
输出样例:
6 is perfect
5 is not perfect
28 is perfect
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=0;i<n;i++)
{
int x=sc.nextInt();
int a=1;
for(int j=2;j<=x/j;j++)
{
if(x%j==0)
{
a+=j;
if(x/j!=j)
{
a+=x/j;
}
}
}
if(x!=1&&a==x) System.out.printf("%d is perfect\n",x);
else System.out.printf("%d is not perfect\n",x);
}
}
}
19.质数
一个大于 1 的自然数,如果除了 1 和它自身外,不能被其他自然数整除则称该数为质数。
例如 7 就是一个质数,因为它只能被 1 和 7 整除。
现在,给定你 N 个大于 1 的自然数,请你依次判断这些数是否是质数。
输入格式
第一行包含整数 N,表示共有 N 个测试数据。
接下来 N 行,每行包含一个自然数 X。
输出格式
每个测试用例输出一个结果,每个结果占一行。
如果测试数据是质数,则输出 X is prime,其中 X 是测试数据。
如果测试数据不是质数,则输出 X is not prime,其中 X 是测试数据。
数据范围
1≤N≤100,
1<X≤107
输入样例:
3
8
51
7
输出样例:
8 is not prime
51 is not prime
7 is prime
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=0;i<n;i++)
{
int x=sc.nextInt();
boolean a=true;
for(int j=2;j<=x/j;j++)
{
if(x%j==0)
{
a=false;
}
}
if (a) System.out.printf("%d is prime\n", x);
else System.out.printf("%d is not prime\n", x);
}
}
}
20.菱形
输入一个奇数 n,输出一个由 * 构成的 n 阶实心菱形。
输入格式
一个奇数 n。
输出格式
输出一个由 * 构成的 n 阶实心菱形。
具体格式参照输出样例。
数据范围
1≤n≤99
输入样例:
5
输出样例:
*
***
*****
***
*
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int x=n/2,y=n/2;//中心点的坐标
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
int d=Math.abs(i-x)+Math.abs(j-y);//到中心点坐标的距离
if(d<=n/2)
{
System.out.printf("*");
}
else
System.out.printf(" ");
}
System.out.printf("\n");
}
}
}