题目
输入
3
1,0 2,1
4,2,0 1,2,0
1,0 10,6,4,2,1
解题思路:
用数组做这个题目太烦躁了,不过鉴于鄙人水平有限,没有竞赛经验,所以就学了一下stl的内容来进行快速的解题。主要使用了stack和deque
首先先把输入的数字进行处理,“,”逗号隔开的数字就每次处理好来存放在stack中,由于输入的数据有\n和空格,就使用getchar()函数来进行数据的输入,然后转换好数字后存放到stack中。再在循环中进行检验跳出while循环(如果是双重循环可以考虑用goto函数来处理)
之后把存好在一起的stack的数据汇总在一个deque容器中,之后进行进制的转换并输出即可
C++代码
#include<iostream>
#include<stack>
#include<deque>
using namespace std;
int base[] = {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};
int main()
{
int total;
cin >> total;
getchar();
for(int i = 0 ; i < total; i++)
{
cout << "case #" << i<<":"<<endl;
stack<long long> fir;
stack<long long> sec;
int flen = 0;
char c;
while((c = getchar()) != ' ')
{
if(c == ',')
{continue;}
int temp = 0;
while(c != ',' && c != ' ')
{
temp *= 10;
temp += (c - '0');
c = getchar();
if(c == ' ')
{goto FLAG1;}//经典死循环,goto,多重循环的神
}
FLAG1:
fir.push(temp);
if(c == ' ')
{goto FLAG2;}
}
FLAG2:
int slen = 0;
while((c = getchar()) != '\n')
{
if(c == ',')
{continue;}
int temp = 0;
while(c != ',' && c != '\n')
{
temp *= 10;
temp += (c - '0');
c = getchar();
if(c == '\n')
{goto FLAG3;}
}
FLAG3:
sec.push(temp);
if(c == '\n')
{goto FLAG4;}
}
FLAG4:
//把他们加起来
deque<long long>sum;
int min_size = (fir.size() > sec.size() ? sec.size() : fir.size());
int max_size = (fir.size() < sec.size() ? sec.size() : fir.size());
while(!fir.empty() && !sec.empty())
{
sum.push_front(fir.top() + sec.top());
fir.pop();
sec.pop();
}
while(!fir.empty())
{
sum.push_front(fir.top());
fir.pop();
}
while(!sec.empty())
{
sum.push_front(sec.top());
sec.pop();
}
//在front一直加,所以从back拿小的位数再放回front
int len = sum.size();
int carry = 0;
for(int i = 0; i < len; i++)
{
int temp = sum.back();
temp += carry;
carry = 0;//注意你的carry每一次都要变成0
if(temp >= base[i])//别忘记等于!!!
{
carry = temp / base[i];
temp %= base[i];
}
sum.pop_back();
sum.push_front(temp);
}
if(carry)
{
sum.push_front(carry);
}
deque<long long> :: iterator it = sum.begin();
for(; it != sum.end() - 1; it++)
{
cout << *it << ",";
}
cout << *it << endl;
}
}