|
描述: |
模拟n个人参加选举的过程,并输出选举结果:假设候选人有四人,分别用“A”、”B”、”C”、”D”表示, 选举时开始计票, 若输入的不是“A”、”B”、”C”、”D”则视为无效票。选举结束后获取各个候选人的 得票数以及无效票的个数,输出结果以添加候选人的顺序进行顺序输出,最后一行为无效的数量。 同时getVoteResult命令为结束命令。 |
|
运行时间限制: |
无限制 |
|
内存限制: |
无限制 |
|
输入: |
输入为多行形式输入,每一行为一条命令。输入的命令只会是有效命令不会有非法命令,但可能存在非法的投票,上面已经描述了。 添加候选人的命令如下:addCandidate为命令 xx1为候选人 addCandidate xx1 投票的命令如下:vote为命令 xx1为候选人的字符串 vote xx1 统计投票的命令如下:getVoteResult为命令 getVoteResult |
|
输出: |
输出结果以添加候选人的顺序进行顺序输出,最后一行为无效的数量。 |
|
样例输入: |
addCandidate xx1 addCandidate xx2 addCandidate xx3 addCandidate xx4 addCandidate xx5 addCandidate xx6 vote xx1 vote xx3 vote xx4 vote xx1 vote xx2 vote xx7 vote xx4 vote xx5 vote xx3 vote xx2 vote xx1 vote xx7 getVoteResult |
|
样例输出: |
xx1 3 xx2 2 xx3 2 xx4 2 xx5 1 xx6 0 2 |
#include <iostream>
#include <string>
#include <map>
#include <iterator>
#include <vector>
using namespace std;
int main()
{
map<int, int> votes; //第一项用编号int代替候选人string
vector<string> candidates; //候选人按先后顺序,就自然有了编号
string cmd;
string candidate;
int candiNum = 0;
int size;
int ilegal = 0;
//while ((cin >> cmd) != "getVoteResult")
//错误,">>"操作符返回其左操作数作为结果,返回的是cin对象,流本身
while ((cin >> cmd) && cmd != "getVoteResult")
{
cin >> candidate;
if (cmd == "addCandidate")
{
candidates.push_back(candidate);
votes.insert(pair<int, int>(candiNum ++, 0));
}
else if (cmd == "vote")
{
size = candidates.size();
int i = 0;
for (; i < size; i++)
{
if (candidates.at(i) == candidate)
{
++ votes[i];
break;
}
if (i == size - 1) //放在for循环里面的话,要判断的次数增多,放到for外面的话,vote最后一个元素的每次投票都要算一次ilegal
++ ilegal;
}
}
}
map<int, int>::iterator it = votes.begin();
for (int i = 0; i < size; ++ i, ++ it)
{
cout << candidates.at(i) << " " << it ->second << endl;
}
cout << ilegal << endl;
//system("pause");
return 0;
}|
描述: |
输入一串数字,找到其中包含的最大递增数。递增数是指相邻的数位从小到大排列的数字。 如: 2895345323,递增数有:289,345,23, 那么最大的递增数为345。 |
|
运行时间限制: |
无限制 |
|
内存限制: |
无限制 |
|
输入: |
输入一串数字,默认这串数字是正确的,即里面不含有字符/空格等情况 |
|
输出: |
输出最大递增数 |
|
样例输入: |
123526897215 |
|
样例输出: |
2689 |
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
cin >> str;
str += '0'; //这个地方加个0有点非常规,但是是个小技巧
string strMax;
strMax += str[0];
string tmp = strMax;
for (int i = 0; i < str.size()-1; ++i)
{
if (str[i] < str[i+1]) //调试发现到最后一个数字时,continue就停止了,如果最大递增字串在最后就不能被正确的发现,于是在原串末尾加个‘0’
{
tmp += str[i+1];
continue;
}
else if (tmp.size() > strMax.size() || (tmp.size() == strMax.size() && tmp > strMax))
strMax = tmp;
tmp = str[i+1];
}
cout << strMax << endl;
//system("pause");
}|
描述: |
Word Maze 是一个网络小游戏,你需要找到以字母标注的食物,但要求以给定单词 字母的顺序吃掉。如上图,假设给定单词if,你必须先吃掉i然后才能吃掉f。
里面到处都是以字母标注的食物,但你只能吃掉能连成给定单词W的食物。
注意区分英文字母大小写,你只能上下左右行走。 |
|
运行时间限制: |
无限制 |
|
内存限制: |
无限制 |
|
输入: |
输入第一行包含两个整数n、m(0<n, m<21)分别表示n行m列的矩阵,第二行是长度不超过 100的单词W,从第3行到底n+3行是只包含大小写英文字母的长度为m的字符串。 |
|
输出: |
如果能在地图中连成给定的单词,则输出“YES”,否则输出“NO”。注意:每个字母只能用一次。 |
|
样例输入: |
5 5 SOLO CPUCY EKLQH CRSOL EKLQO PGRBC |
|
样例输出: |
YES |
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string word; //单词
vector<string> charMap; //字母矩阵
vector<vector<int> > visited; //访问过的字母矩阵位置标记
bool isFind = false; //判断是否找到单词
int n, m; //n行m列的矩阵
int direction[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; //注意里面不能用小括号
void DFS(int i, int j, int depth)
{
if (word.size() == depth)
{
isFind = true;
return;
}
for (int k = 0; k < 4; ++k)
{
int r = i + direction[k][0];
int c = j + direction[k][1];
if (r >= 0 && r <= n-1 && c >= 0 && c <= m-1 && visited[r][c] == 0 && word[depth] == charMap[r][c])
{
visited[r][c] = 1;
DFS(r, c, depth + 1);
visited[r][c] = 0;
}
}
}
int main()
{
cin >> n >> m;
cin >> word;
visited = vector<vector<int> > (n, vector<int> (m, 0) );
for (int i = 0; i < n; ++i)
{
string tmp;
cin >> tmp;
charMap.push_back(tmp);
}
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
if (word[0] == charMap[i][j] && visited[i][j] == 0)
{
visited[i][j] = 1;
DFS(i, j, 1);
visited[i][j] = 0;
}
}
}
cout << ( isFind ? "YES" : "NO" ) << endl;
//system("pause");
return 0;
}
本文探讨了C++编程中的常见机试题,包括数据结构、算法应用和内存管理等方面,旨在提升C++开发者的问题解决能力。
1384

被折叠的 条评论
为什么被折叠?



