473,466 Members | 1,286 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

spoj question BISHOPS. getting wrong answer. heres my code

7 New Member
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<string.h>
  3. int main()
  4. {
  5.     char a[109];
  6.     int i,j,b[109],x,temp,t=1024;
  7.     while(t>0)
  8.     {
  9.         scanf("%s",a);
  10.         if(strlen(a)==1 && a[0]=='1')
  11.         {
  12.             printf("1");
  13.         }
  14.         else
  15.         {
  16.             if(a[strlen(a)-1]!='0')
  17.             {
  18.                 a[strlen(a)-1]=a[strlen(a)-1]-1;
  19.             }
  20.             else
  21.             {
  22.                 for(j=strlen(a)-1;a[j]=='0'&&j>0;j--)
  23.                 {
  24.                     a[j]='9';
  25.                     if(a[j-1]!='0')
  26.                     {
  27.                         a[j-1]=a[j-1]-1;
  28.                     }
  29.                 }
  30.             }
  31.             j=0;
  32.             for(i=strlen(a)-1;i>=0;i--)
  33.             {
  34.                 b[j]=a[i]-'0';
  35.                 j++;
  36.             }
  37.             temp=0;
  38.             for(i=0;i<j;i++)
  39.             {
  40.                 x=b[i]*2+temp;
  41.                 b[i]=x%10;
  42.                 temp=x/10;
  43.             }
  44.             while(temp>0)
  45.             {
  46.                 b[i]=temp%10;
  47.                 temp=temp/10;
  48.                 i++;
  49.             }
  50.             for(j=i-1;j>=0;j--)
  51.                 printf("%d",b[j]);
  52.         }
  53.         printf("\n");
  54.         t--;
  55.     }
  56.  
  57.     return(0);
  58.  
  59. }
  60.  
Jul 9 '13 #1
8 2455
Rabbit
12,516 Recognized Expert Moderator MVP
You need to tell us what input you're using, what answer it's giving you, and what the answer is supposed to be.
Jul 9 '13 #2
depster
7 New Member
http://www.spoj.com/problems/BISHOPS/
here is the link to the problem. I'm getting all the answers correct on my compiler and even on ideone compiler but spoj compiler shows wrong answer
Jul 9 '13 #3
Rabbit
12,516 Recognized Expert Moderator MVP
I just put your code in ideone and the output is wrong. It outputs 2, 4, 2, and then a bunch of 1s. That does not match the expected output of 2, 4.
Jul 9 '13 #4
depster
7 New Member
i have modified my code but still it is showing wrong answer on spoj compiler and correct answer on ideone
earlier i was not able to detect eof but now i have corrected it. My algorithm just computes 2*(n-1) for large numbers and computes 1 for n=1. Is my algorithm correct?
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<string.h>
  3. int main()
  4. {
  5.     char a[109];
  6.     int i,j,b[109],x,temp;
  7.     while(fscanf(stdin,"%s",a)==1)
  8.     {
  9.  
  10.         if(strlen(a)==1 && a[0]=='1')
  11.         {
  12.             printf("1");
  13.         }
  14.         else
  15.         {
  16.             if(a[strlen(a)-1]!='0')
  17.             {
  18.                 a[strlen(a)-1]=a[strlen(a)-1]-1;
  19.             }
  20.             else
  21.             {
  22.                 for(j=strlen(a)-1;a[j]=='0'&&j>0;j--)
  23.                 {
  24.                     a[j]='9';
  25.                     if(a[j-1]!='0')
  26.                     {
  27.                         a[j-1]=a[j-1]-1;
  28.                     }
  29.                 }
  30.             }
  31.             j=0;
  32.             for(i=strlen(a)-1;i>=0;i--)
  33.             {
  34.                 b[j]=a[i]-'0';
  35.                 j++;
  36.             }
  37.             temp=0;
  38.             for(i=0;i<j;i++)
  39.             {
  40.                 x=b[i]*2+temp;
  41.                 b[i]=x%10;
  42.                 temp=x/10;
  43.             }
  44.             while(temp>0)
  45.             {
  46.                 b[i]=temp%10;
  47.                 temp=temp/10;
  48.                 i++;
  49.             }
  50.             for(j=i-1;j>=0;j--)
  51.                 printf("%d",b[j]);
  52.         }
  53.         printf("\n");
  54.     }
  55.     return(0);
  56.  
  57. }
  58.  
Jul 10 '13 #5
Rabbit
12,516 Recognized Expert Moderator MVP
What answer is it showing on spoj?
Jul 10 '13 #6
depster
7 New Member
it just shows wrong answer
Jul 10 '13 #7
Rabbit
12,516 Recognized Expert Moderator MVP
It's hard to say then. If you don't know what the output is supposed to be, then that makes it difficult to know if your algorithm is correct. The only thing you can really do is study the problem to make sure your algorithm is correct.
Jul 10 '13 #8
donbock
2,426 Recognized Expert Top Contributor
Array a holds the digit characters of the grid size. Array b starts out holding something related to the digit numbers of the grid size and ends up holding the digit numbers of the result.

You mention 2*(n-1). The times-2 takes place on numbers at lines 37-49. The subtract-1 takes place on characters at lines 16-30. It would be a lot easier to also do the subtract-1 on numbers instead of characters. Borrow and carry are especially easier. Hint: 2*(n-1) = (2*n)-2.

Things to keep in mind:
  • What if the result has more digits than the input? Where will you put the extra digit(s)?
  • The number array won't be null-terminated.
  • Your program will crash badly if the input string contains anything other than digits. I suppose you can trust spoj not to do that to you but ...
  • Your program will crash badly if the input string is longer than array a. I suppose you can trust spoj not to do that to you but ...
  • You have no comments! At the very least you should state your analytical solution to the Bishops problem.
Jul 12 '13 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Niklaus | last post by:
Hi, Can someone point out what is wrong with this code ? How can i make it better optimize it. When run it gives me seg fault in linux. But windows it works fine(runs for a long time). Do we...
2
by: Rui Macdonald | last post by:
What is wrong with this code to populate a DropDownList? Can somebody Help me? Tnx RMac ===================================================================================== WebForm.aspx.vb
0
by: Lisa Jones | last post by:
Hi Has anyone see this problem in ADO.Net when deleting a record (assuming that the table has identity increment Index) result in getting wrong index key if right after adding the record the table...
12
by: questions? | last post by:
I am testing a problem with linked list. I just do a lot of times: create a list, then free it. ############################################# # include <stdio.h> # include <stdlib.h> struct...
9
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
6
by: amrhi | last post by:
Whats wrong with my code , i cant access to next page. Looks like the text field cant be read by login.php Thanks <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"...
0
by: =?Utf-8?B?UHVjY2E=?= | last post by:
-- Hi, I'm using vs2005,. .net 2.0 for windows applicaiton. I'm doing drag and drop of 1 row of data at a time between 2 datagridview controls. I'm using the myDataGridView.DragDrop event to get...
30
by: Bill Reid | last post by:
#define MAX_VALUES 64 typedef struct { unsigned value_1; double value_2; double value_3; double value_4; } VALUES; typedef struct {
2
by: ccgrl451 | last post by:
Okay, so you probably haven't seen my previous question, whats wrong with this code. For those who have, you know what I'm talking about. If not, heres what I have to do. We have to do this...
0
by: sumansms | last post by:
Hi All, I am getting wrong date when I am using DateTime.Parse function. The following is the code.. DateValue = DateTime.Parse(datarow.ToString()).ToString("MM/dd/yyyy"); I am taking the data...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.