Skip to content

Commit

Permalink
🎨 style: improve format of the code
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchuo committed Sep 5, 2018
1 parent b06ec7c commit 033c694
Show file tree
Hide file tree
Showing 37 changed files with 338 additions and 253 deletions.
9 changes: 3 additions & 6 deletions AdvancedLevel_C++/1070. Mooncake (25).cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ int main() {
int n, need;
cin >> n >> need;
vector<mooncake> a(n);
for (int i = 0; i < n; i++)
cin >> a[i].mount;
for (int i = 0; i < n; i++)
cin >> a[i].price;
for (int i = 0; i < n; i++)
a[i].unit = a[i].price / a[i].mount;
for (int i = 0; i < n; i++) scanf("%f", &a[i].mount);
for (int i = 0; i < n; i++) scanf("%f", &a[i].price);
for (int i = 0; i < n; i++) a[i].unit = a[i].price / a[i].mount;
sort(a.begin(), a.end(), cmp);
float result = 0.0;
for (int i = 0; i < n; i++) {
Expand Down
76 changes: 31 additions & 45 deletions AdvancedLevel_C++/1110. Complete Binary Tree (25).cpp
Original file line number Diff line number Diff line change
@@ -1,56 +1,42 @@
#include <iostream>
#include <queue>
#include <vector>
#include <string>
using namespace std;
struct TREE {
int left, right;
};
struct node{
int l, r;
}a[100];
int maxn = -1, ans;
void dfs(int root, int index) {
if(index > maxn) {
maxn = index;
ans = root;
}
if(a[root].l != -1) dfs(a[root].l, index * 2);
if(a[root].r != -1) dfs(a[root].r, index * 2 + 1);
}
int main() {
int n, root = 0;
scanf("%d", &n);
vector<TREE> tree(n);
vector<int> book(n);
for(int i = 0; i < n; i++) {
int n, root = 0, have[100] = {0};
cin >> n;
for (int i = 0; i < n; i++) {
string l, r;
cin >> l >> r;
if(l == "-") {
tree[i].left = -1;
if (l == "-") {
a[i].l = -1;
} else {
tree[i].left = stoi(l);
book[tree[i].left] = 1;
a[i].l = stoi(l);
have[stoi(l)] = 1;
}
if(r == "-"){
tree[i].right = -1;
if (r == "-") {
a[i].r = -1;
} else {
tree[i].right = stoi(r);
book[tree[i].right] = 1;
}
}
for(int i = 0; i < n; i++) {
if(book[i] == 0) {
root = i;
break;
}
}
queue<int> q;
q.push(root);
int cnt = 0, lastnode = 0;
while(!q.empty()) {
int node = q.front();
q.pop();
if(node != -1) {
lastnode = node;
cnt++;
}else {
if(cnt != n)
printf("NO %d", root);
else
printf("YES %d", lastnode);
return 0;
a[i].r = stoi(r);
have[stoi(r)] = 1;
}
q.push(tree[node].left);
q.push(tree[node].right);
}
while (have[root] != 0) root++;
dfs(root, 1);
if (maxn == n)
cout << "YES " << ans;
else
cout << "NO " << root;
return 0;
}
}

18 changes: 6 additions & 12 deletions BasicLevel_C++/1027. 打印沙漏(20).cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
#include <iostream>
using namespace std;
int main() {
int N;
int N, row = 0;
char c;
cin >> N >> c;
int row = 0;
for (int i = 0; i < N; i++) {
if ((2 * i * (i + 2) + 1) > N) {
row = i - 1;
break;
}
}
for (int i = row; i >= 1; i--) {
for (int k = row - i; k >= 1; k--)
cout << " ";
for (int j = i * 2 + 1; j >= 1; j--)
cout << c;
for (int k = row - i; k >= 1; k--) cout << " ";
for (int j = i * 2 + 1; j >= 1; j--) cout << c;
cout << endl;
}
for (int i = 0; i < row; i++)
cout << " ";
for (int i = 0; i < row; i++) cout << " ";
cout << c << endl;
for (int i = 1; i <= row; i++) {
for (int k = row - i; k >= 1; k--)
cout << " ";
for (int j = i * 2 + 1; j >= 1; j--)
cout << c;
for (int k = row - i; k >= 1; k--) cout << " ";
for (int j = i * 2 + 1; j >= 1; j--) cout << c;
cout << endl;
}
cout << (N - (2 * row * (row + 2) + 1));
Expand Down
13 changes: 4 additions & 9 deletions BasicLevel_C++/1031. 查验身份证(15).cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,21 @@
using namespace std;
bool func(string a);
int main() {
int n;
int count = 0;
int n, count = 0;
cin >> n;
for (int i = 0; i < n; i++) {
string a;
cin >> a;
if (func(a)) count++;
}
if (count == 0)
cout << "All passed";
if (count == 0) cout << "All passed";
return 0;
}

bool func(string s) {
int sum = 0;
int a[18];
int sum = 0, a[18];
for (int i = 0; i < 17; i++)
a[i] = s[i] - '0';
if (s[17] == 'X')
a[17] = 10;
if (s[17] == 'X') a[17] = 10;
else
a[17] = s[17] - '0';
int b[17] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
Expand Down
37 changes: 10 additions & 27 deletions CCCC-GPLT/L1-002. 打印沙漏 .cpp
Original file line number Diff line number Diff line change
@@ -1,44 +1,27 @@
include <iostream>
#include <iostream>
using namespace std;
int main() {
int N;
cin >> N;
int N, row = 0;
char c;
cin >> c;
//计算行数row,只计算一半的行数,不包括最中间的那个一个符号
int row = 0;
for (int i = 1; i < N; i++) {
cin >> N >> c;
for (int i = 0; i < N; i++) {
if ((2 * i * (i + 2) + 1) > N) {
row = i - 1;
break;
}
}
// 打印上半部分
for (int i = row; i >= 1; i--) {
for (int k = row - i; k >= 1; k--) {
cout << " ";
}
for (int j = i * 2 + 1; j >= 1; j--) {
cout << c;
}
for (int k = row - i; k >= 1; k--) cout << " ";
for (int j = i * 2 + 1; j >= 1; j--) cout << c;
cout << endl;
}
// 打印中间的那个符号
for (int i = 0; i < row; i++) {
cout << " ";
}
for (int i = 0; i < row; i++) cout << " ";
cout << c << endl;
// 打印下半部分
for (int i = 1; i <= row; i++) {
for (int k = row - i; k >= 1; k--) {
cout << " ";
}
for (int j = i * 2 + 1; j >= 1; j--) {
cout << c;
}
for (int k = row - i; k >= 1; k--) cout << " ";
for (int j = i * 2 + 1; j >= 1; j--) cout << c;
cout << endl;
}
// 输出还剩下几个符号没有用
cout << (N - (2 * row * (row + 2) + 1));
cout << (N - (2 * row * (row + 2) + 1));
return 0;
}
8 changes: 2 additions & 6 deletions CCCC-GPLT/L1-003. 个位数统计 .cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@ int main() {
cin >> s;
int len = s.length();
int a[10] = {0};
for (int i = 0; i < len; i++) {
a[s[i] - '0']++;
}
for (int i = 0; i < len; i++) a[s[i] - '0']++;
for (int i = 0; i < 10; i++) {
if (a[i] != 0) {
cout << i << ":" << a[i] << endl;
}
if (a[i] != 0) cout << i << ":" << a[i] << endl;
}
return 0;
}
18 changes: 6 additions & 12 deletions CCCC-GPLT/L1-005. 考试座位号 .cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
#include <iostream>
#include <vector>
using namespace std;
struct stu {
string s;
int one;
int two;
int one, two;
};

int main() {
int n;
int n, m, temp;
cin >> n;
stu *a = new stu [n];
vector<stu> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i].s;
cin >> a[i].one;
cin >> a[i].two;
cin >> a[i].s >> a[i].one >> a[i].two;
}
int m;
cin >> m;
int temp;
for (int i = 0; i < m; i++) {
cin >> temp;
for (int j = 0; j < n; j++) {
if (a[j].one == temp) {
cout << a[j].s << " ";
cout << a[j].two << endl;
cout << a[j].s << " " << a[j].two << endl;
break;
}
}
Expand Down
30 changes: 0 additions & 30 deletions CCCC-GPLT/L1-006. 连续因子.cpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,3 @@
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main() {
int n;
scanf("%d", &n);
int max = sqrt(n);
for(int len = 12; len >= 1; len--) {
for(int start = 2; start <= max; start++) {
long long int ans = 1;
for(int i = start; i - start <= len - 1; i++) {
ans *= i;
}
if(n % ans == 0) {
printf("%d\n%d", len, start);
for(int i = start + 1; i - start <= len - 1; i++) {
printf("*%d", i);
}
return 0;
}
}
}
printf("1\n%d", n);
return 0;
}

[Update v2.0] 由github用户littlesevenmo提供的更高效的解法
不用算连续因子最多不会超过12个。也不需要三重循环,两重循环即可,直接去计算当前部分乘积能不能整除N,代码如下:

#include <iostream>
#include <cmath>
using namespace std;
Expand Down
3 changes: 1 addition & 2 deletions CCCC-GPLT/L1-008. 求整数段和.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ using namespace std;
int main() {
int a, b;
cin >> a >> b;
int temp = a;
int sum = 0;
int temp = a, sum = 0;
while(temp <= b) {
for(int j = 0; j < 5 && temp <= b; j++) {
printf("%5d", temp);
Expand Down
12 changes: 3 additions & 9 deletions CCCC-GPLT/L1-010. 比较大小.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@ using namespace std;
int main() {
int a, b, c;
cin >> a >> b >> c;
if(b > c) {
swap(b, c);
}
if(a > b) {
swap(a, b);
}
if(b > c) {
swap(b, c);
}
if(b > c) swap(b, c);
if(a > b) swap(a, b);
if(b > c) swap(b, c);
cout << a << "->" << b << "->" << c;
return 0;
}
8 changes: 2 additions & 6 deletions CCCC-GPLT/L1-011. A-B.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ int main() {
string s, a;
getline(cin, s);
getline(cin, a);
for(int i = 0; i < a.length(); i++) {
book[a[i]] = 1;
}
for(int i = 0; i < a.length(); i++) book[a[i]] = 1;
for(int i = 0; i < s.length(); i++) {
if(book[s[i]] == 1) {
continue;
}
if(book[s[i]] == 1) continue;
cout << s[i];
}
return 0;
Expand Down
7 changes: 2 additions & 5 deletions CCCC-GPLT/L1-013. 计算阶乘和.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
#include <iostream>
using namespace std;
int main() {
int n;
int n, ans = 0;
cin >> n;
int ans = 0;
for(int i = 1; i <= n; i++) {
int temp = 1;
for(int j = 1; j <= i; j++) {
temp *= j;
}
for(int j = 1; j <= i; j++) temp *= j;
ans += temp;
}
cout << ans;
Expand Down
Loading

0 comments on commit 033c694

Please sign in to comment.