UVa 12149 - Feynman

Contents

  1. 1. Problem
  2. 2. Solution
  3. 3. Code

Problem

題目網址
中文網址

Solution

稍微列一下規律

1
1 + 4
1 + 4 + 9
1 + 4 + 9 + 16
... 

Code

UVa 12149
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<cstdio>

int main()
{
int square[101] = {}, ans[101] = {};

for (int i = 0; i < 101; i++)
square[i] = i*i;

for (int i = 1; i < 101; i++)
ans[i] = ans[i - 1] + square[i];
int n;
while (scanf("%d", &n) && n)
printf("%d\n", ans[n]);

return 0;
}