|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Problem 77: Greedy Gift Givers
Why doesn't it do the problem correctly now?
/* ID: albert.4 LANG: C TASK: gift1 */ #include <stdio.h> #define MAXNAMELEN 14 main() { struct friend { int money, ngifts; /* no. gifts */ char name[MAXNAMELEN]; }; int NP; /* no. people */ FILE* in = fopen("gift1.in" , "r"); FILE* out = fopen("gift1.out", "w"); fscanf(in, "%d", &NP); struct friend f[NP]; int i = 0; for (; i < NP; i++) { fscanf(in, "%s", f[i].name); } /* Until input tells us how much each friend has (later in this prog), they're all broke */ i = 0; for (; i < NP; i++) { f[i].money = 0; } char temp[MAXNAMELEN]; for (i = 0; i < NP; i++) { fscanf(in, "%s", temp); if (strcmp(f[i].name, temp) == 0) { int init; /* initial amount of money */ fscanf(in, "%d %d", &init, &f[i].ngifts); f[i].money = init * -1; /* init is given to people */ if (f[i].ngifts != 0) { f[i].money += init % f[i].ngifts; } int amteachpersongets = 0; if (f[i].ngifts != 0) { int amteachpersongets = init / f[i].ngifts; } int j = 0; char temp2[MAXNAMELEN]; for (; j < f[i].ngifts; j++) { fscanf(in, "%s", temp2); int k = 0; for (; k < NP; k++) { if (strcmp(f[k].name, temp2) == 0) { f[k].money += amteachpersongets; break; } } } } } for (i = 0; i < NP; i++) { fprintf(out, "%s %d\n", f[i].name, f[i].money); } exit(0); } |
|
#2
|
|||
|
|||
|
Problem 77: Greedy Gift Givers
Bert wrote:
) Why doesn't it do the problem correctly now? What is the incorrect output then, and what should the correct output be ? SaSW, Willem -- Disclaimer: I am in no way responsible for any of the statements made in the above text. For all I know I might be drugged or something No I'm not paranoid. You all think I'm paranoid, don't you ! #ET |
![]() |
| Viewing: Web Development Archives > FAQs > C/C++ > Problem 77: Greedy Gift Givers |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|