USASO Greedy Gift Givers

1. 文件输入输出好别扭;

2. 第一次没注意到cas--之后for循环就没作用了,得不到结果。

/*
ID: dollarzhaole
PROG: gift1
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Node
{
    string name;
    int sendp;//送给几个人
    int getm;//收到的钱
    int sendm;//送出去的钱
    int inim;//刚开始的钱
} node[11];
int cas;
int getname(string str)
{
    int num;
    for (int i = 0; i < cas; i++)
    {
        if (str == node[i].name)
        {
            num = i;
            break;
        }
    }
    return num;
}
int main()
{
    ofstream fout ("gift1.out");
    ifstream fin ("gift1.in");
    int i, j, curp, sendp;
    string str1, str2;
    fin >> cas;
    for (i = 0; i < cas; i++)
    {
        fin >> node[i].name;
        node[i].getm = 0;
        node[i].sendm = 0;
    }
    int n = cas;
    while (n--)
    {
        fin >> str1;
        curp = getname(str1);
        fin >> node[curp].inim >> node[curp].sendp;
        for (j = 0; j < node[curp].sendp; j++)
        {
            fin >> str2;
            sendp = getname(str2);
            node[sendp].getm += node[curp].inim / node[curp].sendp;
            node[curp].sendm += node[curp].inim / node[curp].sendp;
        }
    }
    for (i = 0; i < cas; i++)
    {
        cout << node[i].name << ' ' << node[i].getm - node[i].sendm << endl;
        fout << node[i].name << ' ' << node[i].getm - node[i].sendm << endl;
    }

    return 0;
}

下边是给的参考代码:

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

#define MAXPEOPLE 10
#define NAMELEN	32

typedef struct Person Person;
struct Person {
    char name[NAMELEN];
    int total;
};

Person people[MAXPEOPLE];
int npeople;

void
addperson(char *name)
{
    assert(npeople < MAXPEOPLE);
	strcpy(people[npeople].name, name);
    npeople++;
}

Person*
lookup(char *name)
{
    int i;

    /* look for name in people table */
    for(i=0; i<npeople; i++)
	if(strcmp(name, people[i].name) == 0)
	    return &people[i];

    assert(0);	/* should have found name */
}

int
main(void)
{
    char name[NAMELEN];
    FILE *fin, *fout;
    int i, j, np, amt, ng;
    Person *giver, *receiver;

    fin = fopen("gift1.in", "r");
    fout = fopen("gift1.out", "w");

    fscanf(fin, "%d", &np);
    assert(np <= MAXPEOPLE);

    for(i=0; i<np; i++) {
	fscanf(fin, "%s", name);
	addperson(name);
    }

    /* process gift lines */
    for(i=0; i<np; i++) {
	fscanf(fin, "%s %d %d", name, &amt, &ng);
	giver = lookup(name);

	for(j=0; j<ng; j++) {
	    fscanf(fin, "%s", name);
	    receiver = lookup(name);
	    giver->total -= amt/ng;
	    receiver->total += amt/ng;
	}
    }

    /* print gift totals */
    for(i=0; i<np; i++)
	printf("%s %d\n", people[i].name, people[i].total);
    exit (0);
}

原文链接: https://www.cnblogs.com/dollarzhaole/archive/2012/04/05/3188952.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    USASO Greedy Gift Givers

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/46185

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 下午10:36
下一篇 2023年2月8日 下午10:37

相关推荐