## 10929 - You can say 11

Moderator: Board moderators

rimu
New poster
Posts: 9
Joined: Sat Feb 26, 2005 4:41 pm

### 10929 - You can say 11

i'm very confused getting WA

can anybody post some critical i/o

my logic is as follows in brief:

gets( r );

for ( i = s = 0; r; ++i )
{
s = s * 10 + r - 48;
s = s % 11;
}

if ( !s ) then multiple

i'll be back

arif_pasha
New poster
Posts: 42
Joined: Fri Jun 13, 2003 3:47 pm
Contact:

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Contact:
I think there are no inputs like above.

I used gets() to take inputs. But There can be leading zeroes in the input.

Input:

Code: Select all

``````011
11
122321
0123123
0``````

Code: Select all

``````....
if(r[0]=='0')
break;
....``````
The code above is wrong. You should use this...

Code: Select all

``````....
if(r[0]=='0'&&r[1]=='\0')
break;
....``````
Hope it works.
Ami ekhono shopno dekhi...
HomePage

TISARKER
Learning poster
Posts: 88
Joined: Tue Oct 12, 2004 6:45 pm
Contact:
According to jans Idea
Try this input

Code: Select all

``00000000``
what is the correct output for above input.?
Mr. Arithmetic logic Unit

iishaque
New poster
Posts: 5
Joined: Sun Apr 03, 2005 5:11 pm

### Why WA

Plz give some critical input and output. Can any one tell Is rimu's algorithm right or wrong??
Plz help!!!!!

SRX
Learning poster
Posts: 63
Joined: Sat May 14, 2005 8:13 am
Location: Taiwan

### Re: Why WA

iishaque wrote:Plz give some critical input and output. Can any one tell Is rimu's algorithm right or wrong??
Plz help!!!!!

Code: Select all

``````     just count num's odd digits sum - num's even digits sum
then check it%11
then use Jan's way ( thanks  :D ) if(r[0]=='0'&&r[1]=='\0') break;
``````

iishaque
New poster
Posts: 5
Joined: Sun Apr 03, 2005 5:11 pm

### thanks

Thank you SRX for you help. I got AC.

Martin Macko
A great helper
Posts: 481
Joined: Sun Jun 19, 2005 1:18 am
Location: European Union (Slovak Republic)
TISARKER wrote:According to jans Idea
Try this input

Code: Select all

``00000000``
what is the correct output for above input.?
The 0000000 is not positive, so it may be only at the end of the input. But it's not because my AC recognized the end of the input by a single 0 in line.

Schutzstaffel
New poster
Posts: 37
Joined: Fri Apr 30, 2004 6:52 pm
Location: Portugal
I'm getting WA and have no clue what it might be

This is my function:

Code: Select all

``````code ACC and thus removed :P
``````
I sum the odd digits and subtract the even digits then check if the sum is divisible by 11. Did I miss something?
Thanks.
Last edited by Schutzstaffel on Fri Jan 06, 2006 6:00 am, edited 1 time in total.

Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:
I don't think that using atoi on a not null-terminated string is safe.

Schutzstaffel
New poster
Posts: 37
Joined: Fri Apr 30, 2004 6:52 pm
Location: Portugal
You're right, it was because of that.
Thanks

athlon19831
New poster
Posts: 20
Joined: Thu Jan 19, 2006 2:32 pm

### 10929 - You can say 11

I got Time Limit Exceeded,but i don't know why
my code:
#include "iostream.h"
#include "stdio.h"
#include "string.h"
#include "math.h"
int main(int argc, char* argv[])
{
char str[1010];
int s_odd,s_even;
int s;
int i,j;
while(cin>>str)
{
if(strcmp(str,"0")==0)
break;
else if(str[0]=='-')
break;
else
{
s_odd=0;
s_even=0;
for(i=0;i<strlen(str);i++)
{
if(i%2)
{
s_odd+=(int(str)-48);
}
else
{
s_even+=(int(str)-48);
}
}
s=int(fabs(s_odd-s_even));
if(s==0)
{
for(i=0;i<strlen(str);i++)
{
cout<<str;
}
cout<<" is a multiple of 11."<<endl;
}
else if(s%11==0)
{
for(i=0;i<strlen(str);i++)
{
cout<<str;
}
cout<<" is a multiple of 11."<<endl;
}
else
{
for(i=0;i<strlen(str);i++)
{
cout<<str;
}
cout<<" is not a multiple of 11."<<endl;
}
}
}
return 0;
}

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore
You are calling strlen each time in the for loop you print the input number. And in fact, you can print out a char array directly just like C++ strings.

Just do a cout << str for the places where you print the input number character by character and the TLE should go away.

If not, you might wan to consider scanf/printf rather cin/cout.

Hope this helps.

athlon19831
New poster
Posts: 20
Joined: Thu Jan 19, 2006 2:32 pm
chunyi81 wrote:You are calling strlen each time in the for loop you print the input number. And in fact, you can print out a char array directly just like C++ strings.

Just do a cout << str for the places where you print the input number character by character and the TLE should go away.

If not, you might wan to consider scanf/printf rather cin/cout.

Hope this helps.
Thanks you for your help. I got AC

Zaspire
New poster
Posts: 36
Joined: Sun Apr 23, 2006 2:42 pm
Location: Russia
Do not forget that you should output number with leading zeroes (if it has)