选题为java语法基础课每章的最后一题
1, 655. 天数转换
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
System.out.printf("%d ano(s)\n",n/365);
n%=365;
System.out.printf("%d mes(es)\n",n/30);
n%=30;
System.out.printf("%d dia(s)\n",n);
}
}
2, 661. 平均数3
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
double a=sc.nextDouble(),b=sc.nextDouble(),c=sc.nextDouble(),d=sc.nextDouble();
double x=(a*2+b*3+c*4+d)/10.0;
System.out.printf("Media: %.1f\n",x);
if(x>=7.0){
System.out.printf("Aluno aprovado.\n");
}
else if(x<5.0){
System.out.printf("Aluno reprovado.\n");
}
else{
System.out.printf("Aluno em exame.\n");
double y=sc.nextDouble();
System.out.printf("Nota do exame: %.1f\n",y);
double z=(x+y)/2.0;
if(z>=5.0){
System.out.printf("Aluno aprovado.\n");
}
else{
System.out.printf("Aluno reprovado.\n");
}
System.out.printf("Media final: %.1f\n",z);
}
}
}
3, 727. 菱形
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int cx=n/2,cy=n/2;
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
int d=Math.abs(i-cx)+Math.abs(j-cy);
if(d<=n/2){
System.out.print("*");
}
else{
System.out.print(" ");
}
}
System.out.printf("\n");
}
}
}
4, 756. 蛇形矩阵
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(),m=sc.nextInt();
int[] dx={-1,0,1,0},dy={0,1,0,-1};
int[][] res=new int[n][m];
int x=0,y=0,d=1;
for(int i=1;i<=n*m;++i){
res[x][y]=i;
int a=x+dx[d],b=y+dy[d];
if(a<0||a>=n||b<0||b>=m||res[a][b]>0){
d=(d+1)%4;
a=x+dx[d];
b=y+dy[d];
}
x=a;
y=b;
}
for(int[] row:res){
for(int val:row){
System.out.printf("%d ",val);
}
System.out.printf("\n");
}
}
}
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
while(n!=0){
String[] s=new String[n];
for(int i=0;i<n;++i){
s[i]=sc.next();
}
int idx=0,len=114514;
for(int i=0;i<n;++i){
if(s[i].length()<len){
len=s[i].length();
idx=i;
}
}
String sub="";
for(int i=1;i<=len;++i){
String str=s[idx].substring(len-i,len);
boolean f=true;
for(int j=0;j<n;++j){
if(!s[j].endsWith(str)){
f=false;
break;
}
}
if(f) sub=str;
}
System.out.println(sub);
n=sc.nextInt();
}
}
}
6, 823. 排列
import java.util.*;
import java.io.*;
public class Main{
private static int[] path;
private static boolean[] st;
private static int n;
private static BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
private static void dfs(int u) throws Exception{
if(u==n){
for(int i=0;i<n;++i){
bw.write(path[i]+1+" ");
}
bw.write("\n");
}else{
for(int i=0;i<n;++i){
if(!st[i]){
path[u]=i;
st[i]=true;
dfs(u+1);
st[i]=false;
}
}
}
}
public static void main(String[] args) throws Exception{
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
path=new int[n];
st=new boolean[n];
dfs(0);
bw.flush();
}
}
7, 862. 三元组排序
import java.util.*;
class Data implements Comparable<Data>{
int a;
double b;
String c;
Data(int a,double b,String c){
this.a=a;
this.b=b;
this.c=c;
}
public int compareTo(Data t){
return a-t.a;
}
}
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
Data[] q=new Data[n];
for(int i=0;i<n;++i){
q[i]=new Data(sc.nextInt(),sc.nextDouble(),sc.next());
}
Arrays.sort(q);
for(Data data:q){
System.out.printf("%d %.2f %s\n",data.a,data.b,data.c);
}
}
}
8, 136. 邻值查找
import java.util.*;
import java.io.*;
public class Main{
private static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
private static BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
private static int n;
private static int[] a;
private static String[] strs;
public static void main(String[] args) throws Exception{
n=Integer.parseInt(br.readLine());
a=new int[n];
strs=br.readLine().split(" ");
for(int i=0;i<n;++i){
a[i]=Integer.parseInt(strs[i]);
}
TreeMap<Integer,Integer> m=new TreeMap<>();
m.put(a[0],0);
for(int i=1;i<n;++i){
Map.Entry<Integer,Integer> y=m.ceilingEntry(a[i]),x=m.floorEntry(a[i]);
if(x==null){
bw.write(Math.abs(y.getKey()-a[i])+" "+(y.getValue()+1)+"\n");
}else if(y==null){
bw.write(Math.abs(x.getKey()-a[i])+" "+(x.getValue()+1)+"\n");
}else if(Math.abs(y.getKey()-a[i])>=Math.abs(x.getKey()-a[i])){
bw.write(Math.abs(x.getKey()-a[i])+" "+(x.getValue()+1)+"\n");
}else{
bw.write(Math.abs(y.getKey()-a[i])+" "+(y.getValue()+1)+"\n");
}
m.put(a[i],i);
}
bw.flush();
}
}