题解 | #登录验证#
登录验证
https://www.nowcoder.com/practice/799daf76369c4fb1a2e2d56f885225ae
C++写法
#include <cstring> #include <iostream> using namespace std; int main() { string username, password; while (cin >> username >> password) { if (username == "admin" && password == "admin") cout << "Login Success!"; else cout << "Login Fail!"; } }
C的写法
#include <string.h> #include <stdio.h> int main() { char username[6], password[6]; while (scanf("%s %s", username, password) != EOF) { if (strcmp(username, "admin") == 0 && strcmp(password, "admin") == 0) cout << "Login Success!"; else cout << "Login Fail!"; } }
注意使用strcmp函数两个字符串相等的时候返回的是0。
C++题解 文章被收录于专栏
记录在牛客网用C++刷题的题解思路