chinanoahli 2015-04-20 01:50 采纳率: 100%
浏览 1923
已采纳

C語言 请教bus error 10,USACO Greedy Gift Givers

代碼如下

/*
LANG: C
TASK: gift1
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


FILE *input_file, *output_file;

int total_people, gift, numbers_of_recever, money_giving[9];

int scan_names_counter, scan_recever_counter, recever_in_names, money[9], money_receving[9], splid_the_gift, how_many_waves_gift_been_send;

char names[9][13], recever[9][13], the_giver[13], the_recever[13];


void scan_names(){
        fscanf(input_file, "%d", &total_people);

        scan_names_counter=0;
        while(scan_names_counter<total_people){
                fscanf(input_file, "%s", names[scan_names_counter]);
                scan_names_counter++;
        }
}


void givers_money(){
        scan_names_counter=0;
        fscanf(input_file, "%s", the_giver);
        fscanf(input_file, "%d%d", &gift, &numbers_of_recever);
        while(strcmp(the_giver, names[scan_names_counter])!=0){
                scan_names_counter++;
        }
        money_giving[scan_names_counter]=money_giving[scan_names_counter]-gift;
        if(numbers_of_recever!=0){
                splid_the_gift=gift/numbers_of_recever;
        }else{
                splid_the_gift=0;
        }
}


void receive_the_money(){
        scan_recever_counter=0;
        while(scan_recever_counter<numbers_of_recever){
                fscanf(input_file, "%s", the_recever);
                scan_names_counter=0;
                while(scan_names_counter<total_people){
                        if(strcmp(the_recever, names[scan_names_counter])==0){
                                money_receving[scan_names_counter]=money_receving[scan_names_counter]+splid_the_gift;
                        }
                        scan_names_counter++;
                }
                scan_recever_counter++;
        }
}


void money_counter(){
        scan_names_counter=0;
        while(scan_names_counter<total_people){
                money[scan_names_counter]=money_receving[scan_names_counter]+money_giving[scan_names_counter];
                scan_names_counter++;
        }
}


void print_gift_tab(){
        scan_names_counter=0;
        while(scan_names_counter<total_people){
                fprintf(output_file, "%s %d\n", names[scan_names_counter], money[scan_names_counter]);
                scan_names_counter++;
        }
}


int main(){
        input_file = fopen("gift1.in", "r");
        output_file = fopen("gift1.out", "w+");


        scan_names();
        how_many_waves_gift_been_send=0;
        while(how_many_waves_gift_been_send<total_people){
                givers_money();
                receive_the_money();
                how_many_waves_gift_been_send++;
        }
        money_counter();
        print_gift_tab();


        fclose(input_file);
        fclose(output_file);
        return 0;
} 

讀下面2個文件
文件1:正常結束並輸出正確的數據到文件

5
dave
laura
owen
vick
arm
dave
200 3
laura
owen
vick
owen
500 1
dave
arm
150 2
vick
owen
laura
0 2
arm
vick
vick
0 0 

文件2:不能正常輸出 bus error 10

10
mitnik
Poulsen
Tanner
Stallman
Ritchie
Baran
Spafford
Farmer
Venema
Linus
mitnik
300 3
Poulsen
Tanner
Baran
Poulsen
1000 1
Tanner
Spafford
2000 9
mitnik
Poulsen
Tanner
Stallman
Ritchie
Baran
Farmer
Venema
Linus
Tanner
1234 1
Poulsen
Stallman
536 3
Farmer
Venema
Linus
Ritchie
2000 1
mitnik
Baran
79 2
Tanner
Farmer
Farmer
0 0
Venema
12 9
mitnik
Poulsen
Tanner
Stallman
Ritchie
Baran
Spafford
Farmer
Linus
Linus
1000 1
mitnik 

英語一般,所以在命名變量的時候可能有點語法錯誤,請勿見笑
因為是初學,所以還請各位詳細指點!!

  • 写回答

1条回答 默认 最新

  • threenewbee 2015-04-20 01:58
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化