标签:
注:编译环境为Visual Studio 2012,答案仅供参考。
36.输入一行数字:123 423 5645 875 186523
在输入第二行:23
将第一行中含有第二行中“23”的数输出并排序
结果即:123 423 186523
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
void sortAndPrint(const string &s1, const string &s2){
vector<int> vs;
auto begin = s1.begin();
while (begin != s1.end())
{
auto ahead = begin + 1;
while (ahead != s1.end() && *ahead != ‘ ‘)
{
ahead++;
}
string tmp(begin,ahead);
int pos = tmp.find(s2);
if (pos >= 0 )
{
vs.push_back(stoi(tmp));
}
if (ahead == s1.end()) break;
begin = ++ahead;
}
if(!vs.size()) return;
sort(vs.begin(),vs.end());
for (unsigned i = 0; i < vs.size(); i++)
{
cout << vs[i] << " ";
}
cout << endl;
}
int main()
{
string s1,s2;
getline(cin,s1);
getline(cin,s2);
sortAndPrint(s1,s2);
return 0;
}
将 电话号码 One Two 。。。Nine Zero
翻译成1 2 。。9 0
中间会有Double
例如输入:OneTwoThree
输出:123
输入:OneTwoDoubleTwo
输出:1222
输入:1Two2 输出:ERROR
输入:DoubleDoubleTwo 输出:ERROR
有空格,非法字符,两个Double相连,Double位于最后一个单词 都错误
#include <iostream>
#include <vector>
#include <string>
using namespace std;
string digit[11]={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Double"};
int convert(const string &s){
for (int i = 0; i < 11; i++)
{
if (s == digit[i])
{
return i;
}
}
return -1;
}
bool englishToNumber(const string &s, string &output){
vector<int> v;
auto begin = s.begin();
while (begin != s.end())
{
auto ahead = begin + 1;
while (ahead != s.end() && !isupper(*ahead))
{
ahead++;
}
string tmp(begin,ahead);
if (convert(tmp) < 0) return false;
v.push_back(convert(tmp));
begin = ahead;
}
for (auto vBegin = v.begin();vBegin != v.end();vBegin++){
if (*vBegin < 10)
{
output.push_back(*vBegin + ‘0‘);
continue;
}
vBegin++;
if (vBegin != v.end() && *vBegin < 10)
{
output.push_back(*vBegin + ‘0‘);
output.push_back(*vBegin + ‘0‘);
continue;
}
return false;
}
return true;
}
int main()
{
string s, output;
getline(cin,s);
cout << (englishToNumber(s,output) ? output : "ERROR") << endl;
return 0;
}
38.输入一个整数,如12336544,或1750,然后从最后一位开始倒过来输出,最后如果是0,则不输出,输出的数字是不带重复数字的,所以上面的输出是456321和571。如果是负数,比如输入-175,输出-571。
#include <iostream>
#include <string>
using namespace std;
void reverseNum(const string s){
int begin = s.find_last_not_of("0");
int end = s[0] == ‘-‘ ? 1 : 0;
if (end == 1) cout << ‘-‘;
for (int i = begin; i >= end; i--)
{
cout << s[i];
}
cout << endl;
}
int main()
{
int n;
cin >> n;
reverseNum(to_string(n));
return 0;
}
39.输入两行字符串正整数,第一行是被减数,第二行是减数,输出第一行减去第二行的结果。
备注:1、两个整数都是正整数,被减数大于减数
示例:
输入:1000000000000001
1
输出:1000000000000000
#include <iostream>
#include <string>
using namespace std;
void bigNumSub(const string &s1, const string &s2, string &s3){
auto b1 = s1.rbegin(), b2 = s2.rbegin();
int overFlow = 0;
while (b1 != s1.rend() && b2 != s2.rend())
{
int sum = ((*b1++) - ‘0‘) - ((*b2++) - ‘0‘) - overFlow;
if (sum < 0)
{
sum += 10;
overFlow = 1;
s3.push_back(sum + ‘0‘);
}
else
{
overFlow = 0;
s3.push_back(sum + ‘0‘);
}
}
while (b1 != s1.rend())
{
int sum = *b1++ - ‘0‘ - overFlow;
if (sum < 0)
{
sum += 10;
overFlow = 1;
s3.push_back(sum + ‘0‘);
}
else
{
overFlow = 0;
s3.push_back(sum + ‘0‘);
}
}
s3.erase(s3.begin()+s3.find_last_not_of("0")+1,s3.end());
reverse(s3.begin(),s3.end());
}
int main()
{
string s1, s2, output;
getline(cin, s1);
getline(cin, s2);
bigNumSub(s1,s2,output);
cout << output << endl;
return 0;
}
40.编程的时候,if条件里面的“(”、“)”括号经常出现不匹配的情况导致编译不过,请编写程序检测输入一行if语句中的圆括号是否匹配正确。同时输出语句中出现的左括号和右括号数量,如if((a==1)&&(b==1))是正确的,而if((a==1))&&(b==1))是错误的。注意if语句的最外面至少有一对括号。提示:用堆栈来做。
输入:if((a==1)&&(b==1))
输出:RIGTH 3 3
输入:if((a==1))&&(b==1))
输出:WRONG 3 4
#include <iostream>
#include <string>
#include <stack>
using namespace std;
bool isMatch(const string &s, int &n1, int &n2){
stack<char> stk;
for (unsigned i = 0; i < s.size(); i++)
{
if (s[i] == ‘(‘)
{
n1++;
stk.push(s[i]);
}
else if (s[i] == ‘)‘)
{
n2++;
if (!stk.empty())
{
stk.pop();
continue;
}
return false;
}
}
return stk.empty();
}
int main()
{
string s;
getline(cin, s);
int num1 = 0, num2 = 0;
cout << (isMatch(s,num1,num2) ? "RIGHT " : "WRONG ") << num1 << " " << num2 << endl;
return 0;
}
标签:
原文地址:http://blog.csdn.net/sps900608/article/details/52192870