All about problems in Volume 5. If there is a thread about your problem, please use it. If not, create one with its number in the subject.
Moderator: Board moderators
limon limon
New poster
Posts: 2 Joined: Tue Jul 09, 2002 10:36 am
Post
by limon limon » Wed Jul 10, 2002 9:52 am
Anyone please say why?
[c]
#include<stdio.h>
#include<iostream.h>
#include<string.h>
void main(){
long power[32],test=1,count=0,val;
char num[35],n;
int len ,f;
power[1]=1;
power[2]=3;
power[3]=7;
power[4]=15;
power[5]=31;
power[6]=63;
power[7]=127;
power[8]=255;
power[9]=511;
power[10]=1023;
power[11]=2047;
power[12]=4095;
power[13]=8191;
power[14]=16383;
power[15]=32767;
power[16]=65535;
power[17]=131071;
power[18]=262143;
power[19]=524287;
power[20]=1048575;
power[21]=2097151;
power[22]=4194303;
power[23]=8388607;
power[24]=16777215;
power[25]=33554431;
power[26]=67108863;
power[27]=134217727;
power[28]=268435455;
power[29]=536870911;
power[30]=1073741823;
power[31]=2147483647;
while(scanf("%[\n]",&num)== 1)
test=1;
while(test)
{
scanf("%[^\n]",&num);
while(scanf("%[\n]",&n)==1 )
test=1;
if(!strcmp(num,"0\0"))
test=0 ;
else
{
val=0;
if(count>0)
printf("\n");
len=strlen(num);
for(f=0;f<len;f++)
{
if(num[f]>48)
val=val+(num[f]-48) * (power[len-f]);
}
printf("%ld",val);
count++ ;
}
}
}
[/c]
rakeb
New poster
Posts: 42 Joined: Fri Aug 30, 2002 2:51 pm
Location: France
Post
by rakeb » Fri Aug 30, 2002 2:55 pm
I think this is much better idea
#include<stdio.h>
#include<string.h>
#include<math.h>
void main(){
double j;
char str[1000];
long l,i,sb=0;
while(gets(str))
{
l=strlen(str);
if(l==1)
if(str[0]-'0' == 0 )
break;
sb=0;
j = double(l);
for(i=0;i<l;i++)
{
sb+=(str-'0')*(pow(2,j)-1);
j--;
}
printf("%ld\n",sb);
}
}
luckyman
New poster
Posts: 3 Joined: Tue Oct 19, 2004 4:11 am
Post
by luckyman » Sat Oct 23, 2004 8:56 pm
here is my code
[java]import java.io.IOException;
class Main
{
static String ReadLn (int maxLg)
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";
try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}
if ((car < 0) && (lg == 0))
return (null);
return (new String (lin, 0, lg));
}
public static void main(String[] args) {
Main myWork = new Main();
myWork.start();
}
private void start()
{
String input;
long a;
while ((input = Main.ReadLn (255)) != null)
{
a = 0;
if (((int)input.charAt(0)-48) == 0)
break;
for (int i=0;i<input.length()-1;i++){
a = a + (((int)input.charAt(i)-48)*(power(2,input.length()-i-1)-1));
}
System.out.println(a);
}
}
long power(int n, int p)
{
long x = n;
for (int i=1;i<p;i++)
x = x*n;
return x;
}
}[/java]
I have tried input many different data
and they all work fine, but OJ gave me a WA
Can someone plz help, thanks
abhi
Learning poster
Posts: 94 Joined: Fri Nov 25, 2005 7:29 pm
Post
by abhi » Fri Dec 02, 2005 3:56 pm
this is my code for problem 575. i have compiled it on the turbo c compiler.
and it gives correct answer.
but OJ gives me
compiler error .
i dont kno wat mistake i have made . plz help.
Code: Select all
#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
char a[100];
long i=0,ans;
while(gets(a))
{
if(a[0]=='0')
{
exit(0);
}
else
{
ans=0;
strrev(a);
for(i=0;a[i]!='\0';i++)
{
ans = ans+(a[i]-48)*(pow(2,i+1)-1);
}
printf("%ld\n",ans);
}
return 0;
}
ayon
Experienced poster
Posts: 161 Joined: Tue Oct 25, 2005 8:38 pm
Location: buet, dhaka, bangladesh
Post
by ayon » Fri Dec 02, 2005 8:19 pm
strrev() not allowed in unix
ishtiak zaman
----------------
the world is nothing but a good program, and we are all some instances of the program
abhi
Learning poster
Posts: 94 Joined: Fri Nov 25, 2005 7:29 pm
Post
by abhi » Sat Dec 03, 2005 6:17 am
i have removed the strrev function.
i replaced
by
Code: Select all
len=strlen(a);
for(i=0;i<len;i++)
{
a[len-i]=a[i];
}
a[i]='\0';
also i have removed
and replaced it by
since i had not included <process.h> in the code
but still CE.............
ayon
Experienced poster
Posts: 161 Joined: Tue Oct 25, 2005 8:38 pm
Location: buet, dhaka, bangladesh
Post
by ayon » Sat Dec 03, 2005 7:49 am
it's very much clear that. your program is missing a '}'. but for that reason, any compiler of this world would give the error message "compound statement missing }", i think. And believe me, your strrev() function wont reverse the string, swapping necessary, check it...
ishtiak zaman
----------------
the world is nothing but a good program, and we are all some instances of the program
Timo
Learning poster
Posts: 70 Joined: Tue Oct 11, 2005 2:44 am
Location: Indonesia
Post
by Timo » Sat Dec 03, 2005 7:59 am
abhi wrote: i have removed the strrev function.
i replaced
by
Code: Select all
len=strlen(a);
for(i=0;i<len;i++)
{
a[len-i]=a[i];
}
a[i]='\0';
also i have removed
and replaced it by
since i had not included <process.h> in the code
but still CE.............
your strrev is wrong
it shoule be
Code: Select all
len=strlen(a);
for(i=0,len--;i<=len;i++,len--)
{
c=a[i];
a[i]=a[len];
a[len]=c;
}
"Life is more beautiful with algorithm"
abhi
Learning poster
Posts: 94 Joined: Fri Nov 25, 2005 7:29 pm
Post
by abhi » Sat Dec 03, 2005 9:20 am
oh i am really sorry..........
that was really a very foolish mistake on my part.
hridoy
New poster
Posts: 21 Joined: Tue May 08, 2007 10:30 am
Location: Dhaka
Contact:
Post
by hridoy » Fri May 25, 2007 9:04 pm
why the following program is CE,
#include<stdio.h>
main()
{
long long int n,z;
while(1)
{
scanf("%lld",&n);
if(n==0)
break;
long long int x,y,i,q=1;
z=0;
for(i=1;;i++)
{
x=n%10;
q*=2;
y=x*(q-1);
z+=y;
n/=10;
if(n==0)
break;
}
printf("%lld\n",z);
}
}
mmonish
Experienced poster
Posts: 109 Joined: Sun Mar 11, 2007 2:55 pm
Location: SUST
Post
by mmonish » Sat May 26, 2007 3:36 am
U must use a return type for main function. U can use like this
void main()
{
//code
}
or
int main()
{
//code
return 0;
}
Hope it helps.
hridoy
New poster
Posts: 21 Joined: Tue May 08, 2007 10:30 am
Location: Dhaka
Contact:
Post
by hridoy » Sat May 26, 2007 11:15 am
still I m getting CE
mmonish
Experienced poster
Posts: 109 Joined: Sun Mar 11, 2007 2:55 pm
Location: SUST
Post
by mmonish » Sat May 26, 2007 2:42 pm
I submit ur code by adding the following extra line
it gives me WA. So u should think about ur solution.
Hope it helps.
kbr_iut
Experienced poster
Posts: 103 Joined: Tue Mar 25, 2008 11:00 pm
Location: IUT-OIC, DHAKA, BANGLADESH
Contact:
Post
by kbr_iut » Wed Apr 02, 2008 8:24 pm
now AC code deleted.
It is tough to become a good programmer.
It is more tough to become a good person.
I am trying both...............................
dewsworld
New poster
Posts: 12 Joined: Fri Aug 13, 2010 11:52 am
Post
by dewsworld » Sat Aug 28, 2010 8:30 am
Can anyone please tell me why I get WA in this code... Thanks in advance
Code: Select all
#include <stdio.h>
#include <string.h>
#define uint32 unsigned int
int main()
{
// freoen("in.txt", "rb", stdin );
char input[100] ;
uint32 len ;
int i ;
uint32 dec ;
uint32 factor ;
while( gets(input) != NULL )
{
if( (len = strlen( input )) == 1 )
if( input[0] == '0' )
break ;
dec = 0 ;
factor = 1 ;
for( i = len-1 ; i >= 0 ; i-- )
{
dec = dec + (input[i]-'0')*(factor-1) ;
factor = factor * 2 ;
}
printf("%u\n", dec ) ;
}
return 0 ;
}