zoukankan      html  css  js  c++  java
  • 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);
    }


  • 相关阅读:
    使用控制结构——循环语句——基本循环
    oracle字符类型 char,varchar2,varchar,clob,nvarchar,nclob
    使用控制结构——条件分支语句——多重条件分支
    hduoj 1518square
    运算符重载实现复数的加减乘除
    使用游标——参数游标
    开发PL/SQL子程序——触发器——编译触发器,删除触发器,显示触发器
    NYOJ58最少步数
    使用控制结构——条件分支语句——简单条件
    开发PL/SQL子程序——触发器——使用触发器注意事项
  • 原文地址:https://www.cnblogs.com/dollarzhaole/p/3188952.html
Copyright © 2011-2022 走看看