输入两个整数a(大于等于1且小于等于9)和n(大于等于1且小于等于80),编程求得并输出下面等式的值:
例如:若输入的a为5,n为6,则要计算下面公式的值:
555555-55555-5555-555-55-5。
【输入形式】从标准输入读入整数a和n,两者之间以一个空格分隔。
【输出形式】在标准输出上输出公式的计算结果。
【样例1输入】5 6
【样例1输出】493830
【样例1说明】输入的a为5,n为6,按照上述公式计算的结果为493830。
【样例2输入】5 20
【样例2输出】49382716049382716060
【样例2说明】输入的a为5,n为20,按照上述公式计算的结果为49382716049382716060。
【评分标准】该程序要求输出求得的公式的值。
参考代码:
import java.math.BigInteger;
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int n = sc.nextInt();
String str = a+"";
String s = a+"";
BigInteger result = new BigInteger(str);
for(int i=1;i<=n-2;i++){
str = str+s;
BigInteger num = new BigInteger(str);
result = result.add(num);
}
str = str+s;
BigInteger numb = new BigInteger(str);
numb = numb.subtract(result);
System.out.println(numb);
}
}