Skip to content

Commit b118f24

Browse files
committed
🔨 refactor: refactor code for pat advanced 1061 1073 1088
1 parent dbedba0 commit b118f24

File tree

4 files changed

+63
-123
lines changed

4 files changed

+63
-123
lines changed
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
11
#include <iostream>
2-
#include <string>
32
#include <cctype>
43
using namespace std;
54
int main() {
65
string a, b, c, d;
76
cin >> a >> b >> c >> d;
8-
int min1 = min(a.length(), b.length());
9-
int min2 = min(c.length(), d.length());
10-
string weekday[7] = {"MON ", "TUE ", "WED ", "THU ", "FRI ", "SAT ", "SUN "};
117
char t[2];
12-
int pos, j;
13-
for (int i = 0; i < min1; i++) {
8+
int pos, i = 0, j = 0;
9+
while(i < a.length() && i < b.length()) {
1410
if (a[i] == b[i] && (a[i] >= 'A' && a[i] <= 'G')) {
1511
t[0] = a[i];
16-
j = i;
1712
break;
1813
}
14+
i++;
1915
}
20-
for (int i = j + 1; i < min1; i++) {
16+
i = i + 1;
17+
while (i < a.length() && i < b.length()) {
2118
if (a[i] == b[i] && ((a[i] >= 'A' && a[i] <= 'N') || isdigit(a[i]))) {
2219
t[1] = a[i];
2320
break;
2421
}
22+
i++;
2523
}
26-
for (int i = 0; i < min2; i++) {
27-
if (c[i] == d[i] && isalpha(c[i])) {
28-
pos = i;
24+
while (j < c.length() && j < d.length()) {
25+
if (c[j] == d[j] && isalpha(c[j])) {
26+
pos = j;
2927
break;
3028
}
29+
j++;
3130
}
32-
int m = t[1] - '0';
33-
if (!isdigit(t[1])) m = t[1] - 'A' + 10;
34-
cout << weekday[t[0] - 'A'];
31+
string week[7] = {"MON ", "TUE ", "WED ", "THU ", "FRI ", "SAT ", "SUN "};
32+
int m = isdigit(t[1]) ? t[1] - '0' : t[1] - 'A' + 10;
33+
cout << week[t[0]-'A'];
3534
printf("%02d:%02d", m, pos);
3635
return 0;
3736
}

AdvancedLevel_C++/1073. Scientific Notation (20).cpp

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,27 @@
11
#include <iostream>
2-
#include <string>
3-
#include <cctype>
42
using namespace std;
53
int main() {
64
string s;
75
cin >> s;
8-
int len = s.length();
6+
int i = 0;
7+
while (s[i] != 'E') i++;
8+
string t = s.substr(1, i-1);
9+
int n = stoi(s.substr(i+1));
910
if (s[0] == '-') cout << "-";
10-
int pose = 0;
11-
for (int i = 1; i < len; i++)
12-
if (s[i] == 'E') pose = i;
13-
int after = 0;
14-
for (int i = pose + 2; i < len; i++)
15-
after = (s[i] - '0') + 10 * after;
16-
if (s[pose + 1] == '-') {
17-
if (0 < after) {
18-
cout << "0.";
19-
for (int i = 1; i < after; i++)
20-
cout << 0;
21-
for (int i = 1; i < pose; i++)
22-
if (isdigit(s[i])) cout << s[i];
23-
} else {
24-
for (int i = 1; i < pose; i++) {
25-
if (i == 2 - after) cout << ".";
26-
if (isdigit(s[i])) cout << s[i];
27-
}
28-
}
11+
if (n < 0) {
12+
cout << "0.";
13+
for (int j = 0; j < abs(n) - 1; j++) cout << '0';
14+
for (int j = 0; j < t.length(); j++)
15+
if (t[j] != '.') cout << t[j];
2916
} else {
30-
if (pose - 3 < after) {
31-
if (s[1] != '0') cout << s[1];
32-
for (int i = 3; i < pose; i++)
33-
if (isdigit(s[i])) cout << s[i];
34-
for (int i = 0; i < after - (pose - 3); i++)
35-
cout << 0;
17+
cout << t[0];
18+
int cnt, j;
19+
for (j = 2, cnt = 0; j < t.length() && cnt < n; j++, cnt++) cout << t[j];
20+
if (j == t.length()) {
21+
for (int k = 0; k < n - cnt; k++) cout << '0';
3622
} else {
37-
if (s[1] != '0') cout << s[1];
38-
for (int i = 3; i < pose; i++) {
39-
if (i == 3 + after) cout << ".";
40-
if (isdigit(s[i])) cout << s[i];
41-
}
23+
cout << '.';
24+
for (int k = j; k < t.length(); k++) cout << t[k];
4225
}
4326
}
4427
return 0;

AdvancedLevel_C++/1088. Rational Arithmetic (20).cpp

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,30 @@ long long gcd(long long t1, long long t2) {
66
return t2 == 0 ? t1 : gcd(t2, t1 % t2);
77
}
88
void func(long long m, long long n) {
9-
int flag1 = 0, flag2 = 0, flag = 0;
10-
if (n == 0) {
11-
printf("Inf");
9+
if (m * n == 0) {
10+
printf("%s", n == 0 ? "Inf" : "0");
1211
return ;
1312
}
14-
if (m == 0) {
15-
printf("0");
13+
bool flag = ((m < 0 && n > 0) || (m > 0 && n < 0));
14+
m = abs(m); n = abs(n);
15+
long long x = m / n;
16+
printf("%s", flag ? "(-" : "");
17+
if (x != 0) printf("%lld", x);
18+
if (m % n == 0) {
19+
if(flag) printf(")");
1620
return ;
1721
}
18-
if (m < 0) flag1 = 1;
19-
if (n < 0) flag2 = 1;
20-
m = abs(m), n = abs(n);
21-
if (flag1 == 1 && flag2 == 1) flag = 0;
22-
else if (flag1 == 1 || flag2 == 1) flag = 1;
23-
if (m == n) {
24-
if (flag == 1) printf("(-1)");
25-
else printf("1");
26-
return;
27-
}
28-
long long x = m % n, y = m / n;
29-
if (x == 0) {
30-
if (flag == 0) printf("%d", y);
31-
else printf("(-%d)", y);
32-
return ;
33-
} else {
34-
long long t1 = m - y * n, t2 = n, t = gcd(t1, t2);
35-
t1 = t1 / t, t2 = t2 / t;
36-
if (flag == 1) {
37-
printf("(-");
38-
if (y != 0) printf("%lld %lld/%lld)", y, t1, t2);
39-
else printf("%d/%d)", t1, t2);
40-
} else {
41-
if (y != 0) printf("%lld %lld/%lld", y, t1, t2);
42-
else printf("%lld/%lld", t1, t2);
43-
}
44-
}
22+
if (x != 0) printf(" ");
23+
m = m - x * n;
24+
long long t = gcd(m, n);
25+
m = m / t; n = n / t;
26+
printf("%lld/%lld%s", m, n, flag ? ")" : "");
4527
}
46-
void print() {
28+
int main() {
29+
scanf("%lld/%lld %lld/%lld", &a, &b, &c, &d);
4730
func(a, b); printf(" + "); func(c, d); printf(" = "); func(a * d + b * c, b * d); printf("\n");
4831
func(a, b); printf(" - "); func(c, d); printf(" = "); func(a * d - b * c, b * d); printf("\n");
4932
func(a, b); printf(" * "); func(c, d); printf(" = "); func(a * c, b * d); printf("\n");
50-
func(a, b); printf(" / "); func(c, d); printf(" = "); func(a * d, b * c); printf("\n");
51-
}
52-
int main() {
53-
scanf("%lld/%lld %lld/%lld", &a, &b, &c, &d);
54-
print();
33+
func(a, b); printf(" / "); func(c, d); printf(" = "); func(a * d, b * c);
5534
return 0;
5635
}

BasicLevel_C++/1034. 有理数四则运算(20).cpp

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,30 @@ long long gcd(long long t1, long long t2) {
66
return t2 == 0 ? t1 : gcd(t2, t1 % t2);
77
}
88
void func(long long m, long long n) {
9-
int flag1 = 0, flag2 = 0, flag = 0;
10-
if (n == 0) {
11-
printf("Inf");
9+
if (m * n == 0) {
10+
printf("%s", n == 0 ? "Inf" : "0");
1211
return ;
1312
}
14-
if (m == 0) {
15-
printf("0");
13+
bool flag = ((m < 0 && n > 0) || (m > 0 && n < 0));
14+
m = abs(m); n = abs(n);
15+
long long x = m / n;
16+
printf("%s", flag ? "(-" : "");
17+
if (x != 0) printf("%lld", x);
18+
if (m % n == 0) {
19+
if(flag) printf(")");
1620
return ;
1721
}
18-
if (m < 0) flag1 = 1;
19-
if (n < 0) flag2 = 1;
20-
m = abs(m), n = abs(n);
21-
if (flag1 == 1 && flag2 == 1) flag = 0;
22-
else if (flag1 == 1 || flag2 == 1) flag = 1;
23-
if (m == n) {
24-
if (flag == 1) printf("(-1)");
25-
else printf("1");
26-
return;
27-
}
28-
long long x = m % n, y = m / n;
29-
if (x == 0) {
30-
if (flag == 0) printf("%d", y);
31-
else printf("(-%d)", y);
32-
return ;
33-
} else {
34-
long long t1 = m - y * n, t2 = n, t = gcd(t1, t2);
35-
t1 = t1 / t, t2 = t2 / t;
36-
if (flag == 1) {
37-
printf("(-");
38-
if (y != 0) printf("%lld %lld/%lld)", y, t1, t2);
39-
else printf("%d/%d)", t1, t2);
40-
} else {
41-
if (y != 0) printf("%lld %lld/%lld", y, t1, t2);
42-
else printf("%lld/%lld", t1, t2);
43-
}
44-
}
22+
if (x != 0) printf(" ");
23+
m = m - x * n;
24+
long long t = gcd(m, n);
25+
m = m / t; n = n / t;
26+
printf("%lld/%lld%s", m, n, flag ? ")" : "");
4527
}
46-
void print() {
28+
int main() {
29+
scanf("%lld/%lld %lld/%lld", &a, &b, &c, &d);
4730
func(a, b); printf(" + "); func(c, d); printf(" = "); func(a * d + b * c, b * d); printf("\n");
4831
func(a, b); printf(" - "); func(c, d); printf(" = "); func(a * d - b * c, b * d); printf("\n");
4932
func(a, b); printf(" * "); func(c, d); printf(" = "); func(a * c, b * d); printf("\n");
50-
func(a, b); printf(" / "); func(c, d); printf(" = "); func(a * d, b * c); printf("\n");
51-
}
52-
int main() {
53-
scanf("%lld/%lld %lld/%lld", &a, &b, &c, &d);
54-
print();
33+
func(a, b); printf(" / "); func(c, d); printf(" = "); func(a * d, b * c);
5534
return 0;
5635
}

0 commit comments

Comments
 (0)