作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在编写一个程序,该程序将为两名候选人计算每个县最多 4 票的选票。基本上它会要求您为每个候选人添加每个县的选票。然后它将计算选票并显示获胜者。这主要是有效的,但它不会将票数相加并正确宣布获胜者?我怀疑第 37 行到第 40 行的循环有问题。
#include<iostream>
using namespace std;
int tier1();
int main(void)
{
int return_val = tier1();
if (return_val < 0) // print an error
return 0;
}
int tier1(){
int votes[7];
int i, j, N; // variables
int k = 0;
for (i=0; i<4; i++)
{
cout << "county" << i << "\n"; // lists the 4 counties/candidates
for (j=0; j<2; j++)
{
cout << "How many votes did the candidate " << j << " get?\n";
N=0;
cin >> N;
votes[k++] = N;;
}
if (votes[k-2] + votes[k-1] > 100) //checking if it goes over 100 votes
{
cout << "County has too many votes. Exiting!\n"; // Print an error
exit(1);
}
}
int candidate0Votes = 0; //resetting
int candidate1Votes = 0;
for (i = 0; i < 7; i++)
{
cout << votes[i+1] << "\n";
cout << votes[i+1] << "\n";
candidate0Votes += votes[i+1];
candidate1Votes += votes[i+1];
}
if (candidate0Votes > candidate1Votes){
cout << "The winner of the election is candidate 0.\n";
}
else
{
cout << "The winner of the election is candidate 1.\n";
}
cout << "Here is the voting results:\n";
cout << "candidate 0 got ";
cout << candidate0Votes;
cout << " votes\n";
cout << "candidate 1 got ";
cout << candidate1Votes;
cout << " votes\n";
return 0;
}
让我知道是否还有其他我应该做的修改!谢谢!
最佳答案
您的投票数组很短。
看起来你有 4x2 个条目,但数组只有 7 个元素。
这是我可以接受的版本:
#include <iostream>
#include <numeric>
using namespace std;
int tier1();
int main(void) {
return tier1();
}
static int getnumber(std::string prompt) {
if (!std::cin)
return 0;
int i;
std::cout << prompt;
std::cin >> i;
while(!std::cin) {
if (std::cin.eof()) {
exit(1);
}
std::cin.clear();
std::cin.ignore(1<<30, '\n');
std::cout << prompt;
std::cin >> i;
}
return i;
}
#include <map>
#include <algorithm>
int tier1() {
using namespace std;
using county = int;
static constexpr int number_of_counties = 4;
static constexpr int number_of_candidates = 2;
map<county, size_t[number_of_candidates]> votes;
for (int cty = 0; cty < number_of_counties; cty++) {
for (int cand = 0; cand < number_of_candidates; cand++) {
votes[cty][cand] = getnumber("How many votes did the candidate " + to_string(cand) + " get in county " + to_string(cty) + "? ");
}
if (std::accumulate(begin(votes[cty]), end(votes[cty]), 0u) > 100)
cout << "County has too many votes. Exiting!\n"; // Print an error
}
size_t totals[number_of_candidates] = { 0 };
for(auto& cty: votes) {
for (auto in = begin(cty.second), accum = begin(totals); accum < end(totals);) {
*accum++ += *in++;
}
}
auto winner = max_element(begin(totals), end(totals));
cout << "The winner of the election is candidate " << distance(totals, winner) << "\n";
cout << "Here is the voting results:\n";
for (int cand = 0; cand < number_of_candidates; cand++) {
cout << "candidate " << cand << " got " << totals[cand] << " votes\n";
}
return 0;
}
关于c++ - 数组不会正确地添加选票?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27195560/
我是一名优秀的程序员,十分优秀!