Pig-Latin(ICPC冬令营集训题目)

Pig-Latin

Problem Description

You have decided that PGP encryptation is not strong enough for your email.
You have decided to supplement it by first converting your clear text letter into Pig Latin before encrypting it with PGP.

Input and Output

You are to write a program that will take in an arbitrary number of lines of text and output it in Pig
Latin. Each line of text will contain one or more words. A “word” is defined as a consecutive sequence
of letters (upper and/or lower case). Words should be converted to Pig Latin according to the following
rules (non-words should be output exactly as they appear in the input):

  1. Words that begin with a vowel (a, e, i, o, or u, and the capital versions of these) should just
    have the string “ay” (not including the quotes) appended to it. For example, “apple” becomes
    “appleay”.
  2. Words that begin with a consonant (any letter than is not A, a, E, e, I, i, O, o, U or u) should
    have the first consonant removed and appended to the end of the word, and then appending “ay”
    as well. For example, “hello” becomes “ellohay”.
  3. Do not change the case of any letter.
Sample Input
This is the input.
1
Sample Output
hisTay isay hetay inputay.
1
  • 输入和输出
  • 请您编写一个程序,输入任意数量行的文本,并以Pig Latin输出。 每行文本将包含一个或多个单词。一个“单词”被定义为一个连 续的字母序列(大写字母和/或小写字母)。单词根据以下的规则 转换为Pig Latin,非单词的字符在输出时则和输入中出现的完全 一样:
  • [1] 以元音字母(a、e、i、o或u,以及这些字母的大写形式)开 头的单词,要在其后面附加字符串“ay”(不包括双引号)。例 如,“apple”变成“appleay”。
  • [2] 以辅音字母(不是A, a, E, e, I, i, O, o, U 或 u的任何字母)开头 的单词,要去掉第一个辅音字母,并将之附加在单词的末尾,然 后再在单词的末尾加上“ay”。例如,“hello”变成“ellohay”。
  • [3] 不要改变任何字母的大小写。
Analysis of Test Questions

1.非字母字符,直接输出;如果是单词(一个连续的字母序列),则判定是否以辅音开头,如果以辅音开头把辅音字母放到单词最后。最后在单词后面加“ay”。
2.设计两个函数is(char c)判断字符c是否是字母,和vowel(char c)判断字符c是否是元音字母。

理解变量是理解程序的关键

AC代码
#include<iostream>
#include<cstdio>

using namespace std;

const int N=1000005;

char temp[N];

int isab(char c){//判断是否是字母
    if(c>='a'&&c<='z')return 1;
    if(c>='A'&&c<='Z')return 1;
    return 0;
}

int vowel(char c){//判断是否是元音字母
    if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u')
        return 1;
    if(c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
        return 1;
    return 0;
}

int main(){
    while(gets(temp)){
        int s=0,t=0;
        while(temp[s]){
            if(!isab(temp[s])){//不是字母,直接输出
                printf("%c",temp[s++]);
                t=s;
            }else if(isab(temp[t])){//是字母
                t++;
            }else {
                if(!vowel(temp[s])){//辅音字母开头
                    for(int i=s+1;i<t;i++)
                        printf("%c",temp[i]);
                    printf("%c",temp[s]);
                }else {//元音字母开头
                    for(int i=s;i<t;i++)
                        printf("%c",temp[i]);
                }
                printf("ay");
                s=t;
            }
        }
        printf("\n");
    }
    return 0;
}
posted @ 2021-01-26 18:09  独苍  阅读(193)  评论(0编辑  收藏  举报