以下是我的code
我很納悶 為何我的程式在44以前 都是正確的 可是在45的時候卻錯誤了
請大家指點指點 謝謝
#include <iostream> // b432
#include <cmath>
using namespace std ;
unsigned long long int C( int a , int b ) ;
unsigned long long int P( int a ) ;
int main(int argc, char** argv) {
unsigned long long int n ;
while ( cin >> n ){
unsigned long long int sum = 0 ;
printf("正解 = %.0lf ",sqrt(5)/5*(pow((1+sqrt(5))/2,n+1)-pow((1-sqrt(5))/2,n+1)));
if ( n % 2 == 0 ){
for ( int i = 1 ; i < n/2 ; i++ ){
sum += C(n-i,i) ;
}
printf("\t我的回答 = %lld\n",2+sum) ;
}else{
for ( int i = 1 ; i <= n/2 ; i++ ) {
sum += C(n-i,i) ;
}
printf("\t我的回答 = %lld\n",1+sum) ;
}
}
}
unsigned long long int C( int a , int b ) {
int c = a - b ;
unsigned long long int sum = 1 ;
if ( b > c ) swap(b,c) ;
for ( int i = 1 ; i <= b ; i++ )
sum *= c+i ;
return sum / P(b) ;
}
unsigned long long int P( int a ){
unsigned long long int sum = 1 ;
for ( int i = 2 ; i <= a ; i++ ) sum *= i ;
return sum ;
}
以下是我的code
我很納悶 為何我的程式在44以前 都是正確的 可是在45的時候卻錯誤了
請大家指點指點 謝謝
#include // b432
#include
using namespace std ;
unsigned long long int C( int a , int b ) ;
unsigned long long int P( int a ) ;
int main(int argc, char** argv) {
unsigned long long int n ;
while ( cin >> n ){
unsigned long long int sum = 0 ;
printf("正解 = %.0lf ",sqrt(5)/5*(pow((1+sqrt(5))/2,n+1)-pow((1-sqrt(5))/2,n+1)));
if ( n % 2 == 0 ){
for ( int i = 1 ; i < n/2 ; i++ ){
sum += C(n-i,i) ;
}
printf("\t我的回答 = %lld\n",2+sum) ;
}else{
for ( int i = 1 ; i <= n/2 ; i++ ) {
sum += C(n-i,i) ;
}
printf("\t我的回答 = %lld\n",1+sum) ;
}
}
}
unsigned long long int C( int a , int b ) {
int c = a - b ;
unsigned long long int sum = 1 ;
if ( b > c ) swap(b,c) ;
for ( int i = 1 ; i <= b ; i++ )
sum *= c+i ;
return sum / P(b) ;
}
unsigned long long int P( int a ){
unsigned long long int sum = 1 ;
for ( int i = 2 ; i <= a ; i++ ) sum *= i ;
return sum ;
}