c++ - 卡住了问题集 3 复数。Check50 告诉我有问题,但是当我自己测试时,没有任何问题
问题描述
现在一直在学习cs50在线课程。我已经解决了这个问题并坚持了一周的复数问题。这是 check50 检查我的代码时的结果。我怎样才能摆脱这个我不知道它是什么的错误,因为当我自己测试它时,没有任何问题?
#include <cs50.h>
#include <stdio.h>
#include <string.h>
// Max number of candidates
#define MAX 9
// Candidates have name and vote count
typedef struct
{
string name;
int votes;
}
candidate;
// Number of candidates
int candidate_count;
// Array of candidates
candidate candidates[MAX];
// Function prototypes
void merge(int left, int mid, int right);
void merge_sort(int left, int right);
bool vote(string name);
void print_winner(void);
int main(int argc, string argv[])
{
// Check for invalid usage
if (argc < 2)
{
printf("Usage: plurality [candidate ...]\n");
return 1;
}
// Populate array of candidates
candidate_count = argc - 1;
if (candidate_count > MAX)
{
printf("Maximum number of candidates is %i\n", MAX);
return 2;
}
for (int i = 0; i < candidate_count; i++)
{
candidates[i].name = argv[i + 1];
candidates[i].votes = 0;
}
int voter_count = get_int("Number of voters: ");
// Loop over all voters
for (int i = 0; i < voter_count; i++)
{
string name = get_string("Vote: ");
// Check for invalid vote
if (!vote(name))
{
printf("Invalid vote.\n");
}
}
merge_sort(0, candidate_count - 1);
print_winner();
}
// Update vote totals given a new vote
bool vote(string name)
{
// TODO
string name1[candidate_count];
int votes1[candidate_count];
for (int i = 0; i < candidate_count; i++)
{
name1[i] = candidates[i].name;
}
int sum = 0;
for (int j = 0; j < candidate_count; j++)
{
if (strcmp(name, name1[j]) == 0)
{
sum = sum + 1;
}
else
{
sum = sum + 0;
}
}
for (int j = 0; j < candidate_count; j++)
{
if (strcmp(name, name1[j]) == 0)
{
votes1[j] = candidates[j].votes + 1;
}
else
{
votes1[j] = candidates[j].votes + 0;
}
}
for (int j = 0; j < candidate_count; j++)
{
candidates[j].votes = votes1[j];
}
if (sum == 1)
{
return true;
}
else
{
return false;
}
}
// Print the winner (or winners) of the election
void print_winner(void)
{
// TODO
string name2[candidate_count];
int votes2[candidate_count];
for (int i = 0; i < candidate_count; i++)
{
name2[i] = candidates[i].name;
votes2[i] = candidates[i].votes;
}
for (int j = 0; j < candidate_count; j++)
{
if (votes2[j] == votes2[candidate_count - 1])
{
printf("%s\n", name2[j]);
}
}
return;
}
void merge(int left, int mid, int right)
{
int len1 = mid - left + 1;
int len2 = right - mid;
int i;
int index1 = 0, index2 = 0;
int left_array[len1];
int right_array[len2];
string strleft_array[len1];
string strright_array[len2];
for (i = 0; i < len1; i++)
{
left_array[i] = candidates[left + i].votes;
strleft_array[i] = candidates[left + i].name;
}
for (i = 0; i < len2; i++)
{
right_array[i] = candidates[mid + 1 + i].votes;
strright_array[i] = candidates[mid + 1 + i].name;
}
i = left;
while (index1 < len1 && index2 < len2)
{
if (left_array[index1] < right_array[index2])
{
candidates[i].votes = left_array[index1];
candidates[i].name = strleft_array[index1];
index1++;
}
else
{
candidates[i].votes = right_array[index2];
candidates[i].name = strright_array[index2];
index2++;
}
i++;
}
while (index1 < len1)
{
candidates[i].votes = left_array[index1];
candidates[i].name = strleft_array[index1];
i++;
index1++;
}
while (index2 < len2)
{
candidates[i].votes = right_array[index2];
candidates[i].name = strright_array[index2];
i++;
index2++;
}
}
void merge_sort(int left, int right)
{
int mid;
int i;
if (left < right)
{
mid = (left + right) / 2;
merge_sort(left, mid);
merge_sort(mid + 1, right);
merge(left, mid, right);
}
}
解决方案
推荐阅读
- powershell - 是否可以使用 powershell 或批处理文件以不同的名称重命名文件夹中的每个文件?
- c# - 在 Windows 窗体应用程序 C# 中使用 Windows 服务应用程序
- python - 从python中的网页中提取电子邮件
- pytorch - CNN:准确率和损失在增加和减少
- firebase - Firebase 刷新用户记录
- django - 姜戈。将空字符串保存到 DateTimeField 时出错(空白 = True)
- r - 如何用大小= n的变量移动寡妇中的最大值替换矩阵中的每个元素?
- python - python的pandas在csv中保存df时创建多个索引列
- c++ - C++ makefile 项目的 Xcode“ld: library not found”
- laravel - Webpack / npm - 如何安装 jquery、bootstrap 和 boostrap-tables