CUIT 2016 新生训练题第一周 E - N!

Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! 
Input
One N in one line, process to the end of file. 
Output
For each N, output N! in one line. 
Sample Input
1
2
3
Sample Output
1
2
6


题解:大数阶乘,注意要记录结果长度,如此可方便输出,减少计算量。
#include<stdio.h>
#include<string.h>

int res[40000];

int main()
{
    int num,length;
    while(~scanf("%d",&num))
    {
        /*重置部分*/
        memset(res,0,sizeof(res));
        
        /*计算部分*/
        res[0]=1;
        length=0;
        for(int i=2;i<=num;i++)
        {
            for(int j=0;j<=length;j++) res[j]*=i;
            
            /*进位部分*/
            for(int i=0;i<=length;i++)
                if(res[i]>9)
                {
                    res[i+1]+=res[i]/10;res[i]%=10;
                    if(i+1>length){length++;}      /*更新结果长度*/
                }
        }
        
        /*输出部分*/
        for(int k=length;k>=0;k--)
            printf("%d",res[k]);
        printf("\n");
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是实现将非负的十进制数转换为指定进制的代码: ```c #include <stdio.h> #include <stdlib.h> #define STACKSIZE 100 typedef int DataType; typedef struct { DataType items[STACKSIZE]; int top; } SqStack; void InitStack(SqStack *s) { s->top = -1; } int StackEmpty(SqStack *s) { return s->top == -1; } int StackFull(SqStack *s) { return s->top == STACKSIZE - 1; } int Push(SqStack *s, DataType x) { if (StackFull(s)) { return 0; } s->top++; s->items[s->top] = x; return 1; } int Pop(SqStack *s, DataType *x) { if (StackEmpty(s)) { return 0; } *x = s->items[s->top]; s->top--; return 1; } int DecimalConvert(SqStack *s, int dec, int scale) { if (dec < 0) { return 0; } do { int remainder = dec % scale; Push(s, remainder); dec /= scale; } while (dec != 0); return 1; } int main() { SqStack s; InitStack(&s); int dec, scale; printf("Enter a non-negative decimal number: "); scanf("%d", &dec); printf("Enter the scale to convert to (2, 8, or 16): "); scanf("%d", &scale); if (scale != 2 && scale != 8 && scale != 16) { printf("Invalid scale.\n"); return 1; } if (DecimalConvert(&s, dec, scale)) { printf("The %d-digit representation of %d is: ", scale, dec); int x; while (Pop(&s, &x)) { if (x < 10) { printf("%d", x); } else { printf("%c", 'A' + x - 10); } } printf("\n"); } else { printf("Invalid decimal number.\n"); return 1; } return 0; } ``` 函数接口定义如下: ```c int DecimalConvert(SqStack *s, int dec, int scale); ``` 其中,`s` 是指向栈的指针,`dec` 是需要转换的十进制数,`scale` 是指定的进制数。函数返回值为 1 表示转换成功,返回 0 表示转换失败(通常是因为 `dec` 参数小于 0)。在主函数中可以通过调用 `DecimalConvert` 函数将十进制数转换成指定进制数,并将结果存储在顺序栈中,最后再通过调用 `Pop` 函数将结果弹出并输出。注意,当 `scale` 不是 2、8 或 16 时,应该输出提示信息并结束程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值