解题思路:
排序很简单直接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 人评分
C语言程序设计教程(第三版)课后习题6.11 (C语言代码)for循环浏览:1099 |
C语言训练-排序问题<1> (C语言代码)浏览:599 |
C语言程序设计教程(第三版)课后习题4.9 (C语言代码)浏览:625 |
2004年秋浙江省计算机等级考试二级C 编程题(1) (C语言代码)浏览:500 |
关于C语言变量位置的问题浏览:272 |
1013题解浏览:552 |
简单的a+b (C语言代码)浏览:943 |
C二级辅导-同因查找 (C语言代码)浏览:563 |
C语言程序设计教程(第三版)课后习题7.2 (C语言代码)浏览:740 |
字符逆序 (C语言代码)浏览:504 |