网易游戏研发笔试求解-由x进制y进制求整数n

不知道哪里出错了,有人帮我看看吗,报错在输入哪里,我加了trim(),然而还有错
import java.util.Scanner;

public class main {  public static void main(String[] args) {  Scanner sc = new Scanner(System.in);  int t = sc.nextInt();  String[] str = new String[t];  // StringBuffer[] sourceStr = new StringBuffer[t];  for (int i = 0; i < t; i++) {  str[i] = sc.nextLine().trim();  }  ans1(str);  }  public static void ans1(String[] str) {  int x=0;  int y=0;  StringBuffer z;  int[] n = new int[str.length];  for (int i = 0; i < str.length; i++) {  String[] s = str[i].trim().split(" ");  x=Integer.parseInt(s[0].trim());  y=Integer.parseInt(s[1].trim());  z=new StringBuffer(s[2]);  n[i] =ans(x,y,z);  System.out.println(n[i]);  }    }  public static int ans(int x, int y, StringBuffer z) {  int n = 0;  int xn = 0;  int yn = 0; // xn为x进制所占位数  char[] zx, zy; // zx为x进制的数的char[]数组  if (x > y) {  int m = (int) Math.ceil(x / y);  xn = z.length() / (m + 1) + 1;  zx = z.substring(0, xn - 1).toString().toCharArray();  for (int i = 0; i < xn; i++) {  n += zx[i] * (x ^ (xn - i - 1));  }  } else if (x < y) {  int m = (int) Math.ceil(y / x);  yn = z.length() / (m + 1) + 1;  // zx= z.substring(0,xn-1).toString().toCharArray();  zy = z.substring(z.length() - yn).toString().toCharArray();  for (int i = z.length()-yn; i < yn; i++) {  n += zy[i] * (x ^ (yn - i - 1));  }  }  return n;  }
}

#笔试题目#
全部评论
package ni; import java.util.Scanner; public class main {     public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         int t = sc.nextInt();         String[] str = new String[t];         // StringBuffer[] sourceStr = new StringBuffer[t];         for (int i = 0; i < t; i++) {             str[i] = sc.nextLine().trim();         }         ans1(str);     }     public static void ans1(String[] str) {         int x=0;         int y=0;         StringBuffer z;         int[] n = new int[str.length];         for (int i = 0; i < str.length; i++) {             String[] s = str[i].trim().split(" ");             x=Integer.parseInt(s[0].trim());             y=Integer.parseInt(s[1].trim());             z=new StringBuffer(s[2]);             n[i] =ans(x,y,z);             System.out.println(n[i]);         }              }     public static int ans(int x, int y, StringBuffer z) {         int n = 0;         int xn = 0;         int yn = 0; // xn为x进制所占位数         char[] zx, zy; // zx为x进制的数的char[]数组         if (x > y) {             int m = (int) Math.ceil(x / y);             xn = z.length() / (m + 1) + 1;             zx = z.substring(0, xn - 1).toString().toCharArray();             for (int i = 0; i < xn; i++) {                 n += zx[i] * (x ^ (xn - i - 1));             }         } else if (x < y) {             int m = (int) Math.ceil(y / x);             yn = z.length() / (m + 1) + 1;             // zx= z.substring(0,xn-1).toString().toCharArray();             zy = z.substring(z.length() - yn).toString().toCharArray();             for (int i = z.length()-yn; i < yn; i++) {                 n += zy[i] * (x ^ (yn - i - 1));             }         }         return n;     } }
点赞 回复 分享
发布于 2018-09-08 22:17

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务