HDU 4730 We Love MOE Girls(签到题 string char)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4730


Problem Description
Chikami Nanako is a girl living in many different parallel worlds. In this problem we talk about one of them.
In this world, Nanako has a special habit. When talking with others, she always ends each sentence with "nanodesu".
There are two situations:
If a sentence ends with "desu", she changes "desu" into "nanodesu", e.g. for "iloveyoudesu", she will say "iloveyounanodesu". Otherwise, she just add "nanodesu" to the end of the original sentence.
Given an original sentence, what will it sound like aften spoken by Nanako?
 

Input
The first line has a number T (T <= 1000) , indicating the number of test cases.
For each test case, the only line contains a string s, which is the original sentence.
The length of sentence s will not exceed 100, and the sentence contains lowercase letters from a to z only.
 

Output
For every case, you should output "Case #t: " at first, without quotes. The  t is the case number starting from 1. Then output which Nanako will say.
 

Sample Input
   
   
   
   
2 ohayougozaimasu daijyoubudesu
 

Sample Output
   
   
   
   
Case #1: ohayougozaimasunanodesu Case #2: daijyoubunanodesu
 

Source
2013 ACM/ICPC Asia Regional Chengdu Online


代码如下:

string

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
string s, s1, s2;

int main()
{
    int t;
    int cas = 0;
    cin >> t;
    while(t--)
    {
        printf("Case #%d: ",++cas);
        cin >> s;
        int len = s.length();
        if(len < 4)
        {
            cout<<s<<"nanodesu"<<endl;
            continue;
        }
        s1 = s.substr(len-4,4);
        s2 = s.substr(0,len-4);
        if(s1 == "desu")
        {
            cout<<s2<<"nanodesu"<<endl;
        }
        else
        {
            cout<<s<<"nanodesu"<<endl;
        }
    }
    return 0;
}
/*
99
aa
bb
ohayougozaimasu
daijyoubudesu
desu
desudesu
ddddd
*/

char

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
char s[117],s1[117];
int main()
{
    int t;
    int cas = 0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",s);
        int len = strlen(s);
        for(int i = 0; i < len-4; i++)
        {
            s1[i] = s[i];
        }
        s1[len-4] = '\0';
        printf("Case #%d: ",++cas);
        if(s[len-4]=='d'&&s[len-3]=='e'&&s[len-2]=='s'&&s[len-1]=='u')
        {
            //if(len != 4)
            printf("%s",s1);
            printf("nanodesu\n");
        }
        else
        {
            printf("%s",s);
            printf("nanodesu\n");
        }
    }
    return 0;
}


你可能感兴趣的:(String,HDU,签到题)