时间限制: 1000 ms 空间限制: 262144 KB 具体限制
题目描述
使用Wifi上网时,通常需要输入正确的密码之后,才能登录。假设输入密码没有次数限制(密码通常为八个字符,假设预置密码为NOIP@CCF)。
请你编写一个程序,模拟使用Wifi上网的登录过程:用户尝试输入密码,直到自己要求结束或者密码正确。
输入
输入包含若干行尝试登录信息,每一次尝试对应两行或一行输入:
第一行,一个字符"Y"或"N ",表示是否继续登录
第一行为"Y"时,则还需要输入第二行,八位字符,表示要尝试的密码
输出
输出仅一行:密码是否正确的提示信息"Success"或"Sorry"
样例输入
输入1: Y cctv@CCF Y NOIP@CCF 输入2: Y cctv@CCF N
样例输出
输出1: Sorry Success 输出2: Sorry
题记:
字符串处理的水题,可以借用<string.h>的函数,不用也可以。
下面提供两个代码。
第一个提交60……求大佬指教
第二个100。
C++程序如下:
1.
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 8;
char correct[N+5]={'N', 'O', 'I', 'P', '@', 'C', 'C', 'F'};
string user;
int main(void){
char s;
int len;
while(cin >> s){
if(s != 'Y')
break;
else{
int flag = 1;
cin >> user;
len = user.size();
if(len != N)
flag = 0;
else
for(int i=0; i<N; i++)
if(correct[i] != user[i])
flag = 0;
if(flag == 1)
cout << "Success" << endl;
else
cout << "Sorry" << endl;
}
}
return 0;
}
2.
#include <stdio.h>
#include <string.h>
#define N 10
char password[] = "NOIP@CCF";
char yes[] = "Y";
char no[] = "N";
char score[N];
int main(void)
{
for(;;) {
if(scanf("%s", score) == EOF)
break;
if(strcmp(score, no) == 0)
break;
else if(strcmp(score, yes) == 0) {
if(scanf("%s", score) == EOF)
break;
if(strcmp(score, password) == 0) {
printf("Success\n");
break;
} else
printf("Sorry\n");
}
}
return 0;
}