题目样例有毒
密码翻译
http://www.nowcoder.com/questionTerminal/136de4a719954361a8e9e41c8c4ad855
看到样例有
1 Hello! How are you!
以为要这么输入
while( scanf("%d",&n) )
{
while( n-- )
{
string str;
getline(cin,str,'\n');
getchar();
int len=str.size();
cout<<str<<endl;
for(int i=0; i<len; ++i)
{
if( isalpha(str[i]) )
{
str[i]=solve[ str[i] ];
}
}
cout<<str<<endl;
}
} 然后发现,根本不是,这题目样例有毒。。
一、C++用getline过了的代码
过不了样例,却AC了。。。
#include<bits/stdc++.h>
using namespace std;
char solve[256];
void init( )
{
for(int i=0; i<256; ++i)
{
solve[i]=i;
}
for(int i='a'; i<='y'; ++i )
{
solve[i]=solve[i+1];
}
solve['z']='a';
for(int i='A'; i<='Y'; ++i)
{
solve[i]=solve[i+1];
}
solve['Z']='A';
}
int n;
int main()
{
init();
string str;
while( getline(cin,str,'\n') )
{
getchar();
int len=str.size();
for(int i=0; i<len; ++i)
{
if( isalpha(str[i]) )
{
str[i]=solve[ str[i] ];
}
}
cout<<str<<endl;
str.clear();
}
return 0;
} 二、C语言中fget做格式输入
char * fgets ( char * str, int num, FILE * stream );
用法:
从流中获取字符串
从流中读取字符,并将它们作为C字符串存储到str中,直到已读取(num -1)个字符或到达换行符或到达文件末尾为止,以先发生的为准。
换行符使fgets停止读取,但是该函数将其视为有效字符并包含在复制到str的字符串中。
复制到str的字符后会自动附加一个终止的空字符。
请注意,fgets与gets完全不同:不仅fgets接受流参数,但还可以指定str的最大大小,并在字符串中包括任何结尾的换行符。
别人AC的代码@https://www.nowcoder.com/profile/521575151
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char buf[256];
int big_int;
int main(void)
{
int x,y,i,j,flag,n,m,big,big2;
//xue
while(NULL!=fgets(buf,256,stdin))
{
big=strlen(buf);
for(i=0;i<big;i++)
{
char a=buf[i];
if(a=='z')
putchar('a');
else if(a=='Z')
putchar('A');
else if((a>='a'&&a<'z')||(a>='A'&&a<'Z'))
putchar(a+1);
else
putchar(a);
}
}
return 0;
} 
