如果我的字符串包含 100 位或 900 位数字,我如何将其解析为整数值 Java。
public static void main(String...args){
long answer1=-1,answer2=-1;
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();//length
int s = sc.nextInt();//sum
String startNmbr="1",endNmbr="9";
int startRange=1,endRange=9;
for(int i=1;i<m;i++){
startNmbr+="0";endNmbr+="9";endRange+=9;
}
long end = Long.parseLong(endNmbr);
for(long start=Long.parseLong(startNmbr);start<=end;start++){
if(answer1==-1 && countNmbr(start)==s){
answer1=start;
}
if(answer1!=-1 && countNmbr(start)==s){
answer2=start;
}
}
System.out.println(answer1+" "+answer2);
}
public static int countNmbr(long a){
int answer=0;
String str = a+"";
for(int i=0;i<str.length();i++){
answer+=(str.charAt(i)-'0');
}
return answer;
}
我的输入是 100,100;
我必须返回长度==m且总和==s的最小值和最大值;
整数的MAX_VALUE
是 2,147,483,647。这是 10 位数字。
您可以使用BigInteger - 无限制,但这取决于你的内存和系统架构。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)