快手:笔试题(版本号比较,平方和为1,合并两个流)
1. 版本号比较
方法1,没有AC
public class A19 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int m = sc.nextInt(); sc.nextLine(); String[][] strs = new String[m][2]; boolean[] result = new boolean[m]; for (int i = 0; i < m; i++) { strs[i] = sc.nextLine().split(" "); int c1 = strs[i][0].split("\\.").length; int c2 = strs[i][1].split("\\.").length; while(c1<c2){ strs[i][0]+=".0"; c1++; } while(c1>c2){ strs[i][1]+=".0"; c2++; } if(strs[i][0].compareTo(strs[i][1])<0){ result[i]= true; } } for (int i = 0; i < m; i++) { System.out.println(result[i]); } } }
方法2 没有ac
package test; import java.util.Scanner; public class A17 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int m = sc.nextInt(); sc.nextLine(); String[][] strs = new String[m][2]; for (int i = 0; i < m; i++) { strs[i] = sc.nextLine().split(" "); } boolean[] result = new boolean[m]; for (int i = 0; i < m; i++) { String[] str1 = strs[i][0].split("\\."); String[] str2 = strs[i][1].split("\\."); int[] val1 = new int[4]; int[] val2 = new int[4]; for (int j = 0; j < str1.length; j++) { val1[j] = Integer.valueOf(str1[j]); } for (int j = 0; j < str2.length; j++) { val2[j] = Integer.valueOf(str2[j]); } for (int j = 0; j < 4; j++) { if(val1[j]<val2[j]){ result[i]=true; break; } } } for (int i = 0; i < m; i++) { System.out.println(result[i]); } } }
方法3 AC
作者:jerryzhuo 链接:https://www.nowcoder.com/discuss/233405?type=post&order=time&pos=&page=1 来源:牛客网 import java.util.Scanner; /** * 版本问题 */ public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int m = in.nextInt(); String[] op1 = new String[m]; String[] op2 = new String[m]; for (int i = 0; i < m; i++) { op1[i] = in.next(); op2[i] = in.next(); } Main main = new Main(); main.outputResult(op1, op2); } private void outputResult(String[] op1, String[] op2) { boolean[] result = new boolean[op1.length]; for (int i = 0; i < op1.length; i++) { String[] oneArray = op1[i].trim().split("\\."); String[] twoArray = op2[i].trim().split("\\."); boolean isOk = true; int pointerOne = 0; int pointerTwo = 0; while (pointerOne < oneArray.length && pointerTwo < twoArray.length) { int compare = Integer.parseInt(oneArray[pointerOne]) - Integer.parseInt(twoArray[pointerTwo]); if (compare < 0) { break; } else if (compare > 0) { isOk = false; break; } else if (pointerOne == oneArray.length - 1) { pointerTwo++; while (pointerTwo < twoArray.length && twoArray[pointerTwo].equals("0")) { pointerTwo++; } if (pointerTwo == twoArray.length) { isOk = false; } } else if (pointerTwo == twoArray.length - 1) { isOk = false; break; } pointerOne++; pointerTwo++; } result[i] = isOk; } for (boolean value : result) { System.out.println(value); } } }
2. 平方和为1
4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 ,循环了,return false
AC
方法1
import java.util.Scanner; public class A20 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int m = sc.nextInt(); int[] n = new int[m]; for (int i = 0; i < m; i++) { n[i] = sc.nextInt(); } for (int i = 0; i < m; i++) { long result = getresult(n[i]); boolean r = true; while(result!=1){ result = getresult(result); if(result==n[i]){ r=false; break; } } System.out.println(r); } } public static long getresult(long result2){ long result = 0; while(result2!=0){ long digit = result2 % 10; result = result+ digit*digit; result2 = result2/10; } return result; } }
方法2
import java.util.Scanner; public class A20 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int m = sc.nextInt(); int[] n = new int[m]; for (int i = 0; i < m; i++) { n[i] = sc.nextInt(); } for (int i = 0; i < m; i++) { long result = getresult(n[i]); int time =0; while(result!=1 && time<1000){ result = getresult(result); time++; } if(result==1){ System.out.println(true); }else{ System.out.println(false); } } } public static long getresult(long result2){ long result = 0; while(result2!=0){ long digit = result2 % 10; result = result+ digit*digit; result2 = result2/10; } return result; } }
3. 合并两个流
AC
import java.util.Scanner; public class A18 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String[] str1 = sc.nextLine().split(" "); String[] str2 = sc.nextLine().split(" "); int len1 = str1.length; int len2 = str2.length; String[] result = new String[len1+len2]; int position = 0; int index2 = 0; for (int i = 0; i < len1; i++) { if((i+1)%4==0 && index2<len2){ result[position++]=str1[i]; result[position++]=str2[index2++]; }else{ result[position++]=str1[i]; } } while(index2<len2){ result[position++]=str2[index2++]; } for (int i = 0; i < (len1+len2); i++) { System.out.print(result[i]); if(i!=(len1+len2-1)){ System.out.print(" "); } } } }