【程式解題】ZeroJudge a216數數愛明明 C解法

直觀遞迴解法

input 數字的大小會造成output整數溢位。

所以要用long unsigned。

#include <stdio.h>

#include <stdlib.h>


long unsigned f(int x);

long unsigned g(int y);

int main(){

int n;

while(scanf("%d",&n)!=EOF){

printf("%lu ",f(n));

printf("%lu\n",g(n));

}

system("pause");

return 0;

}

long unsigned f(x){

if(x==1) return 1;

else return x+f(x-1);

}

long unsigned g(y){

if(y==1) return 1;

else return f(y)+g(y-1);

}

留言

這個網誌中的熱門文章

【程式解題】2019年4月TOI練習賽新手組 - 滿意度調查 (Survey of Satisfaction) C解法(註解版)

【程式解題】 ZeroJudge a038數字翻轉 C解法(逐行註解)