forked from t3nsor/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bishops.cpp
79 lines (79 loc) · 976 Bytes
/
bishops.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// 2008-06-19
#include <iostream>
#include <cstring>
using namespace std;
#define MAX(x,y) ((x)>(y)?(x):(y))
void swap(char&x,char&y)
{
char t=x;
x=y;
y=t;
}
int main()
{
char s[110];
int i;
for(;;)
{
scanf("%s",&s);
int l=strlen(s);
if (l==0)
return 0;
if (l==1&&s[0]=='0')
{
printf("0\n");
s[0]=0;
continue;
}
if (l==1&&s[0]=='1')
{
printf("1\n");
s[0]=0;
continue;
}
for (i=0; i<l/2; i++)
swap(s[i],s[l-i-1]);
int carry=0;
for (i=0; i<l; i++)
{
s[i]=2*(s[i]-'0')+carry;
if (s[i]>=10)
{
carry=1;
s[i]-=10;
}
else
carry=0;
}
if (carry)
s[l++]=1;
bool borrow;
s[0]-=2;
if (s[0]<0)
{
borrow=true;
s[0]+=10;
}
else
borrow=false;
i=1;
while (borrow)
{
s[i]--;
if (s[i]==-1)
{
s[i]=9;
borrow=true;
}
else
borrow=false;
i++;
}
if (s[l-1]==0)
l--;
for (i=l-1; i>=0; i--)
putchar(s[i]+'0');
putchar('\n');
s[0]=0;
}
}