This repository has been archived by the owner on Nov 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
1001.cpp
156 lines (144 loc) · 3.32 KB
/
1001.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE (125)
#define ITEM_SIZE (10)
static char item[ITEM_SIZE][SIZE];
static char * poj_1001(char dst[], char src[], int src_width, int n);
static char * move_leftward (char dst[], char src[], int n);
static char * add_to(char dst[], char src[]);
int main(int argc, char* argv[]){
int i,j,m,n,b;
int point;
char number[5];
char buf[SIZE];
char str_temp[7];
int n_temp;
while(2 == scanf("%s%d", str_temp, &n_temp)){
point = 0;
for(i=0,j=0; i<6; i++){
if(str_temp[i] != '.'){
number[j++] = str_temp[i];
}
else{
point = i;
}
}
point = 5 - point;
point = n_temp * point;
point = SIZE - point -1;
poj_1001(buf,number,5,n_temp);
m=0;
n=SIZE-1;
for (i=0;i<SIZE;i++){
if ( '0' != buf[i] ){
m = i;
break;
}
}
for (i=SIZE-1; i>=0; i--){
if ( '0' != buf[i] ){
n = i;
break;
}
}
b=1;
for (i=0;i<SIZE;i++){
if('0' != buf[i]){
b=0;
}
}
if (b){
printf("0");
}
else {
if (m>point){
printf(".");
for (i=point+1; i<=n; i++){
printf ("%c", buf[i]);
}
}
else if (m<=point && point<n){
for (i=m;i<=point;i++){
printf ("%c", buf[i]);
}
printf (".");
for (i=point+1;i<=n;i++){
printf ("%c", buf[i]);
}
}
else{
for (i=m;i<=point;i++){
printf ("%c", buf[i]);
}
}
}
printf ("\n");
}
return 0;
}
static char * poj_1001 (char dst[], char src[], int src_width, int n){
int i,j,k;
int carry, bitsum;
char buf[SIZE];
char aux[SIZE];
memset(item, '0', ITEM_SIZE*SIZE);
memcpy(item[1]+SIZE-src_width, src, src_width);
bitsum = 0;
for (i=2;i<=9;i++){
carry = 0;
for (j=SIZE-1; j>=0; j--){
k = j - (SIZE - src_width);
if (0<=k){
bitsum = i * (src[k] - '0') + carry;
item[i][j] = '0' + (bitsum % 10);
carry = bitsum / 10;
}
else{
item[i][j] = '0' + carry;
break;
}
}
}
if (0 == n){
memset (dst, '0', SIZE);
memset (dst + SIZE - 1, '1', 1);
}
else if (1 == n){
memcpy (dst, item[1], SIZE);
}
else{
memcpy (dst, item[1], SIZE);
for (i=0; i<n-1; i++){
memset (aux, '0', SIZE);
for (j=0; j<SIZE; j++){
move_leftward (buf, item[dst[j]-'0'], SIZE-1-j);
add_to (aux, buf);
}
memcpy (dst, aux, SIZE);
}
}
return dst;
}
static char * add_to (char dst[], char src[]){
int carry, bitsum;
int i;
carry = 0;
bitsum = 0;
for (i=SIZE-1; i>=0; i--){
bitsum = carry + (dst[i] - '0') + (src[i] - '0');
dst[i] = ('0' + bitsum % 10);
carry = bitsum / 10;
}
return dst;
}
static char * move_leftward (char dst[], char src[], int n){
int i;
int tail;
tail = SIZE-1-n;
memset (dst, '0', SIZE);
for (i=0; i<=tail; i++){
dst[i] = src[i+n];
}
return dst;
}