第一题
655.天数转换
ac代码
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);
}
}
第二题
611.平均数3
ac代码
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 me = a*0.2+b*0.3+c*0.4+d*0.1;
System.out.printf("Media: %.1f\n",me);
if(me>=7.0){
System.out.printf("Aluno aprovado.\n");
}
else if(me<5.0){
System.out.printf("Aluno reprovado.\n");
}
else{
double e = sc.nextDouble();
System.out.printf("Aluno em exame.\n");
System.out.printf("Nota do exame: %.1f\n",e);
me = (me+e)/2;
if(me>=5.0){
System.out.printf("Aluno aprovado.\n");
}
else {
System.out.printf("Aluno reprovado.\n");
}
System.out.printf("Media final: %.1f\n",me);
}
}
}
第三题
727.菱形
ac代码
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = (n-1)/2;
for(int i = 1;i<=n/2;i++){
char c = ' ';
for(int j = 1;j<=(n/2)+i;j++){
if(j==k+1){
c = '*';
}
System.out.printf("%c",c);
}
k--;
System.out.println("");
}
for(int i = 1;i<=n;i++){
System.out.printf("*");
}
System.out.printf("\n");
k = 2;
for(int i = 1;i<=n/2;i++){
char c = ' ';
for(int j = 1;j<=((n+1)/2)+n/2-i;j++){
if(j==k){
c = '*';
}
System.out.printf("%c",c);
}
k++;
System.out.println("");
}
}
}
第四题
756.蛇形矩阵
ac代码
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 cnt = 1;
int i = 0,j = 0;
int [][] a = new int[n][m];
int st = 1;
while (cnt<=n*m){
if(i<n&&j<m&&a[i][j]==0) {
a[i][j] = cnt++;
}
if(st==1) {
if(j==m-1||a[i][j+1]!=0){
st++;
}
else j++;
}
else if(st==2)
{
if(i==n-1||a[i+1][j]!=0){
st++;
}
else i++;
}
else if(st==3) {
if(j==0||a[i][j-1]!=0){
st++;
}
else j--;
}
else if(st==4)
{
if(i==0||a[i-1][j]!=0){
st = 1;
}
else i--;
}
}
for(int k = 0;k<n;k++)
{
for(int t = 0;t<m;t++)
{
System.out.printf("%d ",a[k][t]);
}
System.out.printf("\n");
}
}
}
第五题
779.最长公共字符串后缀
ac代码
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 = 1000000;
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();
}
}
}
第六题
823.排序
ac代码
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();
}
}
第七题
862.三元组排序
ac代码
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);
}
}
}
第八题
136.邻值查询
ac代码
import java.util.*;
import java.io.*;
public class Main{
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
static int n;
static int[] a;
static String[] strs;
public static void main(String[] args)throws Exception{
n = Integer.parseInt(in.readLine());
a = new int[n];
strs = in.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]);
Map.Entry<Integer,Integer>x = m.floorEntry(a[i]);
if(x==null){
out.write(Math.abs(y.getKey()-a[i])+" "+(y.getValue()+1)+"\n");
}
else if(y==null){
out.write(Math.abs(x.getKey()-a[i])+" "+(x.getValue()+1)+"\n");
}
else if(Math.abs(x.getKey()-a[i])<=Math.abs(y.getKey()-a[i])){
out.write(Math.abs(x.getKey()-a[i])+" "+(x.getValue()+1)+"\n");
}
else{
out.write(Math.abs(y.getKey()-a[i])+" "+(y.getValue()+1)+"\n");
}
m.put(a[i],i);
}
out.flush();
}
}