第一题
# include<iostream>
using namespace std;
int root(int a)
{
int sum;
while(a)
{
sum += a%10;
a = a/10;
}
if(sum < 10)
return sum;
else
return root(sum);
}
int main()
{
while(1)
{
int n;
cin >> n;
if(!n)
break;
else
{
int re = root(n);
cout << re << endl;
}
}
return 0;
}
第二题
#include<iostream>
#include<string>
using namespace std;
void reverse(string & s, int start, int end)
{
int n = (end - start + 1) / 2;
for(int i = 0; i < n; i++)
{
char temp = s.at(start + i);
s.at(start + i) = s.at(end - i);
s.at(end - i) = temp;
}
}
int main()
{
int n;
cin >> n;
// cin之后getline会直接读入一行空格,为了不影响正常输入,在此读入一个空字符串
string extra;
getline(cin, extra);
while(n--)
{
string s1;
getline(cin, s1);
int start = 0;
int len = s1.length();
for(int i = 0; i < len; i++)
{
if(s1.at(i) == ' ')
{
reverse(s1, start, i-1);
start = i + 1;
}
}
reverse(s1, start, len - 1);
cout << s1 << endl;
}
}