爱奇艺编程90%,100%,60%,想知道错哪里了,代码如下
第一题:
import java.util.Scanner;
public class First {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int x1,k1,x2,k2;
x1 = scanner.nextInt();
k1 = scanner.nextInt();
x2 = scanner.nextInt();
k2 = scanner.nextInt();
int t = func(x1, k1, x2, k2);
if(t == 1)
System.out.println("Greater");
else if(t == -1)
System.out.println("Less");
else
System.out.println("Equal");
}
public static int func(int a,int b,int c,int d){
int flag = 0;
StringBuilder sBuilder1 = new StringBuilder();
StringBuilder sBuilder2 = new StringBuilder();
for(int i=0;i<b;i++){
sBuilder1.append(String.valueOf(a));
}
for(int i=0;i<d;i++){
sBuilder2.append(String.valueOf(c));
}
if(sBuilder1.length()>sBuilder2.length())
return 1;
else if(sBuilder1.length()<sBuilder2.length())
return -1;
else{
int len = sBuilder1.length();
for(int i=0;i<len;i++){
if((int)sBuilder1.charAt(i)>(int)sBuilder2.charAt(i)){
flag = 1;
break;
}
else if((int)sBuilder1.charAt(i)<(int)sBuilder2.charAt(i)){
flag = -1;
break;
}
}
if(flag == 1)
return 1;
else if(flag == 1)
return -1;
else
return 0;
}
}
}
第二题:
import java.util.Scanner;
public class Second {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
char[] arr = str.toCharArray();
Stack<Character> stack = new Stack<>();
int cnt = 0, max =0;
for(int i=0;i<arr.length;i++){
if(arr[i] == '('){
stack.push(arr[i]);
}
if(arr[i] == ')' && stack.peek() == '('){
if(stack.size() > max)
max = stack.size();
stack.pop();
}
}
System.out.println(max);
}
}
第三题(超时):
import java.util.Scanner;
public class Third {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
int cnt = 0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
double tmp = Math.sqrt(i)+Math.sqrt(j);
double t = tmp*tmp;
if(Math.abs(t - Math.ceil(t))<0.000001 || Math.abs(t - Math.floor(t))<0.000001){
cnt += 1;
}
}
}
System.out.println(cnt);
}
}