习题5-4 交换学生(Foreign Exchange, UVa 10763)

来源:互联网 发布:python 中文字符串 编辑:程序博客网 时间:2024/05/22 08:55

解题用的是map,我的做题思路是先把所有数字都存下来,然后开始一个个找匹配项,一旦匹配上就做好标记,已经匹配上的两对数据就不和其他数据再进行匹配了,如果全部匹配成功就return success~

看的是刘汝佳的书没看原题,输入输出的控制可能不标准。

#include<iostream>#include<map>#include<cmath>using namespace std;int main(){    int n, a, b;    cin >> n;    map<int, int> match;    while(n--){        cin >> a >> b;        match[a] = b;    }    bool success = true;    for(map<int, int>::iterator it = match.begin(); it != match.end(); it++){        if(!match[it->second]){            success = false;            break;        }    }    if(success)        cout << "SUCCESS" << endl;    else        cout << "FAIL" << endl;    return 0;}



原创粉丝点击