10390 - Bean Counting

All about problems in Volume 103. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Post Reply
HKcow
New poster
Posts: 10
Joined: Fri Aug 30, 2002 2:34 pm

10390 - Bean Counting

Post by HKcow »

Please suggest why my program run for more than 10 sec.
In my test case I have no problem ^_^
[cpp]
#include <stdio.h>
#include <string.h>

void main() {
int bean[26], guess[26];
int i, n, m, g[5], tp, tg;
char line[1001], c1, c[5];
double potcount;
scanf("%d", &n);
gets(line);
gets(line);
for (; n>0; n--) {
memset(bean, 0, 26*sizeof(int));
gets(line);
for (i=0; i<strlen(line); i++)
bean[line-'a']++;
memset(guess, 0, 26*sizeof(int));
tp=0;
tg=0;
while ((gets(line)!=NULL) && (line[0]!=0)) {
m=sscanf(line, "%c %c:%d,%c:%d,%c:%d,%c:%d,%c:%d", &c1, &c[0], &g[0], &c[1], &g[1], &c[2], &g[2], &c[3], &g[3], &c[4], &g[4]);
m=(m-1)/2;
for (i=0; i<m; i++) {
tg++;
if (g==bean[c-'a']) {
guess[c1-'A']+=2;
tp+=2;
}
if ((g==bean[c-'a']-1) || (g==bean[c-'a']+1)) {
guess[c1-'A']+=1;
tp++;
}
}
}
potcount=tg*2.0/tp;
for (i=0; i<26; i++)
if (guess!=0)
printf("%c %.2lf\n", (i+'A'), (potcount*guess));
if (n>1)
printf("\n");
}
}
[/cpp]
WR
Experienced poster
Posts: 145
Joined: Thu Nov 27, 2003 9:46 am

10390 - bean counting

Post by WR »

Could somebody please verify the following I/O?

Code: Select all

4 

zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 
A a:0 
Z a:0 

zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 
A z:100 
Z z:100 

                <- empty line (no beans at all) 
A a:65 
A s:0 
Z a:1 

a 
A a:0 
B a:1 
C a:2 
output:

Code: Select all

A 2.00 
Z 2.00 

A 2.00 
Z 2.00 

A 4.00 
Z 2.00 

A 1.50 
B 3.00 
C 1.50 
The program works fine with the original data!

I already verified with assert that the first line is shorter than 8192 characters and that there's no scattered whitespace.

In first example possibly no one wins. I tried that alternative, printing just an empty line, WA, too!

Data types: int for guesses and shares, double for the winnings
daveon
Experienced poster
Posts: 229
Joined: Tue Aug 31, 2004 2:41 am
Location: TORONTO, CANADA

Post by daveon »

Hi,

Your output is correct. Int and doubles are enough. It could be your parsing method or some really minor mistake.
daveon
Experienced poster
Posts: 229
Joined: Tue Aug 31, 2004 2:41 am
Location: TORONTO, CANADA

Post by daveon »

Hi,

TRY CHANGING:

Code: Select all


scanf("%d", &n);
gets(line);
gets(line); 

.
.
.

while ((gets(line)!=NULL) && (line[0]!=0)) 
{ 
}

TO:

Code: Select all

gets(line);
sscanf(line,"%d", &n);
gets(line);

.
.
.

while(gets(line))
{
if(strlen(line)==0) break;
}

Post Reply

Return to “Volume 103 (10300-10399)”