N!对10007取余
题目:请求N!(N<=10000),输出结果对10007取余输入
每行一个整数n,遇到-1结束。
输出
每行一个整数,为对应n的运算结果。
Sample Input
1
2
-1
Sample Output
1
2
[此贴子已经被作者于2016-9-18 21:51编辑过]
#include "stdio.h" int main() { int n,result,i; printf("请输入数字n,或者输入 -1 退出程序:\n"); while((1==scanf("%d",&n))&&n!=-1){ result=1; for(i=2;i<=n;i++) result=result*i%10007; printf("\nresult: %d \n\n",result); printf("继续请输入数字n, 或者输入-1退出程序:\n\n"); } return 0; }
#include<stdio.h> #include<stdlib.h> int main() { int i,n,m; int *p = malloc(sizeof(int)); int *q; int count = 0; while (1) { m = 1; n = -1; scanf("%d",&n); if (-1 == n) { break; } for (i = 1; i <= n; i++) { m *= i; m = m % 10007; } p[count] = m; count++; q = realloc(p, (count + 1) * sizeof(int)); p = q; } for (i = 0; i < count; i++) { printf("%d\n",p[i]); } free(p); return 0; }