Problem
http://codeforces.com/contest/909/problem/A
Analysis
According to the problem statements, you need to consider all the characters in the first name, and only the first character in the last name.
(1) Take “harry potter”for example
① You got“hp”
② You got “hap”, which is less than “hp”
③ You got “harp”,which is greater than “hap”
So, the answer is “hap”
(2) Take“ertuyivhfg v”for example
① You got “ev”
② You got “erv”, which is less than “ev”
③ You got “ertv”, which is less than “erv”
④ You got “ertuv”, which is less than “ertv”
⑤ You got “ertuyv”, which is greater than “ertuv”
So, the answer is “ertuv”
Code
#include<iostream>
#include<string>
using namespace std;
int main()
{
char first[10], last[10];
cin >> first;
cin >> last;
string pre1, pre2, temp, result;
pre1 = "";
pre1 += first[0];
pre2 = "";
pre2 += last[0];
result = pre1 + pre2;
for (int i = 1; first[i]; i++)
{
pre1 += first[i];
temp = pre1 + pre2;
if (temp < result)
{
result = temp;
}
}
cout << result;
}
更多内容请关注微信公众号