SDKD《Java程序设计》 软件18-1,3 实验1
求最大值
考点
基本输入输出,分支语句(if-else)
代码
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
int a, b;
Scanner cin = new Scanner(System.in);
a = cin.nextInt();
b = cin.nextInt();
System.out.println(a >= b ? a : b);
}
}
判断闰年
考点
基本输入输出,分支语句(if-else)
代码
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
int year;
Scanner cin = new Scanner(System.in);
year = cin.nextInt();
boolean yes = false;
if (year % 400 == 0)
yes = true;
else if (year % 4 == 0 && year % 100 != 0)
yes = true;
System.out.println(yes ? "yes" : "no");
}
}
水仙花数
考点
基本输入输出,分支语句(if-else),循环语句
代码
打表法
import java.io.*;
import java.util.*;
public class Main {
static final Scanner cin = new Scanner(System.in);
public static void main(String[] args) {
int[][] ans = new int[5][];
ans[0] = new int[] { 153, 370, 371, 407 };
ans[1] = new int[] { 1634, 8208, 9474 };
ans[2] = new int[] { 54748, 92727, 93084 };
ans[3] = new int[] { 548834 };
ans[4] = new int[] { 1741725, 4210818, 9800817, 9926315 };
int n = cin.nextInt() - 3;
for (int i = 0; i < ans[n].length; i++) {
System.out.println(ans[n][i]);
}
}
}
循环法
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
long N = cin.nextLong();
long _start = 1;
for (long i = 0; i < N - 1; i++)
_start *= 10;
long _end = _start * 10 - 1;
for (long i = _start; i <= _end; i++) {
long tmp = 0, cur = i;
while (true) {
if (cur == 0)
break;
long m = cur % 10, ans = 1;
for (int j = 0; j < N; j++)
ans *= m;
tmp += ans;
cur /= 10;
}
if (tmp == i)
System.out.println(tmp);
}
}
}
基本输入
考点
基本输入输出
代码
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while(cin.hasNext()) //还有下一个输入
{
int a = cin.nextInt();
int b = cin.nextInt();
System.out.println(a+b);
}
}
}