7-1 正整数A+B
#include<bits/stdc++.h>
using namespace std;
inline bool check(string s)
{//检查是否为真的
for (auto i : s)
if (!isdigit(i))
return false;
if (1 <= stoi(s) && stoi(s) <= 1000)return true;
return false;
}
int main()
{
string s, s1, s2, res;
getline(cin, s);//直接读入整行数据
for (int i = 0; i < s.size(); i++)
{
if (s[i] == ' ')
{//找到第一个断点处作为s1的结尾
s1 = s.substr(0, i);//截取0到i的点
s2 = s.substr(i + 1);//从 i + 1开始截取
break;
}
}
if (!check(s1))
{
s1 = res = "?";//一个为假,答案为假
}
if (!check(s2))
{
s2 = res = "?";
}
if (check(s1) && check(s2))
res = to_string(stoi(s1) + stoi(s2));
cout << s1 << " + " << s2 << " = " << res << endl;
}
7-6 一帮一
#include<bits/stdc++.h>
using namespace std;
const int MAX = 100;
char name[MAX][MAX];
bool vis[MAX];
int sex[MAX], N;
char ch, x;
int main()
{
cin >> N;
for (int i = 0; i < N; i++)
{
cin >> sex[i] >> name[i];
}
for (int i = 0; i < N / 2 ; i++)
{
for (int j = N - 1; j >= N / 2; j--)
{
if (sex[i] != sex[j] && vis[j] == 0)
{
vis[j] = 1;//后匹配前,无需判断前者
cout << name[i] << " " << name[j] << endl;
break;
}
}
}
return 0;
}