-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
cutoff_rounder.cpp
83 lines (67 loc) · 1.87 KB
/
cutoff_rounder.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
// https://www.urionlinejudge.com.br/judge/en/problems/view/1305
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
string get_integer_part(string n) {
int length;
for (int i = 0; i < n.size(); i++) if (n[i] == '.') length = i;
return n.substr(0, length);
}
string get_decimal_part(string n) {
int initial;
for (int i = 0; i < n.size(); i++) if (n[i] == '.') initial = i+1;
return n.substr(initial, n.size() - initial);
}
int to_int(string num) {
int number=0, num_digit;
for (int i = 0; i < num.size(); i++) {
num_digit = num[i] - '0';
number += num_digit * pow(10, num.size()-i-1);
}
return number;
}
double to_double(string num) {
int num_digit;
double number=0;
for (int i = 0; i < num.size(); i++) {
num_digit = num[i] - '0';
number += num_digit / pow(10, i+1);
}
cout << "*" << number << endl;
return number;
}
bool is_num_decimal_greater_than_or_equal_to_cutoff_decimal(string num, string cutoff) {
int limit, num_digit, cutoff_digit;
if (num.size() > cutoff.size()) limit = cutoff.size();
else limit = num.size();
if (num == cutoff) {
return true;
} else {
for (int i = 0; i < limit; i++) {
num_digit = num[i] - '0';
cutoff_digit = cutoff[i] - '0';
if (num_digit > cutoff_digit) return true;
}
return false;
}
}
bool has_period(string n) {
for (int i = 0; i < n.size(); i++) if (n[i] == '.') return true;
return false;
}
int main() {
string num, cutoff;
while (cin >> num >> cutoff) {
if (has_period(num)) {
string integer_part = get_integer_part(num);
string num_decimal_part = get_decimal_part(num);
string cutoff_decimal_part = get_decimal_part(cutoff);
if (is_num_decimal_greater_than_or_equal_to_cutoff_decimal(num_decimal_part, cutoff_decimal_part)) cout << to_int(integer_part)+1 << endl;
else cout << to_int(integer_part) << endl;
} else {
cout << num << endl;
}
}
return 0;
}