pat-advanced(1001-1004)

来源:互联网 发布:htc d820u数据连接 编辑:程序博客网 时间:2024/04/29 13:04

1001. A+B Format (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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>#include <string>#include <vector>using namespace std;int main(){int a, b;int res;string temp;cin >> a >> b;res = a + b;if(res == 0){cout << "0"<< endl;return 0;}int flag = 0;if(res < 0){flag = 1;res = -res;}int len = 0;while(res){temp.insert(temp.begin(), res%10+'0');res /= 10;len++;if(len % 3 == 0 && res > 0){temp.insert(temp.begin(), ',');}}if(flag)cout<< "-";cout<< temp<< endl;return 0;}



1002. A+B for Polynomials (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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.22 2 1.5 1 0.5
Sample Output
3 2 1.5 1 2.9 0 3.2
#include <iostream>#include <vector>#include <cstdio>#include <algorithm>#include <cmath>using namespace std;struct node {int a;double n;};int main(){int k1, k2;vector<node> l1, l2;int i, j;cin>> k1;for(i = 0; i <k1; i++){node temp;cin >>temp.a >> temp.n;l1.push_back(temp);}cin>> k2;for(i = 0; i <k2; i++){node temp;cin >>temp.a >> temp.n;l2.push_back(temp);}vector<node> res;i = j = 0;while(i< l1.size() && j < l2.size()){node temp;if(l1[i].a > l2[j].a){temp.a = l1[i].a;temp.n = l1[i].n;i++;}else if(l1[i].a < l2[j].a){temp.a = l2[j].a;temp.n = l2[j].n;j++;}else{temp.a = l1[i].a;temp.n = l1[i].n + l2[j].n;i++; j++;}if(abs(temp.n - 0) < 0.1)//第二个case的注意点continue;res.push_back(temp);}while(i < l1.size())res.push_back(l1[i++]);while(j < l2.size())res.push_back(l2[j++]);if(res.size() > 0){printf("%d", res.size());for(i = 0;  i < res.size(); i++){printf(" %d %.1lf", res[i].a, res[i].n);}printf("\n");}elseprintf("0\n");return 0;}


1003. Emergency (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 21 2 1 5 30 1 10 2 20 3 11 2 12 4 13 4 1
Sample Output
2 4
#include <iostream>#include <vector>#include <climits>using namespace std;struct city {city():reached(0), nr_team(0){};int reached;int  nr_team;};int N, M, C1, C2;int min_l = INT_MAX , max_t = 0;int path = 0;void rescue(vector<city>& cities, vector<vector<int>>& matrix, int &dis, int &nr_t, int x){int i;for(i = 0; i < cities.size(); i++){if(matrix[i][x] > 0 && cities[i].reached == 0){dis += matrix[i][x];nr_t += cities[i].nr_team;cities[i].reached = 1;if(i == C2){if(dis < min_l){min_l = dis;path = 1;max_t = nr_t;}else if(dis == min_l){max_t = max(max_t, nr_t);path++;}}else{rescue(cities, matrix, dis, nr_t, i);}dis -= matrix[i][x];nr_t -= cities[i].nr_team;cities[i].reached = 0;}}}int main(){vector<city> cities;cin>> N >> M>> C1>> C2;int i;for(i = 0; i < N; i++){city temp;cin >> temp.nr_team;cities.push_back(temp);}if(C1 == C2)//重要的边界条件{cout<<"1 "<<cities[C1].nr_team<< endl;return 0;}vector<vector<int>> matrix(N, vector<int>(N, 0));for(i = 0; i < M; i++){int x, y, l;cin >> x>> y >>l;matrix[x][y] = l;matrix[y][x] = l;}int dis = 0, nr_t = cities[C1].nr_team;cities[C1].reached = 1;rescue(cities, matrix, dis, nr_t, C1);cout<< path << " "<< max_t<< endl;return 0;}


1004. Counting Leaves (30)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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 101 1 02
Sample Output
0 1
#include <iostream>#include <vector>#include <string>#include <queue>using namespace std;int N, M;struct node {node():id(-1){};int id;vector<int> children;};int main(){cin >> N>> M;vector<node> nodes(100);int i;for(i = 0; i < M; i++){int id, k;cin >> id>> k;nodes[id].id = id;for(int j = 0; j < k; j++){int t;cin >> t;nodes[t].id = t;nodes[id].children.push_back(t);}}int flag = 0;queue<int> q;q.push(1);while(!q.empty()){int num = q.size();int leaves = 0;for(i = 0; i < num; i++){node temp = nodes[q.front()];q.pop();if(temp.children.empty())leaves++;else{for(int j =0; j < temp.children.size(); j++){q.push(temp.children[j]);}}}if(!flag){cout<< leaves;flag = 1;}elsecout<<  " "<< leaves;}cout<< endl;return 0;}





0 0