判断一个数是不是int类型:if ( x == (int) x)
sort()函数使用: 排序时,数组用多少开多少 不然会将未使用的数组部分,初始化为 0 ,排序时会排在前面
import java
.util
.Arrays
;
int x
[]=new int [n
];
x
[i
]输入
....
Arrays
.sort(x
);
字符串: 字符串之前需要取空行 取指定字符串时:
String x
=sc
.nextLine();
char y
=x
.charAt(i
);
字符串比较
String x
="pop";
if(x
.compareTo("top")==0)
目录
A - C语言实验——判断素数(循环结构)B - C语言实验——求阶乘(循环结构)C - 求绝对值最大值D - C语言实验——圆周率E - 简单计算F - 平方数G - C语言实验——for循环打印图形(循环结构)H - C/C++练习7---求某个范围内的所有素数I - C语言实验——打印菱形J - C语言实验——打印数字图形K - 做乘法L - 期末考试之分等级M - C语言实验——矩阵转置N - C语言实验——矩阵下三角元素之和O - 排序P - 期末考试之排名次Q - 冒泡排序中数据交换的次数S - 统计元音T - 回文串判定U - 字符统计2V - 传说中的数据结构X - C语言实验——打印金字塔
A - C语言实验——判断素数(循环结构)
import java
.util
.Scanner
;
public class Main {
public static void main(String
[] args
) {
Scanner sc
=new Scanner(System
.in
);
int n
=sc
.nextInt();
int f
=0;
if(n
==1) f
=1;
for(int i
=2;i
<n
;i
++)
{
if(n
%i
==0)
{
f
=1;
break;
}
}
if(f
==1) System
.out
.print("This is not a prime.\n");
else System
.out
.printf("This is a prime.\n");
}
}
B - C语言实验——求阶乘(循环结构)
import java
.util
.Scanner
;
public class Main {
public static void main(String
[] args
) {
Scanner sc
=new Scanner(System
.in
);
int n
=sc
.nextInt();
int s
=1;
for(int i
=1;i
<=n
;i
++)
{
s
=s
*i
;
}
System
.out
.printf("%d\n",s
);
}
}
C - 求绝对值最大值
import java
.util
.Scanner
;
public class Main {
public static void main(String
[] args
) {
Scanner sc
=new Scanner(System
.in
);
int n
=sc
.nextInt();
int s
=0;
for(int i
=1;i
<=n
;i
++)
{
int x
=sc
.nextInt();
if(Math
.abs(x
)>Math
.abs(s
)) s
=x
;
}
System
.out
.printf("%d\n",s
);
}
}
D - C语言实验——圆周率
import java
.util
.Scanner
;
public class Main {
public static void main(String
[] args
) {
Scanner sc
=new Scanner(System
.in
);
int n
=sc
.nextInt();
double s
=0;
for(int i
=1;i
<=n
;i
++)
{
s
=s
+1*1.0/(4*i
-3)-1*1.0/(4*i
-1);
}
System
.out
.printf("%.5f\n",s
*4);
}
}
E - 简单计算
import java
.util
.Scanner
;
public class Main {
public static void main(String
[] args
) {
Scanner sc
=new Scanner(System
.in
);
int n
=sc
.nextInt();
int min
=10000000;
int max
=0;
int s
=0;
for(int i
=1;i
<=n
;i
++)
{
int x
=sc
.nextInt();
s
=s
+x
;
if(max
<x
) max
=x
;
if(min
>x
) min
=x
;
}
System
.out
.printf("%d %d %d\n",max
,min
,s
/n
);
}
}
F - 平方数
import java
.util
.Scanner
;
public class Main {
public static void main(String
[] args
) {
Scanner sc
=new Scanner(System
.in
);
int t
=sc
.nextInt();
while(t
>0)
{
int l
,r
,s
=0;
t
--;
int x
=sc
.nextInt();
int y
=sc
.nextInt();
if(x
<y
)
{
l
=x
;r
=y
;
}
else
{
l
=y
;r
=x
;
}
for(int i
=l
;i
<=r
;i
++)
{
if(Math
.sqrt(i
)==(int) Math
.sqrt(i
))
s
=s
+i
;
}
System
.out
.println(s
);
}
}
}
G - C语言实验——for循环打印图形(循环结构)
这凑都能凑出来。。。
import java
.util
.Scanner
;
public class Main {
public static void main(String
[] args
) {
Scanner sc
=new Scanner(System
.in
);
for(int i
=1;i
<5;i
++)
{
for(int j
=1;j
<5-i
;j
++)
{
System
.out
.printf(" ");
}
for(int j
=1;j
<i
*2;j
++)
{
System
.out
.printf("*");
}
System
.out
.println();
}
for(int i
=3;i
>=1;i
--)
{
for(int j
=1;j
<5-i
;j
++)
{
System
.out
.printf(" ");
}
for(int j
=1;j
<i
*2;j
++)
{
System
.out
.printf("*");
}
System
.out
.println();
}
}
}
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
=sc
.nextInt();
int k
=0;
for(int i
=2;i
<n
;i
++)
{
int f
=0;
for(int j
=2;j
<i
;j
++)
{
if(i
%j
==0)
{
f
=1;
break;
}
}
if(f
==0)
{
k
++;
if(k
%10==0) System
.out
.printf("%d \n",i
);
else System
.out
.printf("%d ",i
);
}
}
}
}
I - C语言实验——打印菱形
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
++)
{
for(int j
=1;j
<=n
-i
;j
++)
{
System
.out
.printf(" ");
}
for(int j
=1;j
<i
*2;j
++)
{
System
.out
.printf("*");
}
System
.out
.println();
}
for(int i
=n
-1;i
>=1;i
--)
{
for(int j
=1;j
<=n
-i
;j
++)
{
System
.out
.printf(" ");
}
for(int j
=1;j
<i
*2;j
++)
{
System
.out
.printf("*");
}
System
.out
.println();
}
}
}
J - C语言实验——打印数字图形
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
++)
{
for(int j
=1;j
<=n
-i
;j
++)
{
System
.out
.printf(" ");
}
for(int j
=1;j
<=i
;j
++)
{
System
.out
.printf("%d",j
);
}
for(int j
=i
-1;j
>=1;j
--)
{
System
.out
.printf("%d",j
);
}
System
.out
.println();
}
for(int i
=n
-1;i
>=1;i
--)
{
for(int j
=1;j
<=n
-i
;j
++)
{
System
.out
.printf(" ");
}
for(int j
=1;j
<=i
;j
++)
{
System
.out
.printf("%d",j
);
}
for(int j
=i
-1;j
>=1;j
--)
{
System
.out
.printf("%d",j
);
}
System
.out
.println();
}
}
}
K - 做乘法
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
++)
{
System
.out
.printf("%d*%d=%d\n",n
,i
,n
*i
);
}
}
}
L - 期末考试之分等级
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
=0,c
=0,d
=0,e
=0;
for(int i
=1;i
<=n
;i
++)
{
int x
=sc
.nextInt();
if(x
>=90) a
++;
if(x
<90 && x
>=80) b
++;
if(x
>=70 && x
<80) c
++;
if(x
>=60 && x
<70) d
++;
if(x
<60) e
++;
}
System
.out
.printf("A %d\nB %d\nC %d\nD %d\nE %d\n",a
,b
,c
,d
,e
);
}
}
M - C语言实验——矩阵转置
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
[][]=new int[110][110];
for(int i
=1;i
<=n
;i
++)
{
for(int j
=1;j
<=n
;j
++)
{
x
[i
][j
]=sc
.nextInt();
}
}
for(int i
=1;i
<=n
;i
++)
{
for(int j
=n
;j
>=1;j
--)
{
if(j
==1) System
.out
.printf("%d",x
[j
][i
]);
else System
.out
.printf("%d ",x
[j
][i
]);
}
System
.out
.println();
}
}
}
N - C语言实验——矩阵下三角元素之和
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
[][]=new int [15][15];
for(int i
=1;i
<=n
;i
++)
{
for(int j
=1;j
<=n
;j
++)
{
x
[i
][j
]=sc
.nextInt();
}
}
int s
=0;
for(int i
=1;i
<=n
;i
++)
{
for(int j
=1;j
<=i
;j
++)
s
=s
+x
[i
][j
];
}
System
.out
.println(s
);
}
}
O - 排序
import java
.util
.Scanner
;
import java
.util
.Arrays
;
public class Main {
public static void main(String
[] args
) {
Scanner sc
=new Scanner(System
.in
);
int n
=sc
.nextInt();
int x
[]=new int [n
];
for(int i
=0;i
<n
;i
++)
{
x
[i
]=sc
.nextInt();
}
Arrays
.sort(x
);
for(int i
=0;i
<n
;i
++)
{
if(i
==n
-1) System
.out
.printf("%d",x
[i
]);
else System
.out
.printf("%d ",x
[i
]);
}
}
}
P - 期末考试之排名次
import java
.util
.Scanner
;
import java
.util
.Arrays
;
public class Main {
public static void main(String
[] args
) {
Scanner sc
=new Scanner(System
.in
);
int n
=sc
.nextInt();
int s
[]=new int [n
];
for(int i
=0;i
<n
;i
++)
{
int x
=sc
.nextInt();
int y
=sc
.nextInt();
int z
=sc
.nextInt();
s
[i
]=x
+y
+z
;
}
Arrays
.sort(s
);
for(int i
=n
-1;i
>=0;i
--)
{
System
.out
.println(s
[i
]);
}
}
}
Q - 冒泡排序中数据交换的次数
import java
.util
.Scanner
;
import java
.util
.Arrays
;
public class Main {
public static void main(String
[] args
) {
Scanner sc
=new Scanner(System
.in
);
int t
=sc
.nextInt();
while(t
>0)
{
int x
[]=new int [110];
t
--;
int n
=sc
.nextInt();
for(int i
=0;i
<n
;i
++)
{
x
[i
]=sc
.nextInt();
}
System
.out
.println(hanshu(x
,n
));
}
}
static int hanshu(int x
[],int n
)
{
int k
=0;
for(int i
=0;i
<n
;i
++)
{
for(int j
=0;j
<n
-i
-1;j
++)
{
if(x
[j
]>x
[j
+1])
{
int t
=x
[j
];
x
[j
]=x
[j
+1];
x
[j
+1]=t
;
k
++;
}
}
}
return k
;
}
}
S - 统计元音
字符串之前需要取空行 取指定字符串时
String x
=sc
.nextLine();
char y
=x
.charAt(i
);
import java
.util
.Scanner
;
public class Main {
public static void main(String
[] args
) {
Scanner sc
=new Scanner(System
.in
);
int n
=sc
.nextInt();
sc
.nextLine();
for(int k
=0;k
<n
;k
++)
{
int a
=0,e
=0,i
=0,o
=0,u
=0;
String x
=sc
.nextLine();
int m
=x
.length();
for(int j
=0;j
<m
;j
++)
{
char y
=x
.charAt(j
);
if(y
=='a') a
++;
else if (y
=='e') e
++;
else if(y
=='i') i
++;
else if(y
=='o') o
++;
else if(y
=='u') u
++;
}
System
.out
.printf("a:%d\n",a
);
System
.out
.printf("e:%d\n",e
);
System
.out
.printf("i:%d\n",i
);
System
.out
.printf("o:%d\n",o
);
System
.out
.printf("u:%d\n",u
);
System
.out
.println();
}
}
}
T - 回文串判定
import java
.util
.Scanner
;
public class Main {
public static void main(String
[] args
) {
Scanner sc
=new Scanner(System
.in
);
String x
=sc
.nextLine();
int m
=x
.length();
int f
=0;
for(int i
=0;i
<m
;i
++)
{
char a
=x
.charAt(i
);
char b
=x
.charAt(m
-i
-1);
if(a
!=b
)
{
f
=1;
break;
}
}
if(f
==1) System
.out
.printf("no\n");
else System
.out
.printf("yes\n");
}
}
U - 字符统计2
import java
.util
.Scanner
;
public class Main {
public static void main(String
[] args
) {
Scanner sc
=new Scanner(System
.in
);
while(sc
.hasNext())
{
int st
[]=new int [200];
String x
=sc
.nextLine();
int n
=x
.length();
for(int i
=0;i
<n
;i
++)
{
char a
=x
.charAt(i
);
st
[a
]++;
}
char b
=0;
int max
=0;
for(int i
='A';i
<='z';i
++)
{
if(st
[i
]>max
)
{
max
=st
[i
];
b
=(char)i
;
}
}
System
.out
.printf("%c %d\n",b
,max
);
}
}
}
V - 传说中的数据结构
import java
.util
.Scanner
;
import java
.util
.Arrays
;
import java
.math
.*
;
public class Main {
public static void main(String
[] args
) {
Scanner sc
=new Scanner(System
.in
);
while(sc
.hasNext())
{
int n
=sc
.nextInt();
int st
[]=new int[1010];
int tt
=0;
while(n
>0)
{
n
--;
String x
=sc
.next();
if(x
.compareTo("push")==0)
{
st
[tt
++]=sc
.nextInt();
}
else if(x
.compareTo("pop")==0)
{
if(tt
==0) System
.out
.println("error");
else tt
--;
}
else if(x
.compareTo("top")==0)
{
if(tt
==0) System
.out
.println("empty");
else System
.out
.println(st
[tt
-1]);
}
}
System
.out
.println();
}
}
}
X - C语言实验——打印金字塔
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
++)
{
for(int j
=1;j
<=(n
-i
)*2;j
++)
{
System
.out
.print(" ");
}
for(int j
=1;j
<=i
;j
++)
{
if(j
==1)
System
.out
.printf("%d",j
);
else System
.out
.printf(" %d",j
);
}
for(int j
=i
-1;j
>=1;j
--)
{
System
.out
.printf(" %d",j
);
}
System
.out
.println();
}
}
}