(URI) Feynman - Solution
Once we know that the entry is between 1 and N (N <= 100), we can find the result for every number between 1 and N and store it in an array. Then, for every entry, we only need to call for the array using the entry as the position (e.g.: v[entry]).
import java.io.*;
class Main {
public static final int MAX = 100;
public static int reader(BufferedReader br) throws NumberFormatException, IOException {
int n;
int resp = 0;
while (true) {
n = br.read();
if (n >= '0' && n <= '9') {
break;
}
}
while (true) {
resp = resp*10 + n-'0';
n = br.read();
if (n < '0' || n > '9') {
break;
}
}
return resp;
}
public static void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int[] v = new int[MAX+1];
int result = 0;
for (int i = 1; i <= MAX; i++) {
result += i*i;
v[i] = result;
}
int num = reader(br);
while (num != 0) {
bw.write(v[num] + "\n");
num = reader(br);
}
bw.flush();
bw.close();
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
import java.io.*;
class Main {
public static final int MAX = 100;
public static int reader(BufferedReader br) throws NumberFormatException, IOException {
int n;
int resp = 0;
while (true) {
n = br.read();
if (n >= '0' && n <= '9') {
break;
}
}
while (true) {
resp = resp*10 + n-'0';
n = br.read();
if (n < '0' || n > '9') {
break;
}
}
return resp;
}
public static void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int[] v = new int[MAX+1];
int result = 0;
for (int i = 1; i <= MAX; i++) {
result += i*i;
v[i] = result;
}
int num = reader(br);
while (num != 0) {
bw.write(v[num] + "\n");
num = reader(br);
}
bw.flush();
bw.close();
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
Comments
Post a Comment