usaco1.1.2
Greedy Gift Givers 贪婪的送礼者
题目翻译:http://www.nocow.cn/index.php/Translate:USACO/gift1
考察字符串处理。这道题可以边读入边处理。
注意审题,题目中没有说送礼者和受礼者的姓名和前面的名单中的名字顺序一样。关键在于字符串的匹配。
字符串的匹配可以用一个strcmp函数的。。我忘了这个函数就自己写了一个
。。
考察字符串处理。这道题可以边读入边处理。
注意审题,题目中没有说送礼者和受礼者的姓名和前面的名单中的名字顺序一样。关键在于字符串的匹配。
字符串的匹配可以用一个strcmp函数的。。我忘了这个函数就自己写了一个

/*
ID: wsc5001
LANG: C
TASK: gift1
*/
#include<stdio.h>
#include<stdlib.h>
char pname[10][15]={'\0'};
int n;
int zhaoren(char shuru[])
{
int i,j,k,t,flag;
for(i=0;i<n;i++)
{
flag=1;
t=0;
while ((!(shuru[t]=='\0'&&pname[i][t]=='\0'))&&t<15)
{
if (shuru[t]!=pname[i][t])
flag=0;
t++;
}
if (flag==1)
return i;
}
return -1;
}
int main()
{
FILE *fin,*fout;
fin=fopen("gift1.in","r");
fout=fopen("gift1.out","w");
char tempchar,thisp1[15],thisp2[15];
int pmoney[10]={0},pyuan[10]={0};
int i,j,t,m,p,q,w;
fscanf(fin,"%d%c",&n,&tempchar);
//读入名单
for (i=0;i<n;i++)
{
j=0;
while (1)
{
fscanf(fin,"%c",&tempchar);
if (tempchar!='\n')
{
pname[i][j]=tempchar;
j++;
}
else
break;
}
}
///
for (i=0;i<n;i++)
{
fscanf(fin,"%s%d%d",thisp1,&p,&q);
t=zhaoren(thisp1);
{
if(q!=0)
{
pyuan[t]=(p/q)*q;
for (j=0;j<q;j++)
{
fscanf(fin,"%s",thisp2);
w=zhaoren(thisp2);
pmoney[w]+=p/q;
}
}
}
}
for (i=0;i<n;i++)
{
fprintf(fout,"%s %d\n",pname[i],pmoney[i]-pyuan[i]);
}
fclose(fin);
fclose(fout);
//system("pause");
}