Bailian2981 大整数加法【大数】(POJ NOI0106-10)

求两个不超过200位的非负整数的和。
Input
有两行,每行是一个不超过200位的非负整数,可能有多余的前导0。
Output
一行,即相加后的结果。结果里不能有多余的前导0,即如果结果是342,那么就不能输出为0342。
Sample Input
22222222222222222222
33333333333333333333
Sample Output
55555555555555555555

ac代码(注意0+0)

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <math.h>
#include <string.h>
#include <set>
using namespace std;

typedef unsigned long long ULL;

string add(string s1,string s2)
{
    string max,min;
    if(s1.length()>s2.length())
    {
        max=s1;min=s2;
    }
    else
    {
        max=s2;min=s1;
    }
    int l1=max.length(),l2=min.length();
    for(int i=l1-1,j=l2-1;j>=0;i--,j--)
        max[i]+=min[j]-'0';
    for(int i=l1-1;i>=1;i--)
        if(max[i]>'9')
        {
            max[i]-=10;
            max[i-1]++;
        }
    if(max[0]>'9')
    {
        max[0]-=10;
        max='1'+max;
    }
    return max;
}

int main()
{
    string s1,s2,sum;
    cin>>s1>>s2;
    sum=add(s1,s2);
    int i;
    int len=sum.length();
    for(i=0;sum[i];i++)
        if(sum[i]=='0')
            continue;
        else
            break;
    if(i>=len) printf("0");
    string sum2(sum.substr(i,len-i));//用substr函数截取字符串。
    cout<<sum2<<endl;
    return 0;
}
openjudge约瑟夫问题是一个数学应用问题。问题的描述是:有n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。最后剩下的那个人就是猴王的编号。 这个问题在openjudge中的输入格式是每行是用空格分开的两个整数,第一个是n, 第二个是m (0 < m,n <= 300)。最后一行是0 0作为输入的结束标志。 要解决openjudge约瑟夫问题,我们需要编写一个程序来接收输入的n和m值,并按照约瑟夫问题的规则计算出最后剩下的猴王的编号。具体的解题算法可以使用循环队列或者递归的方式来实现,具体实现的代码可以根据具体的编程语言来完成。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [openjudge 约瑟夫环问题](https://blog.csdn.net/sdz20172133/article/details/88351098)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [bailian.openjudge 2746:约瑟夫问题](https://blog.csdn.net/smartzxf/article/details/100829938)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值