解题思路:
排序很简单直接sort(),主要是输入输出;留作笔记;
参考代码:
#include<bits/stdc++.h> using namespace std; struct cards { int num; int color; }a[5]; bool ordn(struct cards& x, struct cards& y) { if (x.num < y.num) { return true; } else if (x.num == y.num) { if (x.color < y.color) return true; else return false; } else return false; } void fx(int x) { if (x == 1) cout<< 'd'; else if (x == 2) cout<< 'c'; else if (x == 3) cout<<'h'; else if (x == 4) cout<<'s'; } bool strcmp(char d, char f ) { return d==f; } int main() { char *nu; char s[11]; nu = s; cin >> s; for (int i = 0; i < 5; i++) { if (strcmp(*nu, '1')) { a[i].num = 10; nu += 2; } else if (strcmp(*nu, 'J')) { a[i].num = 11; nu++; } else if (strcmp(*nu, 'Q')) { a[i].num = 12; nu++; } else if (strcmp(*nu, 'K')) { a[i].num = 13; nu++; } else if (strcmp(*nu, 'A')) { a[i].num = 14; nu++; } else if(*nu>='2'&&*nu<='9') { a[i].num =(int)(*nu - '0'); nu++; } if (strcmp(*nu, 'd')) { a[i].color = 1; nu++; } else if (strcmp(*nu, 'c')) { a[i].color = 2; nu++; } else if (strcmp(*nu, 'h')) { a[i].color = 3; nu++; } else if (strcmp(*nu, 's')) { a[i].color = 4; nu++; } } sort(a, a + 5, ordn); for (int i = 0; i < 5; i++) { if (a[i].num == 11) { cout << 'J'; fx(a[i].color); } else if (a[i].num == 12) { cout << 'Q'; fx(a[i].color); } else if (a[i].num == 13) { cout << 'K'; fx(a[i].color); } else if (a[i].num == 14) { cout << 'A'; fx(a[i].color); } else { cout << a[i].num; fx(a[i].color); } if(i!=4)cout << " "; } }
0.0分
4 人评分