d096: 00913 - Joana and the Odd Numbers
https://zerojudge.tw/ShowProblem?problemid=d096
簡單數學題
#include <stdio.h>
int main(){
long long number ,d ,n , ans , count , total;
while ( scanf("%lld",&n)!=EOF ){
d = (1 + n) / 2;
total = ( 1 + n ) * d /2;
total = total *2 -1;
number = total*3 -6;
printf("%lld\n",number);
}
return 0;
}
Allen版解答
#include <iostream>
using namespace std;
int main(){
int n,ans,row,lineStarter,lastOdd;
while ( cin>>n ){
//算出n個奇數該出現在第幾列,也就是row
row = (1 + n) / 2;
//算出第row列的開頭數字是多少
lineStarter = (row-1)*(row-1)*2+1;
//該列的最後一個奇數的算法
lastOdd =lineStarter+2*(n-1);
//lastOdd+(lastOdd-2)+(lastOdd-4) = lastOdd*3 -6
ans = lastOdd*3 -6;
cout<<ans<<endl;
}
return 0;
}