有些內容使用中英雙語,有些只有英文或中文。歡迎使用與分享任何內容,但先來信告知並標示此部落格為出處。
Some parts use both Chinese and English, but some parts use only one language. Feel free to share, but please contact me first and list this blog as your reference.

2014年3月2日 星期日

UVa OJ - 12149 Feynman

The following program is my ACcepted code for UVA-12149 .
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know.  :D

此乃UVA 12149 的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )

點這裡看題目 Click here to see this Problem!

一個簡單的數學問題
當邊為 1 時,會有  n  *  n   squares.
當邊為 2 時,會有(n-1)*(n-1) squares.
.
.
直到...
當邊為 n 時,會有  1  *  1   squares.
全部加總會得到答案!

A simple math problem.
When the edge = 1, there're  n  *  n   squares.
When the edge = 2, there're(n-1)*(n-1) squares.
.
.
Until..
When the edge = n, there're  1  *  1   squares.
Then add them to get the answer.


//This program is for UVA 12149 Feynman
//題目來源 Problem link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=243&page=show_problem&problem=3301

#include<stdio.h>
using namespace std;

int calSquar(int n)
{
    int sum = 0;
 
    for(int i = 1; i <= n; i++)
        sum = sum + (i * i);
     
    return sum;  
}

int main()
{
    int n;
 
    while(scanf("%d", &n) && n)
        printf("%d\n", calSquar(n));
    return 0;
}

Please feel free to use it after adding this blog as an reference. (http://autekroy.blogspot.tw) If there is any mistake or comment, please let me know. :D 

歡迎使用與分享任何內容,但請記得標示此部落格為出處。(http://autekroy.blogspot.tw/) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )

沒有留言:

張貼留言

請留下您的任何想法或建議!
Please leave any thought or comment!