Erreur floating point exception
Bonjour,
J'ai écrit un code qui affiche un nombre entier N sous la forme : N = a1 x 1! + a2 x 2! + ... + ap x p! avec a1 + ... + ap minimal.
Exemple 1
Entrée : N = 52
Sortie : p = 4 ; les coefficients sont : 0, 2, 0, 2
Exemple 2
Entrée : N = 120
Sortie : p = 5 ; les coefficients sont : 0, 0, 0, 0, 1
J'obtiens l'erreur Floating Point Exception sur un serveur d'évaluation pour un entier donné que j'ignore (N <= 500 millions). Pourtant, j'ai évité une division par 0 (ou un modulo).
Pour N = 479 000 000, j'ai p = 11 et les coefficients : 0, 1, 1, 3, 4, 4, 7, 8, 9, 10, 11
Pour N = 480 000 000, il affiche p = 12 puis se plante !
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| #include <iostream>
#include <stack>
#include <cstdio>
using namespace std;
stack<int> coef;
int main()
{
int nb;
scanf("%d", &nb);
int fact = 1;
int rang = 1;
while (fact <= nb)
{
rang = rang + 1;
fact = fact * rang;
}
fact = fact / rang;
rang = rang - 1;
printf("%d\n", rang);
coef.push(nb / fact);
while (rang >= 2)
{
nb = nb % fact;
fact = fact / rang;
rang = rang - 1;
coef.push(nb / fact);
}
while(!coef.empty())
{
printf("%d ", coef.top());
coef.pop();
}
} |
Merci pour votre aide !