#include #include #include #include #include #include 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< <<" "; }}void transString(string spark,vector &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 vec){ int i=vec.size()-1; for(;i>=0;i--) { cout<
&vecA,vector
&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
= susuTable[i+1]) { vecA[i] %= susuTable[i+1]; upFlow = 1; } else upFlow = 0; } for(;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 vecA; vector 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,输出即可