-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP537.cpp
80 lines (77 loc) · 1.55 KB
/
P537.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
#include <iostream>
#include <stdio.h>
double readDouble(char *w, int &idx) {
++idx;
unsigned long ret = 0;
char c;
double neg = (w[idx] == '-') ? -1 : 1;
if(w[idx] == '-')
++idx;
unsigned long divider = 1;
bool dot = false;
for(; isdigit(c = w[idx]) || c == '.'; ++idx) {
if(c == '.') {
dot = true;
continue;
}
ret = 10*ret + (c-'0');
if(dot)
divider*=10;
}
//std::cerr << "Ret: " << ret << ", divider: " << divider << ", c: " << c << std::endl;
switch(c) {
case 'm':
return neg*ret/1000.0/divider;
case 'k':
return neg*ret*1000.0/divider;
case 'M':
return neg*ret*1000000.0/divider;
}
return neg*ret/(double)divider;
}
int main() {
char c, w[100000];
// Get cases:
int cases = 0;
gets(w);
int idx = 0;
for(; isdigit(c = w[idx]); ++idx)
cases = 10*cases + (c-'0');
// Handle lines:
for(int cas = 1; cas <= cases; ++cas) {
gets(w);
bool setP = false, setU = false, setI = false;
double P, U, I;
idx = 1;
for(int j = 0; j < 2; ++j) {
// Find '=':
for(; w[idx] != '='; ++idx)
;
switch(w[idx-1]) {
case 'P':
P = readDouble(w, idx);
setP = true;
break;
case 'U':
U = readDouble(w, idx);
setU = true;
break;
default: //case 'I':
I = readDouble(w, idx);
setI = true;
break;
}
}
printf("Problem #%d\n", cas);
if(!setP) {
printf("P=%.2fW\n", U*I);
}
else if(!setU) {
printf("U=%.2fV\n", P/I);
}
else if(!setI) {
printf("I=%.2fA\n", P/U);
}
printf("\n");
}
}