阶乘 - 阵列 - C ++ [英] Factorial - Array - C++

查看:152
本文介绍了阶乘 - 阵列 - C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的问题是计算若干阶乘。我调试我的code,它工作正常进行任何输入,产生的正确输出所有给定的测试用例。但尽管如此,我越来越对SPOJ错误的答案。

问题:

这是整数t,1所述; = T&下; = 100,表示测试用例的数量,接着为T行,每一个包含单个整数n,1&下; =正&下; = 100

任何有识之士将是有益的。我失去了一些重要的测试案例?

我的code:

 的#include<&iostream的GT;
使用命名空间std;
诠释的main()
{长T;
CIN>>吨;
而(T--)
{
    长长的N;    长长的指数,计算;
    霉素>将N;
    长长的一个[300];
    长长的I = 1;
    一个[0] = 1;数= 1;
    而(I< = N)
    {        长长的Z = 0;
        长长的K表;长长的X;
        长长的J =;
        长长的温度= 0;长长的电流= 0;
        对于(K = 0; K<计数; k ++)
        {
            X =(A [k]的* j)条+温度;
            一个[K] = X%10;
            TEMP = X / 10;
        }
        如果(温度大于0)
            而(温度大于0)
            {
                一个[K] =温度;
                TEMP =温度/ 10;
                ķ++;
                算上++;
            }
        我++;
    }
    为(长很长G =计数1; G> = 0; g--)
        COUT<< A [G]。
        COUT<<\\ n;}
返回0;
}


解决方案

这行:


  A [K] =温度;


应该是:

  A [K] =气温%10;

The problem is to compute factorial of a number. I've debugged my code, and it works fine for any input and produces correct output for all the given test cases. But still, I'm getting Wrong Answer on SPOJ.

Problem:

An integer t, 1<=t<=100, denoting the number of testcases, followed by t lines, each containing a single integer n, 1<=n<=100.

Any insight would be helpful. Am I missing some critical test cases?

My Code:

#include <iostream>
using namespace std;
int main()
{

long T;
cin>>T;
while(T--)
{
    long long N;

    long long index, count;
    cin>>N;
    long long a[300];
    long long i=1;
    a[0]=1; count=1;
    while(i<=N)
    {

        long long z=0;
        long long k; long long x;
        long long j=i;
        long long temp=0; long long current=0;
        for(k=0; k<count;k++)
        {
            x=(a[k]*j)+temp;
            a[k]=x%10;
            temp=x/10;
        }
        if(temp>0)
            while(temp>0)
            {
                a[k]=temp;
                temp=temp/10;
                k++;
                count++;
            }


        i++;
    }


    for(long long g=count-1; g>=0; g--)
        cout<<a[g];
        cout<<"\n";

}
return 0;
}

解决方案

This line:

a[k]=temp;

should be:

a[k]=temp % 10;

这篇关于阶乘 - 阵列 - C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆