forked from Guarav/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMINNUM.cpp
129 lines (112 loc) · 2.83 KB
/
MINNUM.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
using namespace std;
int divideBigNumber( char inputString[50], int quotient[50], int res[2] )
{
int length = strlen(inputString);
int dividend ;
char dividendStr[3] ;
dividendStr[0] = inputString[0];
dividendStr[1] = '\0';
int quotientCount = 0;
for(int i = 1; i< length; ++i)
{
dividend = atoi(dividendStr);
if(dividend >= 9)
{
quotient[quotientCount++] = dividend/9;
if( dividend % 9 != 0)
{
dividendStr[0] = dividend % 9 + '0';
dividendStr[1] = inputString[i];
dividendStr[2] = '\0';
}
else
{
dividendStr[0] = inputString[i];
dividendStr[1] = '\0';
}
}
else
{
if( strlen(dividendStr) == 0)
{
quotient[quotientCount++] = 0;
dividendStr[0] = inputString[i];
dividendStr[1] = '\0';
}
else
{
quotient[quotientCount++] = 0;
dividendStr[1] = inputString[i];
dividendStr[2] = '\0';
}
}
}
dividend = atoi(dividendStr);
int remainder;
if(dividend >= 9)
{
quotient[quotientCount++] = dividend/9;
remainder = dividend%9;
}
else
{
quotient[quotientCount++] = 0;
remainder = dividend;
}
res[0] = remainder;
res[1] = quotientCount;
}
int main()
{
int t;
char inputString[50];
//scanf("%d", &t);
while(1)
{
scanf("%s", inputString);
if(!strcmp(inputString, "-1"))
break;
if(strlen(inputString) == 1 && inputString[0] == '0')
{
printf("0\n");
continue;
}
if(strlen(inputString) == 1)
{
printf("1\n");
continue;
}
int quotient[50] = {0};
int res[2] = {0};
divideBigNumber( inputString, quotient, res ) ;
int remainder = res[0] ;
int quotientCount = res[1];
int result[100] = {0};
int k = 99;
int carry = 0 ;
if( remainder )
carry = 1;
for(int i = quotientCount - 1;i >= 0; --i)
{
int temp = quotient[i] + carry;
result[k--] = temp%10;
carry = temp/10;
}
int index = 0;
for(int i = 0;i < 100;++i)
if(result[i] != 0)
{
index = i;
break;
}
for(int i = index; i < 100;++i)
printf("%d", result[i]);
printf("\n");
}
return 0;
}