You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言编程:矩阵列总计与百分比计算问题求助

C语言矩阵投票统计解决方案

Hey there, I totally get how frustrating it is to get stuck on matrix calculations in C—especially when you’re not super comfortable with the language yet. Let’s break this down step by step, starting with the core parts you need for your program.

第一步:定义清晰的数据结构

First, let’s set up variables to hold our voting data, plus labels for districts and candidates. This keeps everything organized:

#include <stdio.h>

// 定义常量,方便后续修改
#define NUM_DISTRICTS 3
#define NUM_CANDIDATES 4

int main() {
    // 选区名称
    char *district_names[NUM_DISTRICTS] = {"District 1", "District 2", "District 3"};
    // 候选人名称
    char *candidate_names[NUM_CANDIDATES] = {"Alice", "Bob", "Charlie", "Diana"};
    // 二维数组存储各选区的投票数
    int votes[NUM_DISTRICTS][NUM_CANDIDATES] = {
        {120, 85, 90, 45},
        {95, 110, 75, 60},
        {80, 70, 125, 55}
    };

第二步:展示带行列标签的表格

To print a clean, labeled table, we’ll use formatted printf statements to align columns properly. We’ll first print the candidate names as the top row, then each district’s votes:

// 打印表头(候选人名称)
    printf("%15s", ""); // 空出第一列的选区标签位置
    for (int i = 0; i < NUM_CANDIDATES; i++) {
        printf("%10s", candidate_names[i]);
    }
    printf("\n");

    // 打印分割线
    printf("%15s", "");
    for (int i = 0; i < NUM_CANDIDATES; i++) {
        printf("----------");
    }
    printf("\n");

    // 打印每一行的选区数据
    for (int district = 0; district < NUM_DISTRICTS; district++) {
        printf("%15s", district_names[district]);
        for (int candidate = 0; candidate < NUM_CANDIDATES; candidate++) {
            printf("%10d", votes[district][candidate]);
        }
        printf("\n");
    }

第三步:计算候选人总票数与百分比

Next, we’ll calculate total votes for each candidate, plus the overall total votes to compute percentages. Important note: We need to cast integers to floats to avoid integer division errors!

// 计算总投票数
    int total_votes = 0;
    int candidate_totals[NUM_CANDIDATES] = {0}; // 存储每位候选人的总票数

    // 遍历矩阵,计算候选人总票和总投票数
    for (int district = 0; district < NUM_DISTRICTS; district++) {
        for (int candidate = 0; candidate < NUM_CANDIDATES; candidate++) {
            candidate_totals[candidate] += votes[district][candidate];
            total_votes += votes[district][candidate];
        }
    }

    // 打印候选人总票数和百分比
    printf("\n\n");
    printf("%15s%10s%15s\n", "Candidate", "Total Votes", "Percentage");
    printf("--------------------------------------------------\n");
    for (int i = 0; i < NUM_CANDIDATES; i++) {
        // 计算百分比:转成浮点型再计算,避免整数除法
        float percentage = ((float)candidate_totals[i] / total_votes) * 100;
        printf("%15s%10d%14.1f%%\n", candidate_names[i], candidate_totals[i], percentage);
    }

    return 0;
}

关键注意事项

  • 对齐问题: Using %15s, %10d etc. in printf ensures columns line up neatly—adjust the numbers if you need wider/narrower columns.
  • 整数除法: If you don’t cast candidate_totals[i] to float, dividing two integers will truncate the decimal (e.g., 5/2 becomes 2 instead of 2.5), which breaks your percentage calculation.
  • 可扩展性: Using #define for the number of districts/candidates means you can easily modify the program later without changing every loop condition.

If you had more requirements (part c that you didn’t finish writing), feel free to expand on them and we can tweak this code further!

内容的提问来源于stack exchange,提问作者Hatcamel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:27:18