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);
    }


  • 相关阅读:
    蓝桥杯——一步之遥,扩展gcd的应用
    质数唯一分解定理应用——多个数的最小公约数和最大公倍数
    蓝桥杯训练 ——天平称重
    蓝桥杯 奇怪的捐赠——进制的运用
    Kubernetes-基本概念
    Docker-基本概念
    虚拟化向容器化发展
    Kubernetes-服务连接和暴露(endpoints资源)
    Kubernetes-NodePort
    Kubernetes-Load Balancer
  • 原文地址:https://www.cnblogs.com/dollarzhaole/p/3188952.html
Copyright © 2011-2022 走看看