杨辉三角变形,大数相加(正整数)

在这里插入图片描述

#include<iostream>
using namespace std;
int main()
{
	int c;
	while (cin >> c)
	{
		if (c == 1 || c == 2)   // 特殊情况
		{
			cout << -1 << endl;
			continue;
		}
		if(c % 2 == 1)      //绘图看
            cout<<2<<endl;
        else if(c % 4 == 0)
            cout<<3<<endl;
        else
            cout<<4<<endl;
	}
	return 0;
}

在这里插入图片描述

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str1;
	string str2;
	string str3;
	while (cin >> str1 >> str2)
	{


		if (str1.size() < str2.size())
			swap(str1, str2);
		str3.resize(str1.size() + 1);
		int flag = 0;
		int a1 = str1.size() - 1;
		int a2 = str2.size() - 1;
		int a3 = str3.size() - 1;
		while (a1 >= 0)
		{
			int a, b;
			if (a2 >= 0)
				b = str2[a2] - '0';
			else
				b = 0;
			a = str1[a1] - '0';
			str3[a3] = ((a + b + flag) % 10) + '0';
			if (a + b + flag >= 10)
				flag = 1;
			else
				flag = 0;
			--a1;
			--a2;
			--a3;
		}
		if (flag == 1)
			str3[0] = '1';
		else
			str3.erase(str3.begin());
		cout << str3 << endl;
		str3.clear();
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值