修飾パスカル三角形、多数の添加(正の整数)

ここに画像を挿入説明

#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;
}
公開された230元の記事 ウォン称賛28 ビュー9340

おすすめ

転載: blog.csdn.net/weixin_43767691/article/details/103227702