天天看点

The Hardest Problem Ever HDU - 1048

Julius Caesar lived in a time of danger and intrigue. The hardest situation Caesar ever faced was keeping himself alive. In order for him to survive, he decided to create one of the first ciphers. This cipher was so incredibly sound, that no one could figure it out without knowing how it worked.

You are a sub captain of Caesar's army. It is your job to decipher the messages sent by Caesar and provide to your general. The code is simple. For each letter in a plaintext message, you shift it five places to the right to create the secure message (i.e., if the letter is 'A', the cipher text would be 'F'). Since you are creating plain text out of Caesar's messages, you will do the opposite:

Cipher text

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Plain text

V W X Y Z A B C D E F G H I J K L M N O P Q R S T U

Only letters are shifted in this cipher. Any non-alphabetical character should remain the same, and all alphabetical characters will be upper case.

Input

Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following description, and there will be no blank lines separating data sets. All characters will be uppercase.

A single data set has 3 components:

Start line - A single line, "START"

Cipher message - A single line containing from one to two hundred characters, inclusive, comprising a single message from Caesar

End line - A single line, "END"

Following the final data set will be a single line, "ENDOFINPUT".

Output

For each data set, there will be exactly one line of output. This is the original message by Caesar.

Sample Input

START
NS BFW, JAJSYX TK NRUTWYFSHJ FWJ YMJ WJXZQY TK YWNANFQ HFZXJX
END
START
N BTZQI WFYMJW GJ KNWXY NS F QNYYQJ NGJWNFS ANQQFLJ YMFS XJHTSI NS WTRJ
END
START
IFSLJW PSTBX KZQQ BJQQ YMFY HFJXFW NX RTWJ IFSLJWTZX YMFS MJ
END
ENDOFINPUT      

Sample Output

IN WAR, EVENTS OF IMPORTANCE ARE THE RESULT OF TRIVIAL CAUSES
I WOULD RATHER BE FIRST IN A LITTLE IBERIAN VILLAGE THAN SECOND IN ROME
DANGER KNOWS FULL WELL THAT CAESAR IS MORE DANGEROUS THAN HE      

骤然间发现自己真的好菜啊,自己对于这些输入输出确实没怎么注意,还有就是一个返回值的问题。之前没怎么考虑过gets()的返回值,现在才发现原来还是

蛮有用的,还就是分块的思想,清楚每一块都是什么,怎么来区分。

#include <iostream>
#include<cstdio>
#include<string.h>
using namespace std;

int main()
{
    char st[10]="START";
    char str[250];
    while( gets(str))
    {
        if(!strcmp(str,st))
        {
           gets(str);
           int len=strlen(str);
           for(int i=0;i<len;i++)
          {
            if(str[i]<='Z'&&str[i]>='A')
            {
                if(str[i]<='E')
                    str[i]=str[i]-5+26;
                else
                    str[i]=str[i]-5;
            }
          }
           puts(str);
           gets(str);
        }
        else
          break;
    }
    return 0;
}
           

转载:

gets函数的读取规则:

1.只要gets遇到换行符,即便它是输入的第一个字符,gets也会停止读入并返回。如果输入的第一个字符就是换行符,则字符串将被置为空串。

2.由于gets函数读入再丢弃换行符,换行符将不会存储在字符串中。

gets的读取规则与getline是一样的。

gets函数的返回值:

1.正常读入(未遇到文件结尾)(注意,读入空串也是正常读入),则返回字符串,即返回字符串首地址,char*类型

2.遇到文件结尾,返回NULL

Ps:gets函数的原型是char * gets(char *s);当读取成功的时候返回s.

思考一:“gets遇到文件结尾标志时返回NULL”表示gets函数没有获得任意数据之前如果遇到文件结尾标志将返回NULL。

继续阅读