import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
String a = scanner.next();
int n = scanner.nextInt();
String s = a;
BigInteger sum = BigInteger.ZERO;
for (int i = 1; i <= n; i++) {
sum=sum.add(new BigInteger(s));
s+=a;
}
System.out.println(sum.toString());
}
}
} import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int a = sc.nextInt();
int n = sc.nextInt();
StringBuilder sb = new StringBuilder();
//使用re接收余数
int re;
//使用lt接收进位
int lt=0;
for(int i=0;i<n;i++){
re=((n-i)*a+lt)%10;
lt=((n-i)*a+lt)/10;
sb.append(re);
}
//将所得数倒置即为所求
System.out.print(sb.reverse());
}
}
}
因为数据大到能达到100位数,使用字符拼接较为理想,性能较快,而且几乎不用考虑范围,从个位开始拼接,
运行时间:49ms
占用内存:11956k
从Long转到BigInteger的路过
再来看看大家的讨论,用Java通过的基本都用了该类~
使用BigInteger和StringBuilder,代码很好理解
import java.math.BigInteger;
import java.util.Scanner;
/**
* @author Allen_Hua
* @create_time 创建时间:May 12, 2018 8:31:26 PM 类说明
*/
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
int a = scan.nextInt();
int n = scan.nextInt();
BigInteger sum = new BigInteger("0");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append(a);
BigInteger temp = new BigInteger(sb.toString());
sum = sum.add(temp);
}
System.out.println(sum);
}
}
}