题192.洛谷P1009 模拟-[NOIP1998 普及组] 阶乘之和

news/2024/7/2 1:21:54 标签: 算法, c++, 模拟

文章目录

  • 题192.洛谷P1009 模拟-[NOIP1998 普及组] 阶乘之和
  • 一、题目
  • 二、题解


题192.洛谷P1009 模拟-[NOIP1998 普及组] 阶乘之和


一、题目

在这里插入图片描述

二、题解

因为本题n可以很大,所以计算阶乘和之后的结果没办法表示然后输出,所以必须转字符串去模拟计算过程–外壳不变,依旧递归求阶乘,循环求和,其中的用到的起关键作用的求和与求积用模拟竖式计算来解决。代码如下:

#include <bits/stdc++.h>

using namespace std;

string Add(string a,string b)
{
    string ans;
    int add=0;//需要加到本位的进位数
    int i,j;
    for(i=a.length()-1,j=b.length()-1;i>=0&&j>=0;i--,j--)
    {
        int res=(a[i]-'0')+(b[j]-'0')+add;//和等于两数之和加上进位
        ans=to_string(res%10)+ans;//只将和的个位数存入结果
        add=res/10;//计算进位
    }
    while(i>=0)
    {
        int res=(a[i--]-'0')+add;
        ans=to_string(res%10)+ans;
        add=res/10;
    }
    while(j>=0)
    {
        int res=(b[j--]-'0')+add;
        ans=to_string(res%10)+ans;
        add=res/10;
    }
    if(add!=0)//如果进位不是0就还得算上
    {
        ans=to_string(add)+ans;
    }
    return ans;
}

string Multiple(string a,string b)
{
    string tmp[2001];//存储列竖式计算乘法时的中间结果
    string ans="0";
    if(a[0]==0||b[0]==0)
    {
        ans="0";
        return ans;
    }
    int alen=a.length(),blen=b.length();
    for(int i=blen-1; i>=0; i--)
    {
        int preadd=0,add=0;//本位的进位,下一位的进位
        for(int j=0; j<(blen-1)-i; j++)
        {
            tmp[i]="0"+tmp[i];//用0补位,便于后序计算
        }
        for(int j=alen-1; j>=0; j--)
        {
            int res=(a[j]-'0')*(b[i]-'0');
            add+=res/10;//得到下一位进位
            res=res%10;//本位结果只留个位数
            res+=preadd;//本位结果还需加上本位的进位
            add+=res/10;//本位结果又做了加法所以得再更新一回下一位进位
            res=res%10;
            preadd=add;//更新本位的进位,给下一轮使用
            add=0;//下一位进位再初始化为0
            tmp[i]=to_string(res)+tmp[i];
        }
        if(preadd!=0)//如果进位不是0就还得算上
        {
            tmp[i]=to_string(preadd)+tmp[i];
        }
        ans=Add(ans,tmp[i]);
    }
    return ans;
}

string getFactorial(int n)
{
    if(n==1)
    {
        return "1";//原本为1
    }
    else
    {
        return Multiple(to_string(n),getFactorial(n-1));//原本为n*getF...(n-1)
    }
}

int main()
{
    int n;
    cin>>n;
    string res="0";
    for(int i=1;i<=n;i++)
    {
        res=Add(res,getFactorial(i));
    }
    cout<<res<<endl;
}


http://www.niftyadmin.cn/n/1061861.html

相关文章

c#中事务及回滚

程序一般在特殊数据的时候&#xff0c;会有数据上的同步&#xff0c;这个时候就用到了事物。闲话不多说&#xff0c;直接上代码。 1 public void UpdateContactTableByDataSet(DataSet ds, string strTblName)2 {3 try4 {5 Sq…

题193.2022寒假天梯赛训练-7-4 N个数求和 (20 分)

文章目录题193.2022寒假天梯赛训练-7-4 N个数求和 (20 分)一、题目二、题解题193.2022寒假天梯赛训练-7-4 N个数求和 (20 分) 一、题目 二、题解 本题两个关键&#xff0c;通分与约分。需用到求最大公约数与最小公倍数的算法。 #include <bits/stdc.h>using namespace st…

磁盘分区调整

目标是&#xff1a;将D盘分给C盘&#xff1a; 方法&#xff1a; 1&#xff09;右键“计算机”&#xff0c;选择“管理”&#xff0c;进入可以看到有个“磁盘管理” 2&#xff09;先给D盘“删除卷”&#xff0c;会提示“是否清除该盘所有数据”&#xff0c;删除完后可以可拿到D盘…

Atom git-pull插件 git push报错 permission denied (publickey)

搞了很久&#xff0c;试了很多办法&#xff0c;最后也不知道是哪一步解决的&#xff0c;总结如下&#xff1a; 插件设置里面 git路径 C:\Users\youruser\AppData\Local\GitHub\PortableGit_25d850739bc178b2eb13c3e2a9faafea2f9143c0\cmd\git.exe c:\user\.ssh\下面的githu…

c++常用库

pugixml boost.log http://www.cryptopp.com/ sqlite https://github.com/pmed/sqlitepp https://github.com/neosmart/CppSQLite http://sourceforge.net/projects/wxsqlite/转载于:https://www.cnblogs.com/anjsxz/p/3729106.html

题194.洛谷P1518 [USACO2.4]两只塔姆沃斯牛 The Tamworth Two

文章目录题194.洛谷P1518 [USACO2.4]两只塔姆沃斯牛 The Tamworth Two一、题目二、题解题194.洛谷P1518 [USACO2.4]两只塔姆沃斯牛 The Tamworth Two 一、题目 二、题解 本题没什么好说的&#xff0c;记录一下自己咋犯病的 #include <bits/stdc.h>using namespace std;co…

Linux下C编程入门(4)

Linux下编译器介绍转载于:https://www.cnblogs.com/guochaoxxl/p/7745425.html

关于perl bignum模块用法

用bignum处理大数:对于32位的perl来说&#xff0c;他的整数范围被限定在32位。perl -le "print 873498127389471892374891723489172389407128947891234" 8.73498127389472e50 打印的结果为科学记数法显示 在命令行中输入一个较大位数的整数&#xff0c;显示结果与我…