用了map,感觉不错,结果~~~~~

P4414 [COCI2006-2007#2] ABC

Lxhaxx @ 2024-10-13 15:40:03


#include<iostream>
#include<string>
#include<iomanip>
#include<map>
#include<utility>
#include<algorithm>
#define pi 3.141593
using namespace std;
int main() {
    int a[3];
    string s;
    map<char, int>mp;
    for (int i = 0; i < 3; i++) {
        cin >> a[i];
    }
    cin >> s;
    sort(a, a + 3);
    mp.insert(pair<char,int>('A',a[0]));
    mp.insert(pair<char,int>('B',a[1]));
    mp.insert(pair<char,int>('C',a[2]));
    for (int i = 0; i < 3; i++) {
        cout << mp[s[i]]<<setw(2);
    }
    cout << endl;
    return 0;
}
``` ```

by Luiruixi @ 2024-10-13 15:53:24

妙啊~~


by Lxhaxx @ 2024-10-13 15:57:18

我知道为什么了,我用了setw(2)占位,如果是两位数就会出现数字连在一起的情况,直接把setw改成" "就可以了


by Lxhaxx @ 2024-10-13 15:59:36

#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<map>
#include<utility>
#include<algorithm>
#define pi 3.141593
using namespace std;
int main() {
    int a[3];
    string s;
    map<char, int>mp;
    for (int i = 0; i < 3; i++) {
        cin >> a[i];
    }
    cin >> s;
    sort(a, a + 3);
    mp.insert(pair<char,int>('A',a[0]));
    mp.insert(pair<char,int>('B',a[1]));
    mp.insert(pair<char,int>('C',a[2]));
    for (int i = 0; i < 3; i++) {
        cout << mp[s[i]]<<" ";
    }
    cout << endl;
    return 0;
} 

|