标签:style blog io ar color os sp for div
#include <iostream> #include <string.h> #include <sstream> #include <math.h> #include <vector> #include <algorithm> using namespace std; int susuTable[28]={1}; bool isLear(int num) { if(num==2) return true; else { int i = 2; for(;i<=sqrt(num);i++) { if(num%i == 0) return false; } return true; } } void setArr(int arr[]) { int i = 2; int totalBit = 1; while(totalBit!=28) { if(isLear(i) ==true) { arr[totalBit] = i; totalBit++; } i++; } } void display(int arr[]) { int i = 0; for(;i<28;i++) { cout<<arr[i]<<" "; } } void transString(string spark,vector<int> &vec) { int len = spark.length(); int i = 0; while(i!=len) { int temp = 0; while(spark[i]!=‘,‘ && i!=len) { temp = temp*10 + (spark[i]-‘0‘); i++; } vec.insert(vec.begin(),temp); if(i==len) break; else i++; } } void disVec(vector<int> vec) { int i=vec.size()-1; for(;i>=0;i--) { cout<<vec[i]; if(i!=0) cout<<‘,‘; } cout<<endl; } void add(vector<int> &vecA,vector<int> &vecB) { int upFlow = 0; int index = 1; int sizeA = vecA.size(); int sizeB = vecB.size(); if(sizeB > sizeA) vecA.swap(vecB); int i = 0; for(;i<vecB.size();i++) { vecA[i] = vecA[i] + vecB[i] + upFlow; if(vecA[i] >= susuTable[i+1]) { vecA[i] %= susuTable[i+1]; upFlow = 1; } else upFlow = 0; } for(;i<vecA.size();i++) { vecA[i] = vecA[i] + upFlow; if(vecA[i] >= susuTable[i+1]) { vecA[i] %= susuTable[i+1]; upFlow = 1; } else upFlow = 0; } if(upFlow==1) vecA.push_back(1); } int main() { string num_a,num_b; vector<int> vecA; vector<int> vecB; setArr(susuTable); //display(susuTable); cin>>num_a>>num_b; while(!(num_a =="0" || num_b=="0")) { transString(num_a,vecA); //disVec(vecA); transString(num_b,vecB); add(vecA,vecB); disVec(vecA); vecA.clear(); vecB.clear(); cin>>num_a>>num_b; } return 1; }
题目描述见上一篇。
解题思路:将火星数字a,b中的各个部分的数值取出来,放入各自的vec中,采用头插法,然后遍历2个数组,将对应的元素相加,如有进位,保存在upFlow中,加到下一组元素,知道某一个vec到尽头,继续讲进位加下去,直到另一个vec到尽头,最后将最后的进位插入到存储和的vec的末尾,这个vec就是火星数字a+b,输出即可
标签:style blog io ar color os sp for div
原文地址:http://www.cnblogs.com/55open/p/4119625.html