- #include<stdio.h>
-
#include<string.h>
-
int main()
-
{
-
char a[109];
-
int i,j,b[109],x,temp,t=1024;
-
while(t>0)
-
{
-
scanf("%s",a);
-
if(strlen(a)==1 && a[0]=='1')
-
{
-
printf("1");
-
}
-
else
-
{
-
if(a[strlen(a)-1]!='0')
-
{
-
a[strlen(a)-1]=a[strlen(a)-1]-1;
-
}
-
else
-
{
-
for(j=strlen(a)-1;a[j]=='0'&&j>0;j--)
-
{
-
a[j]='9';
-
if(a[j-1]!='0')
-
{
-
a[j-1]=a[j-1]-1;
-
}
-
}
-
}
-
j=0;
-
for(i=strlen(a)-1;i>=0;i--)
-
{
-
b[j]=a[i]-'0';
-
j++;
-
}
-
temp=0;
-
for(i=0;i<j;i++)
-
{
-
x=b[i]*2+temp;
-
b[i]=x%10;
-
temp=x/10;
-
}
-
while(temp>0)
-
{
-
b[i]=temp%10;
-
temp=temp/10;
-
i++;
-
}
-
for(j=i-1;j>=0;j--)
-
printf("%d",b[j]);
-
}
-
printf("\n");
-
t--;
-
}
-
-
return(0);
-
-
}
-
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.
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
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.
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? - #include<stdio.h>
-
#include<string.h>
-
int main()
-
{
-
char a[109];
-
int i,j,b[109],x,temp;
-
while(fscanf(stdin,"%s",a)==1)
-
{
-
-
if(strlen(a)==1 && a[0]=='1')
-
{
-
printf("1");
-
}
-
else
-
{
-
if(a[strlen(a)-1]!='0')
-
{
-
a[strlen(a)-1]=a[strlen(a)-1]-1;
-
}
-
else
-
{
-
for(j=strlen(a)-1;a[j]=='0'&&j>0;j--)
-
{
-
a[j]='9';
-
if(a[j-1]!='0')
-
{
-
a[j-1]=a[j-1]-1;
-
}
-
}
-
}
-
j=0;
-
for(i=strlen(a)-1;i>=0;i--)
-
{
-
b[j]=a[i]-'0';
-
j++;
-
}
-
temp=0;
-
for(i=0;i<j;i++)
-
{
-
x=b[i]*2+temp;
-
b[i]=x%10;
-
temp=x/10;
-
}
-
while(temp>0)
-
{
-
b[i]=temp%10;
-
temp=temp/10;
-
i++;
-
}
-
for(j=i-1;j>=0;j--)
-
printf("%d",b[j]);
-
}
-
printf("\n");
-
}
-
return(0);
-
-
}
-
Rabbit 12,516
Recognized Expert Moderator MVP
What answer is it showing on spoj?
it just shows wrong answer
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
|
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...
|
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...
|
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=
|
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"...
|
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...
|
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 {
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |