zoukankan      html  css  js  c++  java
  • pat乙级1067

    1、用cin输入数据后,再用getline 输入,还是会输入cin已经输入的数据,即cin和getline互相独立。

    2、题目中没有说尝试的密码不包含空格,因此不能用cin,而用getline。

     1 #include <iostream>
     2 #include <string>
     3 using namespace std;
     4 bool cmp(string a, string b)
     5 {
     6     if (a.size() != b.size())
     7         return false;
     8     else
     9     {
    10         for (int i = 0; i < a.size(); i++)
    11         {
    12             if (a[i] != b[i])
    13                 return false;
    14         }
    15     }
    16     return true;
    17 }
    18 int main()
    19 {
    20     string answer, s;
    21     int n;
    22     cin >> answer >> n;
    23     int count = 0;
    24     getline(cin, s);
    25     while (true)
    26     {
    27         getline(cin, s);
    28         if (s == "#") break;
    29         count++;
    30         bool equal = cmp(s, answer);
    31         if (equal)
    32         {
    33             cout << "Welcome in" << endl;
    34             break;
    35         }
    36         else
    37             cout << "Wrong password: " << s << endl;
    38         if (count == n)
    39         {
    40             cout << "Account locked" << endl;
    41             break;
    42         }
    43     }
    44     return 0;
    45 }
  • 相关阅读:
    HDU 4005 The war
    #undef
    [转载] #define new DEBUG_NEW
    [转载]常用正则表达式
    [百科]
    [转载]
    [转载]
    [转载]
    [百科]
    [转载]VC6中的文件后缀
  • 原文地址:https://www.cnblogs.com/lxc1910/p/8624686.html
Copyright © 2011-2022 走看看