484 - The Department of Redundancy Department

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

Moderator: Board moderators

rakeb
New poster
Posts: 42
Joined: Fri Aug 30, 2002 2:51 pm
Location: France

Post by rakeb »

Because scanf stops input after getting space, here you can take input by scanf and sore it in an array. u should take input as a string and then by searching the array give the output
here is my code:
[cpp]
#include<stdio.h>
#include<string.h>

struct d
{
char a[100];
long num;

}data[100];
long nc;

long check(char n[])
{
long i;
for(i=0;i<nc;i++)
if(!strcmp(data.a,n))
return i;

strcpy(data.a,n);
nc++;
return i;

}




void main()
{
long b,i;
char n[100];
nc=0;
//freopen("484.in","r",stdin);

while(scanf("%s",n)==1)
{
b=check(n);
data.num++;

}
for(i=0;i<nc;i++)
{
printf("%s %ld\n",data.a,data.num);

}

}
[/cpp]

lendlice
New poster
Posts: 22
Joined: Thu Nov 21, 2002 10:50 am

484 - The Department of Redundancy Department

Post by lendlice »

I have wrong answer.Who can help me?

Code: Select all

#include<stdio.h>
#include<iostream.h>
main()
{
 int i=0,j=0,n=0,i1=0,j1=0,count[100],i3=0,k=0,i4=0,k1=0;
 char in[1000],save[100][1000];
 for(i=0;i<=100;i++)
   count[i]=0;
 gets(in);
 n=strlen(in);
 for(i=0;i<=n;i++)
 {
  if(in[i]!=' '&&in[i]!='\0'&&in[i]>='0'&&in[i]<='9'||in[i]=='-')
  {
   if(in[i]=='-')
     k1++;
   if(k1==1&&in[i+1]>='0'&&in[i+1]<='9')
   {
    save[i1][j1]=in[i];
    j1++;
   }
  }
  else
  {
   for(i3=0;i3<i1;i3++)
   {
    if(strcmp(save[i3],save[i1])==0)
    {
     count[i3]++;
     k++;
     for(i4=0;i4<=j1;i4++)
       save[i1][i4]=0;
        j1=0;
     }
   }
   if(k==0)
   {
     count[i1]++;
     i1++;
     j1=0;
   }
   k=0;
   k1=0;
  }
 }
 for(j=0;j<i1;j++)
 if(save[j][0]!=' '&&save[j][0]!='\0')
   cout << save[j] <<" "<<count[j]<<endl ;
}

kelfin
New poster
Posts: 4
Joined: Wed Nov 13, 2002 1:50 pm
Location: Taiwan

Re: ...

Post by kelfin »

Fresh wrote:Change 'char' to 'int' and split the input as below,

[cpp]
while(gets(y))
{
int inp;
char *p = strtok(y);
while (p != NULL)
{ inp = atoi(p);
p = strtok(NULL," \n");
.....
.....
}
.....
.....
}
[/cpp]

-novice :wink:
I change , but "Wrong Answer".
The code in up.

Fresh
New poster
Posts: 46
Joined: Mon Apr 15, 2002 10:42 am
Contact:

...

Post by Fresh »


supermin
New poster
Posts: 37
Joined: Sat Oct 12, 2002 9:54 am
Location: (Taiwan)
Contact:

Post by supermin »

to rakeb:
I test and submit your source code, but judge send me "Runtime Error (SIGSEGV)". Therefore, I think you can check your code again. Maybe you could tell me whether it can run correctly. :)


I got AC,and I offer some information about this problem:

1.the integer number(positive/negative/zero) is ranged from 65535
to -65536,so you can use scanf("%d",&XXX);
2.The amout of all integers is below 10000.

Good luck!

Eric
Learning poster
Posts: 83
Joined: Wed Sep 11, 2002 6:28 pm
Location: Hong Kong

Post by Eric »

Hello, I try to solve this problem but get WA.
If the input is ranged from 65535 to -65536 and is below 10000, it should work.
Can anyone have any opinion?
Last edited by Eric on Sat Jan 31, 2004 6:35 am, edited 2 times in total.

Jalal
Learning poster
Posts: 65
Joined: Sun Jun 02, 2002 8:41 pm
Location: BANGLADESH
Contact:

Post by Jalal »

kelfin!
There is no need to calculate
if any calculation exeeds the limit of 2147483647.
u may take as INF over the calcution.....

Observer
Guru
Posts: 570
Joined: Sat May 10, 2003 4:20 am
Location: Hong Kong

Post by Observer »

I wonder if there are trailing spaces in the input......
What can I do if so?? (I'm using Pascal......)


I couldn't find any mistakes in my code. My algorithm is:
1. Input number
2. If first occurence then
...2.1 increment n
...2.2 and store the number in an array
...2.3 set occurence = 1
3. else
...3.1 increment occurence
4. Back to step 1
5. Output


What should I do??? Please help!!!!!!
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org

little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post by little joey »

I think there are, maybe among other irregularities. I had to write my own input routine before I could get AC in Pascal...

Observer
Guru
Posts: 570
Joined: Sat May 10, 2003 4:20 am
Location: Hong Kong

Post by Observer »

little joey wrote: I had to write my own input routine before I could get AC in Pascal...
Special input routine?

Can you give me some hint??? I really want to know...... Plz......
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org

little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post by little joey »

Read the input line by line into a string, and extract the numbers from this string by processing the line.

It's a bit of a spoiler, but consider this little program[pascal]{$MODE DELPHI}

program test;

function line2nums(var line:ansistring;var num:array of integer):integer;
var
i,j,len:integer;
begin
result:=0;
len:=length(line);
i:=1;
while (i<=len) do begin
while ((i<=len) and not (line in ['-','0'..'9'])) do inc(i);
j:=i;
while ((i<=len) and (line in ['-','0'..'9'])) do inc(i);
if (i>j) then begin
val(copy(line,j,i-j),num[result]);
inc(result);
end;
end;
end;


var
num:array[1..1000] of integer;
line:ansistring;
nums,i:integer;
begin
line:=' -9 3 22222 9847 rubbish -123456 more rubbish 777 ';
nums:=line2nums(line,num);
for i:=1 to nums do writeln(num);
end.[/pascal]
It compiles using FPC, the same compiler the judge uses, but certainly not with BP (I don't know about Delphi). No comments, you should be able to figure out what it does. Use ansistring, string is to short for this particular input. The val() procedure makes it a little slow, so write your own if you want more speed. The function can't cope with monsters like ('123-456'), but I don't think they are there.

good luck.

Observer
Guru
Posts: 570
Joined: Sat May 10, 2003 4:20 am
Location: Hong Kong

Post by Observer »

Thanks, little joey!!!! Finally I got an AC!!!!!!!!


Remainder for Pascal users:

There ARE trailing spaces, and the input comes in more than ONE line......

No. of numbers in the input < 8000 :D
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org

User avatar
Riyad
Experienced poster
Posts: 131
Joined: Thu Aug 14, 2003 10:23 pm
Location: BUET
Contact:

484,cant understand the input format

Post by Riyad »

i am having trouble with the prob 484 . i cant take the input of the prob. as it is a multiple input prob so the output for differnet input block should be different (with a new line).but my programs gives the output together .
tha means the complete out put together. here is my code , and sorry for my not so good english;
pliz tell me if there are any efficient algorithm. is my process right??
thanx in advance
Riyad :cry: :cry: :cry:


/* start of code*/
/*----------------*/
#include<stdio.h>

#define size 10000


struct num{
long int n;
int fr;


};

struct num obj[size];

int index;
long int input[size];
int iindex;

void init(){

register int i;
index=iindex=0;
for(i=0;i<size;i++){
obj.fr=0;

}


}


int search(int x)
{
register int i;
for(i=0;i<index;i++){
if(obj.n==x){
return i;

}

}
return -1;


}


void calculate(){

register int i,check ;
for(i=0;i<iindex;i++){

if(i==0){

obj[index].n=input;
obj[index].fr=1;
index++;

}

else{

check=search(input);
if(check!=-1){

obj[check].fr++;
}
else if(check==-1){

obj[index].n=input;
obj[index].fr=1;
index++;
}


}


}




}


void print(){

register int i;
for(i=0;i<(index-1);i++){

printf("%ld %d\n",obj.n,obj.fr);
}



}




int main(){

//freopen("input.in","rt",stdin);
register int cinput;
scanf("%d",&cinput);
while(cinput>0){

init();

while(scanf("%ld",&input[iindex++])!=EOF){

;
}


calculate();
print();
printf("\n");
cinput--;

}

return 0;
}
/*----------------*/
/*end of code */
HOLD ME NOW ,, I AM 6 FEET FROM THE EDGE AND I AM THINKIN.. MAY BE SIX FEET IS SO FAR DOWN

UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 »

This is NOT a multiple input problem (it has a red check mark, not a blue one). The input file just contains many many many integers.

User avatar
Riyad
Experienced poster
Posts: 131
Joined: Thu Aug 14, 2003 10:23 pm
Location: BUET
Contact:

thanx a lot

Post by Riyad »

thanx buddy i got it ac now . i some how made a stupid mistake :roll: by considering 484 as a multiple input problem . problems like this can be made by we new people. thanx once agian for u r help. :P
bye
Riyad :lol:
HOLD ME NOW ,, I AM 6 FEET FROM THE EDGE AND I AM THINKIN.. MAY BE SIX FEET IS SO FAR DOWN

Post Reply

Return to “Volume 4 (400-499)”