PTA乙级 (1067 试密码 (20分))
1067 试密码 (20分)
https://pintia.cn/problem-sets/994805260223102976/problems/994805266007048192
第一次:测试点四没过
测试点4是一个特殊情况,就是题目的这句描述:
如果是错误的,则在一行中按格式输出 Wrong password: 用户输入的错误密码;当错误尝试达到 N 次时,再输出一行 Account locked,并结束程序。
第二次:AC
方法一:
1 #include <iostream> 2 #include <string> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #include <cstdio> 7 using namespace std; 8 int main() 9 { 10 string str,str1; 11 int n; 12 cin>>str>>n; 13 str1=str; 14 getchar(); 15 int count=0; 16 while((getline(cin,str))&&(str!="#")) 17 { 18 if (str.compare(str1) == 0) { 19 cout << "Welcome in" << endl; break; 20 } 21 else { 22 cout << "Wrong password: " << str << endl; n--; 23 } 24 if (n == 0) { cout << "Account locked"<<endl;return 0; } 25 } 26 return 0; 27 }
方法二:(参考https://blog.csdn.net/ZiYuTongXue/article/details/101423509)
1 #include<vector> 2 #include<string> 3 #include<iostream> 4 using namespace std; 5 int main() { 6 string passwrold, test; 7 vector<string>V; 8 int N; 9 cin >> passwrold >> N; 10 cin.ignore(); 11 getline(cin, test); 12 while (test.compare("#") != 0) { 13 V.push_back(test); 14 getline(cin, test); 15 } 16 for (int i = 0; i < V.size(); i++) { 17 18 if (V[i].compare(passwrold) == 0) { 19 cout << "Welcome in" << endl; break; 20 } 21 else { 22 cout << "Wrong password: " << V[i] << endl; N--; 23 } 24 if (N == 0) { cout << "Account locked"<<endl;return 0; } 25 } 26 return 0; 27 }
天晴了,起飞吧