转载请注明出处:https://blog.csdn.net/Mercury_Lc/article/details/82767275 作者:Mercury_Lc SDUT Java基础语法练习1
代码语言:javascript复制A C语言实验——判断素数(循环结构)(SDUT 1122)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
n = sc.nextInt();
if (n == 1)
System.out.println("This is not a prime.");
else {
int flag = 1;
for (int i = 2; i < n; i ) {
if (n % i == 0) {
flag = 0;
break;
}
}
if (flag == 1)
System.out.println("This is a prime.");
else
System.out.println("This is not a prime.");
}
}
}
代码语言:javascript复制B C语言实验——求阶乘(循环结构)(SDUT 1123)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n, sum = 1;
n = sc.nextInt();
for (int i = 1; i <= n; i )
sum *= i;
System.out.println(sum);
}
}
代码语言:javascript复制C 求绝对值最大值(SDUT 1147)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n, x;
int ans = 0, max1, max2 = 0;
n = sc.nextInt();
for (int i = 0; i < n; i ) {
x = sc.nextInt();
if (x <= 0)
max1 = -x;
else
max1 = x;
if (max2 < max1) {
max2 = max1;
ans = x;
}
}
System.out.println(ans);
}
}
代码语言:javascript复制D C语言实验——圆周率(SDUT 1193)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
double ans = 0, x, y;
n = sc.nextInt();
for (int i = 1; i <= n; i ) {
x = (double) 1 / (4 * i - 3.0);
y = (double) 1 / (4 * i - 1.0);
ans = x - y;
}
ans *= 4.0;
System.out.printf("%.5fn", ans);
}
}
代码语言:javascript复制E 简单计算(SDUT 2250)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n, x, min, max, ave;
min = 0x3f3f3f3f;
max = ave = 0;
n = sc.nextInt();
for (int i = 0; i < n; i ) {
x = sc.nextInt();
if (x > max) {
max = x;
}
if (x < min) {
min = x;
}
ave = x;
}
ave /= n;
System.out.println(max " " min " " ave);
}
}
代码语言:javascript复制F 平方数(SDUT 3439)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t, n, m, a, b;
t = sc.nextInt();
for (int cas = 0; cas < t; cas ) {
a = sc.nextInt(); // n和m 的大小不确定
b = sc.nextInt();
n = Math.min(a, b);
m = Math.max(a, b);
int sum = 0;
for (int i = n; i <= m; i ) {
double x = Math.sqrt((double) i);
int xx = (int) x;
if (xx * xx == i) {
sum = i;
}
}
System.out.println(sum);
}
}
}
代码语言:javascript复制G C语言实验——for循环打印图形(循环结构)(SDUT 1121)
import java.util.Scanner; // for循环也很简单,和c 没什么区别。这里就是凑个完整QWQ。
public class Main {
public static void main(String[] args) {
System.out
.println(" *rn" " ***rn" " *****rn" "*******rn" " *****rn" " ***rn" " *");
}
}
代码语言:javascript复制H C/C 练习7---求某个范围内的所有素数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n,num = 0;
n = sc.nextInt();
for(int i = 0; i < n; i )
{
if(prime(i) == 1) {
num ;
if(num % 10 == 0) System.out.printf("%d n",i);
else System.out.printf("%d ", i);
}
}
}
public static int prime(int n) {
if(n == 1 || n == 0) return 0;
for(int i = 2; i <= (int)Math.sqrt((double)n); i ){
if(n % i == 0) return 0;
}
return 1;
}
}