The basic task is simple: given N real numbers, you are supposed to calculate their average. But what makes it complicated is that some of the input numbers might not be legal. A legal input is a real number in $[−1000,1000]$ and is accurate up to no more than 2 decimal places. When you calculate the average, those illegal numbers must not be counted in.
intmain(){ int n, cnt = 0; char a[50], b[50]; double temp = 0.0, sum = 0.0; cin >> n; for(int i = 0; i < n; i++) { scanf("%s", a); sscanf(a, "%lf", &temp); sprintf(b, "%.2f",temp); int flag = 0; for(int j = 0; j < strlen(a); j++) if(a[j] != b[j]) flag = 1; if(flag || temp < -1000 || temp > 1000) { printf("ERROR: %s is not a legal number\n", a); continue; } else { sum += temp; cnt++; } } if(cnt == 1) printf("The average of 1 number is %.2f", sum); elseif(cnt > 1) printf("The average of %d numbers is %.2f", cnt, sum / cnt); else printf("The average of 0 numbers is Undefined"); return0; }
[1109] Group Photo (25)
数学模拟
题目
Formation is very important when taking a group photo. Given the rules of forming K rows with N people as the following:
The number of people in each row must be $N/K$ (round down to the nearest integer), with all the extra people (if any) standing in the last row;
All the people in the rear row must be no shorter than anyone standing in the front rows;
In each row, the tallest one stands at the central position (which is defined to be the position $(m/2+1)$, where $m$ is the total number of people in that row, and the division result must be rounded down to the nearest integer);
In each row, other people must enter the row in non-increasing order of their heights, alternately taking their positions first to the right and then to the left of the tallest one (For example, given five people with their heights 190, 188, 186, 175, and 170, the final formation would be 175, 188, 190, 186, and 170. Here we assume that you are facing the group so your left-hand side is the right-hand side of the one at the central position.);
When there are many people having the same height, they must be ordered in alphabetical (increasing) order of their names, and it is guaranteed that there is no duplication of names.
Now given the information of a group of people, you are supposed to write a program to output their formation.
Input Specification:
Each input file contains one test case. For each test case, the first line contains two positive integers $N (≤10^4)$, the total number of people, and $K (≤10)$, the total number of rows. Then N lines follow, each gives the name of a person (no more than 8 English letters without space) and his/her height (an integer in $[30, 300]$).
Output Specification:
For each case, print the formation – that is, print the names of people in $K$ lines. The names must be separated by exactly one space, but there must be no extra space at the end of each line. Note: since you are facing the group, people in the rear rows must be printed above the people in the front rows.
int cnt = 0; for (int row = 0; row < k; row++) { if (!row) m = n - n / k * (k - 1); else m = n / k; vector<string> col(m); col[m/2] = vec[cnt++].name;
int k = 1; for (int i = 1; k < m; i++) { if (m / 2 - i >= 0) { col[m/2-i] = vec[cnt++].name; k++; } if (m / 2 + i < m) { col[m/2+i] = vec[cnt++].name; k++; } } }
sort(vec.begin(), vec.end(), [&](Person a, Person b) { return a.height == b.height? a.name < b.name: a.height > b.height; });
int cnt = 0; for (int i = 0; i < k; i++) { if (!i) m = n - n / k * (k-1); else m = n / k; vector<string> columns(m); // 中间位置是m/2+1,数组下标就是m/2 columns[m/2] = vec[cnt].name;
int j = m / 2 - 1; for (int k = cnt + 1; k < cnt + m; k += 2) columns[j--] = vec[k].name; j = m / 2 + 1; for (int k = cnt + 2; k < cnt + m; k += 2) columns[j++] = vec[k].name; }
[1110] Complete Binary Tree (25)
完全二叉树
题目
Given a tree, you are supposed to tell if it is a complete binary tree.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer $N (≤20)$ which is the total number of nodes in the tree – and hence the nodes are numbered from $0$ to $N−1$. Then $N$ lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a - will be put at the position. Any pair of children are separated by a space.
Output Specification:
For each case, print in one line YES and the index of the last node if the tree is a complete binary tree, or NO and the index of the root if not. There must be exactly one space separating the word and the number.
注意点与解析
被一个很弱智的点给卡住了,输入的N大小不大于20,所以每个结点的左右结点有可能是11、16这样的两位数,变量不能开char要开string(因为经常用 a - '0' 这样的写法所以形成了惯性思维开成了char),转换的时候用 stoi() 就行;
structNode { int left, right; }; vector<Node> node; vector<bool> isRoot; int ans, maxn = -1;
voiddfs(int root, int idx){ if (idx > maxn) { // maxn用来记录当前位置下标,等会用于比较是否大于结点个数来判断是否是完全二叉树 // ans用来记录最后一个最后一个结点的值 // idx > maxn的条件是用来防止覆盖的 maxn = idx; ans = root; } if (node[root].left != -1) dfs(node[root].left, 2 * idx); if (node[root].right != -1) dfs(node[root].right, 2 * idx + 1); return; }
intmain(){ int n; cin >> n; node.resize(n); isRoot.resize(n, true); for (int i = 0; i < n; i++) { string a, b; cin >> a >> b; if (a == "-") node[i].left = -1; else { node[i].left = stoi(a); isRoot[stoi(a)] = false; } if (b == "-") node[i].right = -1; else { node[i].right = stoi(b); isRoot[stoi(b)] = false; } } int root = 0; while (!isRoot[root]) root++; dfs(root, 1); if (maxn == n) cout << "YES " << ans; else cout << "NO " << root; return0; }
[1111] Online Map (30)
题目
Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is guaranteed that a path exists for any request.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers $N (2≤N≤500)$, and $M$, being the total number of streets intersections on a map, and the number of streets, respectively. Then M lines follow, each describes a street in the format:
1
V1 V2 one-way length time
where V1 and V2 are the indices (from 0 to $N−1$) of the two ends of the street; one-way is 1 if the street is one-way from V1 to V2, or 0 if not; length is the length of the street; and time is the time taken to pass the street.
Finally a pair of source and destination is given.
Output Specification:
For each case, first print the shortest path from the source to the destination with distance D in the format:
1
Distance = D: source -> v1 -> ... -> destination
Then in the next line print the fastest path with total time T:
1
Time = T: source -> w1 -> ... -> destination
In case the shortest path is not unique, output the fastest one among the shortest paths, which is guaranteed to be unique. In case the fastest path is not unique, output the one that passes through the fewest intersections, which is guaranteed to be unique.
In case the shortest and the fastest paths are identical, print them in one line in the format: