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 Aug 31, 2018
1 parent 6054e07 commit 3f94a4d
Show file tree
Hide file tree
Showing 147 changed files with 451 additions and 5,053 deletions.
13 changes: 1 addition & 12 deletions AdvancedLevel_C++/1001. A+B Format (20).cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
1001. A+B Format (20)
Calculate a + b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits).
Input
Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.
Output
For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.
Sample Input
-1000000 9
Sample Output
-999,991

#include <iostream>
using namespace std;
int main() {
Expand All @@ -22,4 +11,4 @@ int main() {
if ((i + 1) % 3 == len % 3 && i != len - 1) cout << ",";
}
return 0;
}
}
62 changes: 14 additions & 48 deletions AdvancedLevel_C++/1002. A+B for Polynomials (25).cpp
Original file line number Diff line number Diff line change
@@ -1,61 +1,27 @@
1002. A+B for Polynomials (25)
This time, you are supposed to find A+B where A and B are two polynomials.

Input
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 aN1 N2 aN2 ... NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, ..., K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10,0 <= NK < ... < N2 < N1 <=1000.

Output
For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.

Sample Input
2 1 2.4 0 3.2
2 2 1.5 1 0.5
Sample Output
3 2 1.5 1 2.9 0 3.2

#include <iostream>
using namespace std;
int main() {
float a[1001], b[1001], c[1001];
for (int i = 0; i < 1001; i++)
c[i] = 0.0;
int m, n;
cin >> m;
float c[1001] = {0};
int m, n, t;
float num;
scanf("%d", &m);
for (int i = 0; i < m; i++) {
int t;
cin >> t;
cin >> a[t];
c[t] += a[t];
scanf("%d%f", &t, &num);
c[t] += num;
}
cin >> n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int t;
cin >> t;
cin >> b[t];
c[t] += b[t];
scanf("%d%f", &t, &num);
c[t] += num;
}
int count = 0;
int cnt = 0;
for (int i = 0; i < 1001; i++) {
if (c[i] != 0)
count++;
if (c[i] != 0) cnt++;
}
cout << count;
if (count != 0)
cout << " ";
int temp = 1001;
printf("%d", cnt);
for (int i = 1000; i >= 0; i--) {
if (c[i] != 0.0 && count > 1) {
cout << i << " ";
printf("%.1f ", c[i]);
count--;
temp = i;
}
}
for (int i = temp - 1; i >= 0; i--) {
if (c[i] != 0.0) {
cout << i << " ";
printf("%.1f", c[i]);
}
if (c[i] != 0.0)
printf(" %d %.1f", i, c[i]);
}
return 0;
}
33 changes: 3 additions & 30 deletions AdvancedLevel_C++/1003. Emergency (25) .cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,8 @@
1003. Emergency (25)
As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

Input

Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (<= 500) - the number of cities (and the cities are numbered from 0 to N-1), M - the number of roads, C1 and C2 - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.

Output

For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather.
All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

Sample Input
5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1
Sample Output
2 4
题目大意:n个城市m条路,每个城市有救援小组,所有的边的边权已知。给定起点和重点,求从起点到重点的最短路径条数以及最短路径上的救援小组数目之和。如果有多条就输出点权(城市救援小组数目)最大的那个~
分析:用一遍dijkstra算法。设立num[i]和w[i]表示从出发点到i结点拥有的路的条数,以及能够找到的救援队的数目~~~当判定dis[u] + e[u][v] < dis[v]的时候,不仅仅要更新dis[v],还要更新num[v] = num[u], w[v] = weight[v] + w[u]; 如果dis[u] + e[u][v] == dis[v],还要更新num[v] += num[u],而且判断一下是否权重w[v]更小,如果更小了就更新w[v] = weight[v] + w[u];

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int n, m, c1, c2;
int dis[510], weight[510], e[510][510], num[510], w[510];
int e[510][510], weight[510], dis[510], num[510], w[510];
bool visit[510];
const int inf = 99999999;
int main() {
Expand All @@ -40,8 +14,7 @@ int main() {
int a, b, c;
for(int i = 0; i < m; i++) {
scanf("%d%d%d", &a, &b, &c);
e[a][b] = c;
e[b][a] = c;
e[a][b] = e[b][a] = c;
}
dis[c1] = 0;
w[c1] = weight[c1];
Expand Down
63 changes: 0 additions & 63 deletions AdvancedLevel_C++/1004. Counting Leaves (30) .cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,3 @@
1004. Counting Leaves (30)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.
Input
Each input file contains one test case. Each case starts with a line containing 0 < N < 100, the number of nodes in a tree, and M (< N), the number of non-leaf nodes. Then M lines follow, each in the format:
ID K ID[1] ID[2] ... ID[K]
where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID's of its children. For the sake of simplicity, let us fix the root ID to be 01.
Output
For each test case, you are supposed to count those family members who have no child for every seniority level starting from the root. The numbers must be printed in a line, separated by a space, and there must be no extra space at the end of each line.
The sample case represents a tree with only 2 nodes, where 01 is the root and 02 is its only child. Hence on the root 01 level, there is 0 leaf node; and on the next level, there is 1 leaf node. Then we should output "0 1" in a line.
Sample Input
2 1
01 1 02
Sample Output
0 1
题目大意:给出一棵树,问每一层各有多少个叶子结点。
分析:可以用dfs也可以用bfs。
dfs的话,用二维数组保存每一个有孩子结点的结点以及他们的孩子结点,从根结点开始遍历,直到遇到叶子结点,就将当前层数depth的book[depth]++; 标记第depth层拥有的叶子结点数,最后输出
如果用bfs,设立两个数组,第一个level,保存i结点的层数,为了bfs的时候可以让当前结点的层数是它的父结点层数+1,第二个数组book,保存i层所拥有的叶子结点的个数。变量maxlevel保存最大的层数~~

dfs:
#include <iostream>
#include <vector>
#include <algorithm>
Expand Down Expand Up @@ -48,47 +28,4 @@ int main() {
for(int i = 1; i <= maxdepth; i++)
printf(" %d", book[i]);
return 0;
}


bfs:

#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
int level[100], book[100], maxlevel = -1;
vector<int> v[100];
void bfs() {
queue<int> q;
q.push(1);
level[1] = 0;
while(!q.empty()) {
int index = q.front();
q.pop();
maxlevel = max(level[index], maxlevel);
if(v[index].size() == 0)
book[level[index]]++;
for(int i = 0; i < v[index].size(); i++) {
q.push(v[index][i]);
level[v[index][i]] = level[index] + 1;
}
}
}
int main() {
int n, m, k, node, c;
scanf("%d %d", &n, &m);
for(int i = 0; i < m; i++) {
scanf("%d %d",&node, &k);
for(int j = 0; j < k; j++) {
scanf("%d", &c);
v[node].push_back(c);
}
}
bfs();
printf("%d", book[0]);
for(int i = 1; i <= maxlevel; i++)
printf(" %d", book[i]);
return 0;
}
40 changes: 6 additions & 34 deletions AdvancedLevel_C++/1005. Spell It Right (20).cpp
Original file line number Diff line number Diff line change
@@ -1,43 +1,15 @@
1005. Spell It Right (20)
Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.

Input Specification:

Each input file contains one test case. Each case occupies one line which contains an N (<= 10100).

Output Specification:

For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.

Sample Input:
12345
Sample Output:
one five

#include <iostream>
#include <vector>
using namespace std;
int main() {
string s[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
string a;
cin >> a;
int sum = 0;
for(int i = 0; i < a.length(); i++) {
for (int i = 0; i < a.length(); i++)
sum += (a[i] - '0');
}
vector<int> v;
while(sum != 0) {
v.push_back(sum % 10);
sum = sum / 10;
}
int len = v.size();
if(len == 0) { // if sum == 0 print zero else print s[v[len - 1]] 否则会段错误
cout << s[0];
} else {
cout << s[v[len - 1]];
}
for(int i = len - 2; i >= 0; i--) {
cout << " " << s[v[i]];
}
string s = to_string(sum);
string arr[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
cout << arr[s[0] - '0'];
for (int i = 1; i < s.length(); i++)
cout << " " << arr[s[1] - '0'];
return 0;
}
31 changes: 1 addition & 30 deletions AdvancedLevel_C++/1006. Sign In and Sign Out (25).cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,5 @@
1006. Sign In and Sign Out (25)
At the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock the door. Given the records of signing in's and out's, you are supposed to find the ones who have unlocked and locked the door on that day.

Input Specification:

Each input file contains one test case. Each case contains the records for one day. The case starts with a positive integer M, which is the total number of records, followed by M lines, each in the format:

ID_number Sign_in_time Sign_out_time
where times are given in the format HH:MM:SS, and ID number is a string with no more than 15 characters.

Output Specification:

For each test case, output in one line the ID numbers of the persons who have unlocked and locked the door on that day. The two ID numbers must be separated by one space.

Note: It is guaranteed that the records are consistent. That is, the sign in time must be earlier than the sign out time for each person, and there are no two persons sign in or out at the same moment.

Sample Input:
3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40
Sample Output:
SC3021234 CS301133

题目大意:给出n个人的id、sign in时间、sign out时间,求最早进来的人和最早出去的人的ID~

分析:将时间都转换为总秒数,最早和最迟的时间保存在变量minn和maxn中,并同时保存当前最早和最迟的人的ID,最后输出~

#include <iostream>
#include <cstdio>
#include <limits.h>
#include <climits>
using namespace std;
int main() {
int n, minn = INT_MAX, maxn = INT_MIN;
Expand Down
49 changes: 11 additions & 38 deletions AdvancedLevel_C++/1007. Maximum Subsequence Sum (25).cpp
Original file line number Diff line number Diff line change
@@ -1,51 +1,24 @@
1007. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (<= 10000). The second line contains K numbers, separated by a space.

Output Specification:

For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

Sample Input:
10
-10 1 2 3 4 -5 -23 3 7 -21
Sample Output:
10 1 4
题目大意:求最大连续子序列和,输出最大的和以及这个子序列的开始值和结束值。如果所有数都小于0,那么认为最大的和为0,并且输出首尾元素。
分析:sum为要求的最大和,temp为临时最大和,left和right为所求的子序列的下标,tempindex标记left的临时下标。
temp = temp + v[i],当temp比sum大,就更新sum的值、left和right的值;当temp < 0,那么后面不管来什么值,都应该舍弃temp < 0前面的内容,因为负数对于总和只可能拉低总和,不可能增加总和,还不如舍弃;
舍弃后,直接令temp = 0,并且同时更新left的临时值tempindex。因为对于所有的值都为负数的情况要输出0,第一个值,最后一个值,所以在输入的时候用flag判断是不是所有的数字都是小于0的,如果是,要在输入的时候特殊处理。~~~~


#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, flag = 0, sum = -1, temp = 0, left = 0, right = 0, tempindex = 0;
int n;
scanf("%d", &n);
vector<int> v(n);
for(int i = 0; i < n; i++) {
int leftindex = 0, rightindex = n - 1, sum = -1, temp = 0, tempindex = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &v[i]);
if(v[i] >= 0)
flag = 1;
temp = temp + v[i];
if(temp > sum) {
sum = temp;
left = tempindex;
right = i;
} else if(temp < 0) {
if (temp < 0) {
temp = 0;
tempindex = i + 1;
} else if (temp > sum) {
sum = temp;
leftindex = tempindex;
rightindex = i;
}
}
if(flag == 0)
printf("0 %d %d", v[0], v[n - 1]);
else
printf("%d %d %d", sum, v[left], v[right]);
if (sum < 0) sum = 0;
printf("%d %d %d", sum, v[leftindex], v[rightindex]);
return 0;
}
Loading

0 comments on commit 3f94a4d

Please sign in to comment.