10323 - Factorial! You Must be Kidding!!!
Moderator: Board moderators
-
- New poster
- Posts: 11
- Joined: Wed Oct 09, 2002 7:31 pm
- Contact:
-
- Learning poster
- Posts: 67
- Joined: Sun Sep 22, 2002 5:40 am
- Location: Taiwan
10323
Why WA?
[cpp]#include <iostream.h>
typedef unsigned long long ULLT;
int main() {
const ULLT UNDER = 10000LL, OVER = 6227020800LL;
ULLT limit, fact, i;
while (cin >> limit) {
fact = 1;
for (i = 1; i <= limit; i++) {
fact *= i;
if (fact > OVER) {
cout << "Overflow!\n";
break;
}
}
if (fact <= OVER) {
if (fact < UNDER)
cout << "Underflow!\n";
else
cout << fact << '\n';
}
}
return 0;
}[/cpp]
![:roll:](./images/smilies/icon_rolleyes.gif)
[cpp]#include <iostream.h>
typedef unsigned long long ULLT;
int main() {
const ULLT UNDER = 10000LL, OVER = 6227020800LL;
ULLT limit, fact, i;
while (cin >> limit) {
fact = 1;
for (i = 1; i <= limit; i++) {
fact *= i;
if (fact > OVER) {
cout << "Overflow!\n";
break;
}
}
if (fact <= OVER) {
if (fact < UNDER)
cout << "Underflow!\n";
else
cout << fact << '\n';
}
}
return 0;
}[/cpp]
-
- New poster
- Posts: 4
- Joined: Sat Nov 23, 2002 2:00 pm
For negative numbers it's always underflow or overflow (depends on whether the number is even or odd).
This problem is completely stupid (mathematically incorrect), as you can read on this thread:
http://acm.uva.es/board/viewtopic.php?t=803
You can start flaming me for posting the "solution" here, I don't care... People shouldn't be supposed to guess, only to think about logical things.
This problem is completely stupid (mathematically incorrect), as you can read on this thread:
http://acm.uva.es/board/viewtopic.php?t=803
You can start flaming me for posting the "solution" here, I don't care... People shouldn't be supposed to guess, only to think about logical things.
-
- Experienced poster
- Posts: 192
- Joined: Sat Nov 30, 2002 5:14 am
P 10323
How if the input is :
Input :
-1
-2
-3
Ouput:
Overflow
Underflow
Overflow
Is This Right ?
Regards,
RED SCORPION![:lol:](./images/smilies/icon_lol.gif)
Input :
-1
-2
-3
Ouput:
Overflow
Underflow
Overflow
Is This Right ?
Regards,
RED SCORPION
![:lol:](./images/smilies/icon_lol.gif)
-
- Experienced poster
- Posts: 192
- Joined: Sat Nov 30, 2002 5:14 am
Give some explanation
I can't understand how to calculate factorial of negative numbers...if u put n=0 in the formula (n-1)!=n!/n, u get
-1!=0!/0...how is this possible?
pls give some explanation of the process how negative even numbers get Underflow! and odd numbers get Overflow!...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Razib
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Our lives improve only when we take chances - and the first and most
difficult risk we can take is to be honest with ourselves.[/quote]
-1!=0!/0...how is this possible?
pls give some explanation of the process how negative even numbers get Underflow! and odd numbers get Overflow!...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Razib
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Our lives improve only when we take chances - and the first and most
difficult risk we can take is to be honest with ourselves.[/quote]
-
- New poster
- Posts: 9
- Joined: Wed Apr 16, 2003 6:50 pm
- Location: Braga, Portugal
- Contact:
-
- Guru
- Posts: 1080
- Joined: Thu Dec 19, 2002 7:37 pm