이 프로그램을 작성했습니다. 하지만 산술 예외 오류가 발생합니다. 프로그램 :Artihmetic 예외 오류 받기
이#include<bits/stdc++.h>
using namespace std;
int fact(int n)
{
int facte = 1;
for (int i = 1; i <= n; i++)
facte *= i;
return facte;
}
int main()
{
int n, k;
cin >> n >> k;
int sum1 = 0;
int res = 0;
int temp = k;
int term1, term2, term3;
while ((n - temp) > 1)
{
term1 = fact(n);
term2 = fact(n - temp);
term3 = fact(temp);
res = int((term1/(term2 * term3)));
sum1 += res;
temp--;
}
cout << sum1 * fact(n - k - 1) + 1;
}
입력 :
n=4 and k=2오류 :
GDB trace: Reading symbols from solution...done. [New LWP 18123] Core was generated by `solution'. Program terminated with signal SIGFPE, Arithmetic exception. #0 main() at solution.cc:29 29 res=int((term1/(term2*term3))); #0 main() at solution.cc:29
'term2'또는 'term3'은 0이어야합니다. – dasblinkenlight
'p term2'와'p term3'은 어떤 값을 가지고 있는지 알고 싶습니다. – pm100
'sum + = res; 대신에'sum1 + = res;'이어야한다고 생각합니다. –