这是一道纯粹的模拟题,但是听说用Hash会很快,不过我现在还不会Hash,好吧,要学的东西真的很多~
这一道题不难,不过要理解理解题意的话还是有一点难!所以,我把大概的题意说一下:
就是说有NP的小朋友,每个小朋友要把钱当做礼物给其他的小朋友,问每个人得到的钱比送出去的钱多多少!
输入格式也比较繁琐:第一行是NP,即人数,然后接下来就是这NP个人的名字。然后是每一次输一个人名,代表那个要送钱的人;接下来一行是他的钱数以及送的人数,然后就是送的人的名字的列表!大概就是这些!
下面是代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 10
#define LEN 15
typedef struct
{
char name[LEN];
int in;
int out;
}Friend;
int main()
{
FILE * fin = fopen( "gift1.in", "r" );
FILE * fout = fopen( "gift1.out", "w" );
int numofriend;
int i, j, k;
Friend f[ N ];
Friend Tmp;
int TmpGivePeople;
fscanf( fin, "%d", &numofriend );
//fgetchar();
for( i = 0; i < numofriend; i++ )
{
fscanf( fin, "%s", f[i].name );
f[i].in = 0;
}
//fgetchar();
for( i = 0; i < numofriend; i++ )
{
fscanf( fin, "%s", Tmp.name );
//printf( "TmpFirst is %s", Tmp.name );
for( j = 0; j < numofriend; j++ )
{
if( strcmp( Tmp.name, f[j].name ) == 0 )
{
fscanf( fin, "%d%d", &Tmp.out, &TmpGivePeople );
//fgetchar();
//printf( " j = %d && name is %s\n" , j, f[j].name );
if( TmpGivePeople == 0 )
{
f[j].out = 0;
}
else
{
if( Tmp.out % TmpGivePeople == 0 )
{
f[j].out = Tmp.out;
}
else
{
f[j].out = Tmp.out / TmpGivePeople * TmpGivePeople;
}
}
}
}
for( j = 0; j < TmpGivePeople; j++ )
{
fscanf( fin, "%s", Tmp.name );
for( k = 0; k < numofriend; k++ )
{
if( strcmp( Tmp.name, f[k].name ) == 0 )
{
f[k].in += ( Tmp.out / TmpGivePeople );
}
}
}
}
for( i = 0; i < numofriend; i++ )
{
fprintf( fout, "%s %d\n", f[i].name, f[i].in - f[i].out );
}
exit(0);
}