USACO Greedy Gift Givers 解题心得

本题算法不难想出,但是中间还是出现了一些问题。

开始的时候是#11:Execution error,后来把普通的数组改成动态数组后问题消失。

后来又出现了Execution error: Your program had this runtime error: Illegal file open (/dev/tty)。

随后google解决方案,多数都是数组开小了。遂开大数组,无效。

突然意识到很有可能是低级错误,于是检查代码。

发现写了个

for(int i = 0; i < n2 ; i++)
delete[] d;

一开始没有注意到是因为这些错误编译器(ClodeBlocks)都会无视而成功编译,本地可以成功运行.

删掉循环,上传,通过。

有时候错误可能并不是和预设地一样,但是通过逻辑检查应该还是可以检查出来。

自己本地写代码,应该尽可能严谨,而不是只求编译器通过。

最后的代码:

/*ID: bluemit1PROG: gift1LANG: C++*/#include <iostream>#include <fstream>#include <cstring>using namespace std;
int main() {    ofstream fout ("gift1.out");    ifstream fin ("gift1.in");    int n;    fin >> n ;    char *a0 = new char[n*18];    char **a;    a = new char *[n];    for(int i=0;i<n;++i)    a[i] = a0 + i*18;    char c[18];int *b=new int [n];    for(int i=0;i<n;i++)    {        b[i]=0;    }    for(int i=0;i<n;i++)        for(int j=0;j<18;j++)        {            a[i][j]=‘\0‘;        }    for (int i=0;i<n;i++)    {        fin>>a[i];    }
    for(int i=0;i<18;i++)    {        c[i]=‘\0‘;    }    fin>>c;    while(c[0]!=‘\0‘)    {        int sum,n2;        fin>>sum>>n2;
        if(n2!=0)        {            char *d0 = new char[n2*18];            char **d;            d = new char *[n2];            for(int i=0;i<n2;++i)            d[i] = d0 + i*18;            for(int i=0;i<n2;i++)            for(int j=0;j<18;j++)            {                d[i][j]=‘\0‘;            }            for(int i=0;i<n2;i++)            {                fin>>d[i];            }            for(int i=0;i<n2;i++)            {                for(int j=0;j<n;j++)                {
                if(strcmp(a[j],d[i])==0)                    {
                        b[j]+=(sum/n2);
                    }
                }            }
            for(int j=0;j<n;j++)            {                if(strcmp(a[j],c)==0)                {                    b[j]-=sum;                    b[j]+=sum%n2;                }            }
            delete[] d;            delete [] d0;
        }        for(int i=0;i<18;i++)        {            c[i]=‘\0‘;        }        fin>>c;
    }
    for(int i=0;i<n;i++)    {        fout<<a[i]<<" "<<b[i]<<endl;    }
    delete[] a;    delete b;    return 0;}
时间: 2024-08-26 13:04:55

USACO Greedy Gift Givers 解题心得的相关文章

usaco Greedy Gift Givers

/* ID: modengd1 PROG: gift1 LANG: C++ */ #include <iostream> #include<string.h> #include<stdio.h> using namespace std; char names[30][20]; int acount[30]; int counter,N; int getIndex(char name[20]) { for(int i=0;i<counter;i++) { if(st

USACO 1.1 Greedy Gift Givers

Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts of money. Each of these friends might or might not give some money to any or all of the other friends. Likewise, each friend might or might not receiv

USACO Train 1.1.2 Greedy Gift Givers

这道题大意就是几个人互送礼物,让你求每个人的盈利. 原题给的样例数据: 5(人的个数.) =========(下面是人名,输出按照这顺序)davelauraowenvickamr ==========(下面是每个人的要给的人)dave200 3lauraowenvick ----------owen500 1dave ----------amr150 2vickowen -----------laura0 2amrvick ----------vick0 0 这题使用模拟算法就行了,就是注意输入

贪婪的送礼者Greedy Gift Givers

洛谷——P1201 [USACO1.1]贪婪的送礼者Greedy Gift Givers codevs——1381 贪婪的礼物送礼者  USACO 题目描述 对于一群(NP个)要互送礼物的朋友,GY要确定每个人送出的钱比收到的多多少.在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那些将收到他的礼物的人.然而,在任何一群朋友中,有些人将送出较多的礼物(可能是因为有较多的朋友),有些人有准备了较多的钱.给出一群朋友,没有人的名字会长于 14 字符,给出每个人将花在送礼上的钱,和

【USACO1.1.2】Greedy Gift Givers(map)

Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts of money. Each of these friends might or might not give some money to any or all of the other friends. Likewise, each friend might or might not receiv

1.1.4 PROB Greedy Gift Givers

Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts of money. Each of these friends might or might not give some money to any or all of the other friends. Likewise, each friend might or might not receiv

洛谷P1201 [USACO1.1]贪婪的送礼者Greedy Gift Givers

题目描述 对于一群(NP个)要互送礼物的朋友,GY要确定每个人送出的钱比收到的多多少.在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那些将收到他的礼物的人.然而,在任何一群朋友中,有些人将送出较多的礼物(可能是因为有较多的朋友),有些人有准备了较多的钱.给出一群朋友,没有人的名字会长于 14 字符,给出每个人将花在送礼上的钱,和将收到他的礼物的人的列表,请确定每个人收到的比送出的钱多的数目. SilverN补充:如果钱不能按整数均分,那么余数部分会被收回,不会送出去. 输入

1.1.2 Greedy Gift Givers

A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts of money. Each of these friends might or might not give some money to any or all of the other friends. Likewise, each friend might or might not receive money from any or

P1201 [USACO1.1]贪婪的送礼者Greedy Gift Givers

题目描述 对于一群(NP个)要互送礼物的朋友,GY要确定每个人送出的钱比收到的多多少.在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那些将收到他的礼物的人.然而,在任何一群朋友中,有些人将送出较多的礼物(可能是因为有较多的朋友),有些人有准备了较多的钱.给出一群朋友,没有人的名字会长于 14 字符,给出每个人将花在送礼上的钱,和将收到他的礼物的人的列表,请确定每个人收到的比送出的钱多的数目. 输入输出格式 输入格式: 第 1 行: 人数NP,2<= NP<=10 第 2