PAT_A_01

  1. 1001 A+B Format
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 Specification:
Each input file contains one test case. Each case contains a pair of integers a and b where −10 
6
 ≤a,b≤10 
6
 . The numbers are separated by a space.

Output Specification:
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 <bits/stdc++.h>
using namespace std;
void print(int num){
	if(num < 0) cout << "-";
	num = abs(num);
	char res[20];
	int l = 0, cnt = 0;
	while(1){
		char c = num%10 + '0';
		num /= 10;
		res[l] = c;
		l++, cnt++;
		if(cnt >= 3) {
			res[l] = ',';
			cnt = 0;
			l++;
		}
		if(num == 0) break;
	}
	for(int i=l-1;i>=0;i--){
		if(res[i] != ',') {
			for(int j=i;j>=0;j--){
				cout << res[j];
			}
			break;
		}
	}
}
int main()
{
	int a, b;
	while(cin >> a >> b){
		int c = a + b;
		print(c);
		cout << "\n";
	} 
    return 0;
}
  1. 1002 A+B for Polynomials
This time, you are supposed to find A+B where A and B are two polynomials.

Input Specification:
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:


where K is the number of nonzero terms in the polynomial, N  (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10,0≤N 
Output Specification:
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 <bits/stdc++.h>
using namespace std;
const int N = 1010;
double nums[N] = {0};
double res[N] = {0};
int main()
{
	int k, a;
	double b;
	cin >> k;
	for(int i=0;i<k;i++){
		cin >> a >> b;
		nums[a] += b;
 	}
 	cin >> k;
 	for(int i=0;i<k;i++){
 		cin >> a >> b;
 		nums[a] += b;
	}
	int flag = 0;
	for(int i=N-1;i>=0;i--){
		if(nums[i] != 0){
			flag ++;
		}
	}
	cout << flag;
	for(int i=N-1;i>=0;i--){
		if(nums[i] != 0){
			printf(" %d %.1lf",i, nums[i]);
		}
	}
	return 0;
} 
  1. 1003 Emergency
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 Specification:
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, C 
1
​
  and C 
2
​
  - 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 c 
1
​
 , c 
2
​
  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 C 
1
​
  to C 
2
​
 .

Output Specification:
For each test case, print in one line two numbers: the number of different shortest paths between C 
1
​
  and C 
2
​
 , 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
个人题解
#include <bits/stdc++.h>
using namespace std;
const int N = 510;
int flag[N] = {0};
struct edge
{
    int from, to, w;
    edge(int f, int t, int ww):
        from(f), to(t), w(ww)
    {}
};
vector<edge> maps[N];
int nums[N];
int res = 0, res1 = INT_MAX, res2 = 0, t1, t2;
bool dfs(int u, int dist, int worth)  
{
	if(u == t2){
		//cout << dist << "\n";
		if(dist  < res1){
			res = 1;
			res1 = dist;
			res2 = worth;	
		}
		else if(dist == res1){
			res ++;
			if(worth > res2){
				res2 = worth;
			}
		}
		return true;
	}
	if(maps[u].empty()) return true;  
    for(int i=0;i<maps[u].size();i++){
    	int v = maps[u][i].to;
    	if(flag[v] == 0){
    		int d = maps[u][i].w;
    		flag[v] = 1; 
    		dfs(v, dist + d, worth + nums[v]);
    		flag[v] = 0;
    	}
    }
    return true;

}
int main()
{
	int n, m;
	cin >> n >> m >> t1 >> t2;
	for(int i=0;i<n;i++){
		cin >> nums[i];
	}
	int a, b, w;
	for(int i=0;i<m;i++){
		cin >> a >> b >> w;
		maps[a].push_back(edge(a, b, w));
		maps[b].push_back(edge(b, a, w));
	}
	flag[t1] = 1;
	dfs(t1, 0, nums[t1]);
	cout << res << " " << res2;
	return 0;
}
/*
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

2 4

邻接表 + dfs 
*/

  1. 1004 Counting Leaves
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.

Input Specification:
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.

The input ends with N being 0. That case must NOT be processed.

Output Specification:
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
个人题解
#include <bits/stdc++.h>
using namespace std;
struct pairs
{
	int v, w;
	pairs(int _v, int _w):
		v(_v), w(_w)
	{}	
};
const int N = 110;
int res[N] = {0};
int nn = 1;
void bfs(vector<int> map[], int u)
{
	queue<struct pairs> q;
	q.push(pairs(u, 0));
	nn = 0;
	while(!q.empty()){
		//int x = q.front();
		struct pairs x = q.front();
		if(map[x.v].size() == 0)
			res[x.w] ++;
		q.pop();
		for(int i=0;i<map[x.v].size();i++){
			q.push(pairs(map[x.v][i], x.w + 1));
			nn = x.w + 1;
		}
	}
}

int main()
{
	int n, m;
	while(cin >> n >> m){
		int a, k, b;
		vector<int> maps[N];
		for(int i=0;i<N;i++) res[i] = 0; 
		for(int i=0;i<m;i++){
			cin >> a >> k;
			for(int j=0;j<k;j++){
				cin >> b;
				maps[a].push_back(b);
			}
		}
		bfs(maps, 1);
		cout << res[0];
		for(int i=1;i<=nn;i++){
			cout << " " << res[i];
		}
		cout << "\n";
	}
	return 0;
}

//bfs

posted @ 2023-05-11 21:05  小麟qwq  阅读(16)  评论(0)    收藏  举报