原题链接:蓝桥杯算法提高VIP-洗牌
解题思路:
注意事项:
参考代码:
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <vector> #include <algorithm> #include <stdio.h> #include <string> using namespace std; void splitStr(string str, char c, vector<string>& strList) { int index1 = 0; int index2 = index1; int len = str.length(); while (index1 < len && (index2 = str.find(c,index1)) != string::npos) { string temp = str.substr(index1, index2 - index1); strList.push_back(temp); index1 = index2 + 1; } if (index1 < len) { string temp = str.substr(index1, len - index1); strList.push_back(temp); } remove(strList.begin(), strList.end(), ""); //strList.shrink_to_fit(); } int main() { string temp; vector<string> cardArr, washedCardArr; while (getline(cin, temp)) splitStr(temp, ' ', cardArr); if (cardArr.size() < 52) { cout << -1; return 1; } for (int i = 0; i < 52; i++) { string curCard = cardArr[i]; int curCardNo = 0; if (curCard == "10") curCardNo = 10; else if (curCard == "J") curCardNo = 11; else if (curCard == "Q") curCardNo = 12; else if (curCard == "K") curCardNo = 13; else curCardNo = curCard[0] - '0'; if (washedCardArr.size() >= curCardNo) washedCardArr.insert(washedCardArr.begin() + curCardNo, curCard); else washedCardArr.push_back(curCard); } for (vector<string>::iterator it = washedCardArr.begin(); it < washedCardArr.end(); it++) cout << *it << " "; cout << endl; return 0; }
0.0分
0 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复