PAT 1067 试密码 (20 分)

传送门

思路:

1.先把输入的字符串全部存起来,读到#为止;

2.对字符串逐个判断,并做相应输出。

注意:

1.密码正确并次数没有超过限制时要结束程序;

2.冒号后面有空格;

3.如果密码错误且已经到达输入次数限制要先输出 Wrong password: 用户输入的错误密码,再输出 Account locked。

附上AC代码:

 1 #include<iostream>
 2 #include <iomanip>
 3 #include<cstdio>
 4 using namespace std;
 5 
 6 string str[1000];//用来存输入的字符串 
 7 
 8 int main(){
 9     string s;
10     int n,cnt=0;
11     cin>>s;
12     cin>>n;
13     cin.get(); //读取掉回车 
14     while(1){
15         string t;
16         getline(cin,t);
17         str[cnt++]=t;
18         if(t=="#") break;  //读到#就结束 
19     }
20     for(int i=0;i<cnt-1;i++){  //循环最多到#前面一个字符串结束 
21         if(str[i]==s){
22             cout<<"Welcome in"<<endl;  //密码正确并结束 
23             break;
24         }
25         else cout<<"Wrong password: "<<str[i]<<endl;
26         if(i==n-1){    //如果到达输入次数限制,输出 Account locked并且结束 
27             cout<<"Account locked";
28             break;
29         }
30     }
31     return 0;
32 } 

ACM小白,有诸多不足,欢迎指正。

猜你喜欢

转载自www.cnblogs.com/anitena/p/11347334.html
今日推荐