哈工大机试 数字阶梯求和 Easy *注意字符串+字符串补零快一点

基本思想:

无;

关键点:

无;

#include<string>
#include<vector>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;

void add(string& a, string b) {
	//补全0;
	if (a.size() < b.size()) {
		for (int i = b.size() - a.size(); i > 0; i--) {
			a.push_back('0');
		}
	}
	else {
		for (int i = a.size() - b.size(); i > 0; i--) {
			b.push_back('0');
		}
	}
	//开始计算;
	int carry = 0;
	for (int i = 0; i < a.size(); i++) {
		int temp = carry + (a[i] - '0') + (b[i] - '0');
		a[i] = temp % 10+'0';
		carry = temp / 10;
	}
	if (carry != 0)
		a.push_back('1');
	//消除前导零;
	while (a.size() != 0 && a[a.size() - 1] == '0')
		a.pop_back();
}

int main() {
	char c;
	int b;
	while (cin >> c >> b) {
		string s = "";
		string res = "0";
		for (int i = 0; i < b; i++) {
			s += c;
			add(res, s);
		}
		reverse(res.begin(), res.end());
		cout << res << endl;
	}
}

  

猜你喜欢

转载自www.cnblogs.com/songlinxuan/p/12466725.html