forked from cacophonix/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CERC07N.cpp
107 lines (99 loc) · 2.37 KB
/
CERC07N.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
/*
USER: zobayer
TASK: CERC07N
ALGO: ad-hoc
*/
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <queue>
#include <algorithm>
#include <functional>
#include <iterator>
#include <limits>
#include <numeric>
#include <utility>
#include <valarray>
using namespace std;
#define DEBUG if(0)
#define PAUSE system("pause")
#define ALL(c) c.begin(), c.end()
#define PB(x) push_back(x)
#define SET(p) memset(p, -1, sizeof(p))
#define CLR(p) memset(p, 0, sizeof(p))
#define BIG(p) memset(p, 0x3f, sizeof(p))
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define SZ(c) c.size()
typedef long long i64;
typedef unsigned long long u64;
typedef pair< int, int > pii;
template< class T > T _abs(const T &n) { return (n < 0 ? -n : n); }
template< class T > T _max(const T &a, const T &b) { return (a > b ? a : b); }
template< class T > T _min(const T &a, const T &b) { return (a < b ? a : b); }
template< class T > T gcd(const T a, const T b) { return (b ? gcd<T>(b, a%b) : a); }
template< class T > T lcm(const T a, const T b) { return (a / gcd<T>(a, b) * b); }
template< class T > bool inrange(const T &a, const T &b, const T &c) { return a<=b && b<=c; }
template< class T > T sq(const T &x) { return x * x; }
const double EPS = 1e-10;
const int INF = 0x3f3f3f3f;
int from(char *val, int base) {
int i, ret = 0;
for(i=0; val[i]; i++) {
ret += val[i]-'0';
ret *= base;
}
ret /= base;
return ret;
}
string to(char *valstr, int base) {
int val, tmp;
string ret = "";
sscanf(valstr, "%d", &val);
while(val) {
tmp = val % base;
val /= base;
if(tmp < 0) {
tmp += (-base);
val++;
}
ret.push_back(tmp + '0');
}
if(ret.length()==0) ret.push_back('0');
reverse(ret.begin(), ret.end());
return ret;
}
int main() {
char sbase[40], value[40];
int base;
while(scanf("%s", sbase)==1) {
if(sbase[0]=='e') break;
scanf("%s", value);
if(sbase[0]=='f') {
sscanf(&sbase[4], "%d", &base);
printf("%d\n", from(value, base));
}
else if(sbase[0]=='t') {
sscanf(&sbase[2], "%d", &base);
printf("%s\n", to(value, base).c_str());
}
}
return 0;
}