#28238: #0: 100% RE (SIGFPE) 溢位或者除以0的錯誤!! Floating point exception (core dumped) 請問為什麼跑不出來


qazasd0131 (isu10803026a)

學校 : 義守大學
編號 : 88593
來源 : [60.249.202.127]
最後登入時間 :
2022-01-14 10:59:41
d134. 00369 - Combinations -- UVa369 | From: [219.69.120.16] | 發表日期 : 2021-11-20 01:33

#0: 100% RE (SIGFPE)

溢位或者除以0的錯誤!! 
Floating point exception (core dumped)

#include<iostream> using namespace std; unsigned long long int gcd(unsigned long long int a,unsigned long long int b){ if(b==0) return a; else gcd(b,a%b); } int main(){ unsigned long long int N , M ; while(cin>>N>>M){ int rM=M; if(N==M and N==0) break; if(M>=N/2){ M = N-M; } unsigned long long int a=1 ,i,tmp,g,g2; for(i=1;i<=M;i++){ tmp = i; g = gcd(a,tmp); g2 = gcd((N-i+1),tmp); if(g!=1 and g2!=1){ a = (a/g) * ((N-i+1)/g2) / (i/g/g2); } else if(g!=1){ a = (a/g) * (N-i+1) / (i/g); } else if(g2!=1){ a = a * ((N-i+1)/g2) / (i/g2); } else a = a * (N-i+1) / i; // cout << "a= " << a << " (N-i+1)= " << N-i+1 << " i= " << i << " g= " << g << " g2= " << g2 <<endl; } cout << N << " things taken " << rM << " at a time is " << a << " exactly."<< endl; } }

 

 
#28239: Re:#0: 100% RE (SIGFPE) 溢位或者除以0的錯誤!! Floating point exception (core dumped) 請問為什麼跑不出來


qazasd0131 (isu10803026a)

學校 : 義守大學
編號 : 88593
來源 : [60.249.202.127]
最後登入時間 :
2022-01-14 10:59:41
d134. 00369 - Combinations -- UVa369 | From: [219.69.120.16] | 發表日期 : 2021-11-20 01:49

#0: 100% RE (SIGFPE)

溢位或者除以0的錯誤!! 
Floating point exception (core dumped)

#include using namespace std; unsigned long long int gcd(unsigned long long int a,unsigned long long int b){ if(b==0) return a; else gcd(b,a%b); } int main(){ unsigned long long int N , M ; while(cin>>N>>M){ int rM=M; if(N==M and N==0) break; if(M>=N/2){ M = N-M; } unsigned long long int a=1 ,i,tmp,g,g2; for(i=1;i<=M;i++){ tmp = i; g = gcd(a,tmp); g2 = gcd((N-i+1),tmp); if(g!=1 and g2!=1){ a = (a/g) * ((N-i+1)/g2) / (i/g/g2); } else if(g!=1){ a = (a/g) * (N-i+1) / (i/g); } else if(g2!=1){ a = a * ((N-i+1)/g2) / (i/g2); } else a = a * (N-i+1) / i; // cout << "a= " << a << " (N-i+1)= " << N-i+1 << " i= " << i << " g= " << g << " g2= " << g2 <<endl; } cout << N << " things taken " << rM << " at a time is " << a << " exactly."<< endl; } }

 

更新 通過了

//UVA 00369 Combinations

#include<iostream>

using namespace std;

unsigned long long int gcd(unsigned long long int a,unsigned long long int b){

if(b==0) return a;

else gcd(b,a%b);

}

int main(){

unsigned long long int N , M ;

while(cin>>N>>M){

int rM=M;

if(N==M and N==0) break;

if(M>=N/2){

M = N-M;

}

unsigned long long int a=1 ,i,tmp,g,g2;

for(i=1;i<=M;i++){

tmp = i;

g = gcd(a,tmp);

g2 = gcd((N-i+1),tmp);

// cout << "a= " << a << " (N-i+1)= " << N-i+1 << " i= " << i << " g= " << g << " g2= " << g2 <<endl;

if(g!=1 and g2!=1 and i>=g*g2){

a = (a/g) * ((N-i+1)/g2) / (i/g/g2);

}

else if(g!=1){

a = (a/g) * (N-i+1) / (i/g);

}

else if(g2!=1){

a = a * ((N-i+1)/g2) / (i/g2);

}

else a = a * (N-i+1) / i;

// cout << "a= " << a << " (N-i+1)= " << N-i+1 << " i= " << i << " g= " << g << " g2= " << g2 <<endl;

}

cout << N << " things taken " << rM << " at a time is " << a << " exactly."<< endl;

}

}

 

 
ZeroJudge Forum