题解 | #火星A+B#

火星A+B

https://www.nowcoder.com/practice/46bb070835e548678c78477f1fb0b92e

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int length(string str){
	int k=1;
	for (int i = 0; i < str.size(); i++){
		if (str[i] == ','){
			k++;
		}
	}
	return k;
}
int main(){
	int arr[25] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 };
	string str1, str2;
	while (cin >> str1 >> str2){
		vector<int>	vec1;
		vector<int>vec2;
		int l1 = length(str1);
		int l2 = length(str2);
		if (l1 > l2){
			for (int i = 0; i < l1 - l2; i++){
				vec2.push_back(0);
			}
		}
		else{
			for (int i = 0; i < l2 - l1; i++){
				vec1.push_back(0);
			}
		}int m;
		for (int i = 0; i < str1.size(); ){
			if (str1[i] != ','){
				if (str1[i + 1] == ','||i+1==str1.size()){
					m = str1[i] - '0';
					vec1.push_back(m);
					i++;
				}
				else{
					m = (str1[i] - '0') * 10 + str1[i + 1] - '0';
					vec1.push_back(m);
					i = i + 2;
				}
			}
			else{
				i++;
			}
		}
		for (int i = 0; i < str2.size();){
			if (str2[i] != ','){
				if (str2[i + 1] == ','||i + 1 == str2.size()){
					m = str2[i] - '0';
					vec2.push_back(m);
					i++;
				}
				else{
					m = (str2[i] - '0') * 10 + str2[i + 1] - '0';
					vec2.push_back(m);
					i = i + 2;
				}
			}
			else{
				i++;
			}
		}
		int x=0, y=0;
		for (int i = vec1.size()-1; i >= 0; i--){
			vec1[i] = vec1[i] + vec2[i]+x;
			x = vec1[i] / arr[y];
			vec1[i] = vec1[i] % arr[y];
			y++;
		}
		if (x != 0){
			vec1.insert(vec1.begin(), 1);
		}
		printf("%d", vec1[0]);
		for (int i = 1; i < vec1.size(); i++){
			printf(",%d", vec1[i]);
		}
		printf("\n");
	}
}

全部评论

相关推荐

我已成为0offer的糕手:别惯着,胆子都是练出来的,这里认怂了,那以后被裁应届被拖工资还敢抗争?
点赞 评论 收藏
分享
offer多多的六边形战士很无语:看了你的博客,感觉挺不错的,可以把你的访问量和粉丝数在简历里提一下,闪光点(仅个人意见)
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务