1013 problem: [introductory programming] Sn the sum formula
Time limit: 1Sec Memory Limit: 128MB submission: 8018 Resolution: 5541
Seeking Sn = a + aa + aaa + ... + aa ... aaa (with n a) of the value, where a is a number of 2. For example, n = 5 when = 2 + 22 + 222 + 2222 + 22222, n input from the keyboard.
n
The value of Sn
5
24690
Analysis:
When For n = 3, i is 1 ~ n
i=1 2*10^i-1=2
i=2 2*10^i-1+2*10^i-2=22
i=3 2*10^i-1+2*10^i-2+2*10^i-3=222
......
import java.util.Scanner;
import java.math.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int sum=0;
int n = in.nextInt();
for(int i =1;i<=n;i++){
sum+=fun(i);
}
System.out.println(sum);
}
public static int fun(int m){
int num=0;
for(int i=0;i<m;i++){
num+=2*Math.pow(10, i);
}
return num;
}
}