1013325146
2018-12-08 17:24
#include<stdio.h>
int prodict(int n){
int ss;
if(n < 0){
printf("输入错误!");
return 0;
}
if(n == 1 || n == 0){
return 1;
}
else {
ss = prodict(n-1) * n;
}
return ss;
}
int main() {
printf("n=%d: %d\n", 1, prodict(1));
printf("n=%d: %d\n", 2, prodict(2));
printf("n=%d: %d\n", 3, prodict(3));
printf("n=%d: %d\n", 4, prodict(4));
printf("n=%d: %d\n", 5, prodict(5));
printf("n=%d: %d\n", 6, prodict(6));
printf("n=%d: %d\n", 7, prodict(7));
printf("n=%d: %d\n", 7, prodict(8));
printf("n=%d: %d\n", 9, prodict(9));
printf("n=%d: %d\n", 10, prodict(10));
printf("n=%d: %d\n", 20, prodict(20));
printf("n=%d: %d\n", 30, prodict(30));
printf("n=%d: %d\n", 50, prodict(50));
return 0;
}
运算结果:
n=1: 1
n=2: 2
n=3: 6
n=4: 24
n=5: 120
n=6: 720
n=7: 5040
n=7: 40320
n=9: 362880
n=10: 3628800
n=20: -2102132736
n=30: 1409286144
n=50: 0
超界。考虑int型范围在-32768~32767,用实型更妥当
C语言入门
926025 学习 · 20793 问题
相似问题