Skip to content

Commit

Permalink
🔨 refactor: refactor code for pat advanced 1061 1073 1088
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchuo committed Mar 16, 2019
1 parent dbedba0 commit b118f24
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 123 deletions.
27 changes: 13 additions & 14 deletions AdvancedLevel_C++/1061. Dating (20).cpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main() {
string a, b, c, d;
cin >> a >> b >> c >> d;
int min1 = min(a.length(), b.length());
int min2 = min(c.length(), d.length());
string weekday[7] = {"MON ", "TUE ", "WED ", "THU ", "FRI ", "SAT ", "SUN "};
char t[2];
int pos, j;
for (int i = 0; i < min1; i++) {
int pos, i = 0, j = 0;
while(i < a.length() && i < b.length()) {
if (a[i] == b[i] && (a[i] >= 'A' && a[i] <= 'G')) {
t[0] = a[i];
j = i;
break;
}
i++;
}
for (int i = j + 1; i < min1; i++) {
i = i + 1;
while (i < a.length() && i < b.length()) {
if (a[i] == b[i] && ((a[i] >= 'A' && a[i] <= 'N') || isdigit(a[i]))) {
t[1] = a[i];
break;
}
i++;
}
for (int i = 0; i < min2; i++) {
if (c[i] == d[i] && isalpha(c[i])) {
pos = i;
while (j < c.length() && j < d.length()) {
if (c[j] == d[j] && isalpha(c[j])) {
pos = j;
break;
}
j++;
}
int m = t[1] - '0';
if (!isdigit(t[1])) m = t[1] - 'A' + 10;
cout << weekday[t[0] - 'A'];
string week[7] = {"MON ", "TUE ", "WED ", "THU ", "FRI ", "SAT ", "SUN "};
int m = isdigit(t[1]) ? t[1] - '0' : t[1] - 'A' + 10;
cout << week[t[0]-'A'];
printf("%02d:%02d", m, pos);
return 0;
}
49 changes: 16 additions & 33 deletions AdvancedLevel_C++/1073. Scientific Notation (20).cpp
Original file line number Diff line number Diff line change
@@ -1,44 +1,27 @@
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main() {
string s;
cin >> s;
int len = s.length();
int i = 0;
while (s[i] != 'E') i++;
string t = s.substr(1, i-1);
int n = stoi(s.substr(i+1));
if (s[0] == '-') cout << "-";
int pose = 0;
for (int i = 1; i < len; i++)
if (s[i] == 'E') pose = i;
int after = 0;
for (int i = pose + 2; i < len; i++)
after = (s[i] - '0') + 10 * after;
if (s[pose + 1] == '-') {
if (0 < after) {
cout << "0.";
for (int i = 1; i < after; i++)
cout << 0;
for (int i = 1; i < pose; i++)
if (isdigit(s[i])) cout << s[i];
} else {
for (int i = 1; i < pose; i++) {
if (i == 2 - after) cout << ".";
if (isdigit(s[i])) cout << s[i];
}
}
if (n < 0) {
cout << "0.";
for (int j = 0; j < abs(n) - 1; j++) cout << '0';
for (int j = 0; j < t.length(); j++)
if (t[j] != '.') cout << t[j];
} else {
if (pose - 3 < after) {
if (s[1] != '0') cout << s[1];
for (int i = 3; i < pose; i++)
if (isdigit(s[i])) cout << s[i];
for (int i = 0; i < after - (pose - 3); i++)
cout << 0;
cout << t[0];
int cnt, j;
for (j = 2, cnt = 0; j < t.length() && cnt < n; j++, cnt++) cout << t[j];
if (j == t.length()) {
for (int k = 0; k < n - cnt; k++) cout << '0';
} else {
if (s[1] != '0') cout << s[1];
for (int i = 3; i < pose; i++) {
if (i == 3 + after) cout << ".";
if (isdigit(s[i])) cout << s[i];
}
cout << '.';
for (int k = j; k < t.length(); k++) cout << t[k];
}
}
return 0;
Expand Down
55 changes: 17 additions & 38 deletions AdvancedLevel_C++/1088. Rational Arithmetic (20).cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,30 @@ long long gcd(long long t1, long long t2) {
return t2 == 0 ? t1 : gcd(t2, t1 % t2);
}
void func(long long m, long long n) {
int flag1 = 0, flag2 = 0, flag = 0;
if (n == 0) {
printf("Inf");
if (m * n == 0) {
printf("%s", n == 0 ? "Inf" : "0");
return ;
}
if (m == 0) {
printf("0");
bool flag = ((m < 0 && n > 0) || (m > 0 && n < 0));
m = abs(m); n = abs(n);
long long x = m / n;
printf("%s", flag ? "(-" : "");
if (x != 0) printf("%lld", x);
if (m % n == 0) {
if(flag) printf(")");
return ;
}
if (m < 0) flag1 = 1;
if (n < 0) flag2 = 1;
m = abs(m), n = abs(n);
if (flag1 == 1 && flag2 == 1) flag = 0;
else if (flag1 == 1 || flag2 == 1) flag = 1;
if (m == n) {
if (flag == 1) printf("(-1)");
else printf("1");
return;
}
long long x = m % n, y = m / n;
if (x == 0) {
if (flag == 0) printf("%d", y);
else printf("(-%d)", y);
return ;
} else {
long long t1 = m - y * n, t2 = n, t = gcd(t1, t2);
t1 = t1 / t, t2 = t2 / t;
if (flag == 1) {
printf("(-");
if (y != 0) printf("%lld %lld/%lld)", y, t1, t2);
else printf("%d/%d)", t1, t2);
} else {
if (y != 0) printf("%lld %lld/%lld", y, t1, t2);
else printf("%lld/%lld", t1, t2);
}
}
if (x != 0) printf(" ");
m = m - x * n;
long long t = gcd(m, n);
m = m / t; n = n / t;
printf("%lld/%lld%s", m, n, flag ? ")" : "");
}
void print() {
int main() {
scanf("%lld/%lld %lld/%lld", &a, &b, &c, &d);
func(a, b); printf(" + "); func(c, d); printf(" = "); func(a * d + b * c, b * d); printf("\n");
func(a, b); printf(" - "); func(c, d); printf(" = "); func(a * d - b * c, b * d); printf("\n");
func(a, b); printf(" * "); func(c, d); printf(" = "); func(a * c, b * d); printf("\n");
func(a, b); printf(" / "); func(c, d); printf(" = "); func(a * d, b * c); printf("\n");
}
int main() {
scanf("%lld/%lld %lld/%lld", &a, &b, &c, &d);
print();
func(a, b); printf(" / "); func(c, d); printf(" = "); func(a * d, b * c);
return 0;
}
55 changes: 17 additions & 38 deletions BasicLevel_C++/1034. 有理数四则运算(20).cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,30 @@ long long gcd(long long t1, long long t2) {
return t2 == 0 ? t1 : gcd(t2, t1 % t2);
}
void func(long long m, long long n) {
int flag1 = 0, flag2 = 0, flag = 0;
if (n == 0) {
printf("Inf");
if (m * n == 0) {
printf("%s", n == 0 ? "Inf" : "0");
return ;
}
if (m == 0) {
printf("0");
bool flag = ((m < 0 && n > 0) || (m > 0 && n < 0));
m = abs(m); n = abs(n);
long long x = m / n;
printf("%s", flag ? "(-" : "");
if (x != 0) printf("%lld", x);
if (m % n == 0) {
if(flag) printf(")");
return ;
}
if (m < 0) flag1 = 1;
if (n < 0) flag2 = 1;
m = abs(m), n = abs(n);
if (flag1 == 1 && flag2 == 1) flag = 0;
else if (flag1 == 1 || flag2 == 1) flag = 1;
if (m == n) {
if (flag == 1) printf("(-1)");
else printf("1");
return;
}
long long x = m % n, y = m / n;
if (x == 0) {
if (flag == 0) printf("%d", y);
else printf("(-%d)", y);
return ;
} else {
long long t1 = m - y * n, t2 = n, t = gcd(t1, t2);
t1 = t1 / t, t2 = t2 / t;
if (flag == 1) {
printf("(-");
if (y != 0) printf("%lld %lld/%lld)", y, t1, t2);
else printf("%d/%d)", t1, t2);
} else {
if (y != 0) printf("%lld %lld/%lld", y, t1, t2);
else printf("%lld/%lld", t1, t2);
}
}
if (x != 0) printf(" ");
m = m - x * n;
long long t = gcd(m, n);
m = m / t; n = n / t;
printf("%lld/%lld%s", m, n, flag ? ")" : "");
}
void print() {
int main() {
scanf("%lld/%lld %lld/%lld", &a, &b, &c, &d);
func(a, b); printf(" + "); func(c, d); printf(" = "); func(a * d + b * c, b * d); printf("\n");
func(a, b); printf(" - "); func(c, d); printf(" = "); func(a * d - b * c, b * d); printf("\n");
func(a, b); printf(" * "); func(c, d); printf(" = "); func(a * c, b * d); printf("\n");
func(a, b); printf(" / "); func(c, d); printf(" = "); func(a * d, b * c); printf("\n");
}
int main() {
scanf("%lld/%lld %lld/%lld", &a, &b, &c, &d);
print();
func(a, b); printf(" / "); func(c, d); printf(" = "); func(a * d, b * c);
return 0;
}

0 comments on commit b118f24

Please sign in to comment.