import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
System.out.print("L");
for(int i=0;i<t;i++) {
System.out.print("o");
}
System.out.print("ng");
sc.close();
}
}
按题目要求,按部就班的写,(题目仅对o的个数有额外要求)。
import java.util.Scanner;
public class text1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
sc.nextLine();
for(int i=0;i<t;i++) {
String a=sc.nextLine();
a=a.toLowerCase();
if(a.equals("yes")) {
System.out.println("YES");
}
else {System.out.println("NO");}
}
}
}
按题目要求,按部就班的写(先将输入都花成小写降低复杂性在比较)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
sc.nextLine();
for(int i=0;i<t;i++) {
String b=sc.nextLine();
int a=b.charAt(b.length()-1)-'0';
if(a%2==0) {
System.out.println("even");
}
else {System.out.println("odd");}
}sc.close();
}
}
只比较最后一个数是否为偶数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++) {
int n=sc.nextInt();
int m=sc.nextInt();
int A=0;
int B=0;
int C=0;
int D=0;
int E=0;
int F=0;
int G=0;
int q=0;
sc.nextLine();
String a=sc.nextLine();
for(int o=0;o<n;o++) {
switch(a.charAt(o)) {
case('A'):A++;break;
case('B'):B++;break;
case('C'):C++;break;
case('D'):D++;break;
case('E'):E++;break;
case('F'):F++;break;
case('G'):G++;break;
}
}
if(A<m) {q+=m-A;}
if(B<m) {q+=m-B;}
if(C<m) {q+=m-C;}
if(D<m) {q+=m-D;}
if(E<m) {q+=m-E;}
if(F<m) {q+=m-F;}
if(G<m) {q+=m-G;}
System.out.println(q);
}
sc.close();
}
}
统计各种难度题目出现的次数,和题目要求比较
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
int k=sc.nextInt();
int y=0;
for(int o=0;o<m;o++) {
int x=0;
for(int i=0;i<n;i++) {
int t=sc.nextInt();
if(t==k) {x++;}
}
if(x>=(n+1)/2) {y++;}
}
if(y>=(m+1)/2) {
System.out.println("YES");
}
else {System.out.println("NO");
}
sc.close();
}
}
同过一个循环来求一天内是否同意,再来一个循环检测是否合理
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N, Q;
String S;
N = sc.nextInt();
S = sc.next();
Q = sc.nextInt();
char[] c = new char[26];
for (int i = 0; i < 26; i++) {
c[i] = (char) ('a' + i);
}
while (Q-- > 0) {
char from = sc.next().charAt(0);
char to = sc.next().charAt(0);
for (int i = 0; i < 26; i++) {
if (c[i] == from) {
c[i] = to;
}
}
}
char[] sArray = S.toCharArray();
for (int i = 0; i < sArray.length; i++) {
if (sArray[i] >= 'a' && sArray[i] <= 'z') {
sArray[i] = c[sArray[i] - 'a'];
}
}
System.out.println(new String(sArray));
}
}
建立数组,先对操作历程进行处理,再处理输入的字符串
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String[] firstLine = br.readLine().split(" ");
int a = Integer.parseInt(firstLine[0]);
int step = Integer.parseInt(firstLine[1]);
int[][] array = new int[a][a];
for (int i = 0; i < a; i++) {
String[] row = br.readLine().split(" ");
for (int j = 0; j < a; j++) {
array[i][j] = Integer.parseInt(row[j]);
}
}
int[] colMap = new int[a];
for (int i = 0; i < a; i++) {
colMap[i] = i;
}
for (int s = 0; s < step; s++) {
String[] opLine = br.readLine().split(" ");
int op = Integer.parseInt(opLine[0]);
int x = Integer.parseInt(opLine[1]);
int y = Integer.parseInt(opLine[2]);
if (op == 1) {
int[] temp = array[x - 1];
array[x - 1] = array[y - 1];
array[y - 1] = temp;
} else if (op == 0) {
int tempCol = colMap[x - 1];
colMap[x - 1] = colMap[y - 1];
colMap[y - 1] = tempCol;
}
}
int[][] result = new int[a][a];
for (int i = 0; i < a; i++) {
for (int j = 0; j < a; j++) {
result[i][j] = array[i][colMap[j]];
}
}
for (int i = 0; i < a; i++) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < a; j++) {
sb.append(result[i][j]).append(" ");
}
bw.write(sb.toString().trim());
bw.newLine();
}
bw.flush();
br.close();
bw.close();
}
}
重点处理要得到的矩阵,同时用行映射和列映射,最后通过colMap的映射关系一次性构造最终结果矩阵。
学习总结:
时间复杂度的大小是代码运行速度的反应。如何改变算法,实现更小的时间复杂度简化代码。算法题目的与众不同的点让我刻骨铭心,总是修改自己的代码避免超时。C++还在进行初步的学习,auto的命名方式和以往的接触到的相比具有极大的便捷性。