pat 1001 - 1004

本文解析了四个经典算法题目,包括基本数值计算、多项式加法、图论问题及树结构处理,涵盖数据结构与算法设计的核心技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

AC时候需要注意的细节:
我是这么去考虑的,这道题实际上就是在于处理1,000中的,的打印问题,首先进行正常的加法运算,然后对结果进行判断,判断结果是 > 0 还是 < 0, < 0 的我们首先打印出一个 - 符号,然后去考虑数字中的 , 号打印问题。这时候我们对结果去分析,由于-1000000 <= a, b <= 1000000,我们可以知道 >= 1000000 我们需要在哪个位置中打印出,号,我们了解到在c/1000000和c/1000%1000之后分别打印出 , 号。此外还需要注意的是在中间位置数据的打印,可能计算得出的结果是 0xx, 00x, 000, 我们需要保证中间位置打印3位,故%3d,同理。
#include<stdio.h>

int main()
{
	freopen("E://input.txt", "r", stdin);
	int a, b;
	scanf("%d %d", &a, &b);
	
	int ans = a + b;
	
	if(ans < 0)
	{
		ans = -ans;
		printf("-");
	}
	
	if(ans >= 1000000)
		printf("%d,%03d,%03d\n", ans / 1000000, ans / 1000 % 1000, ans % 1000);
	else if(ans >= 1000)
		printf("%d,%03d\n", ans/1000, ans % 1000);
	else
		printf("%d\n", ans);
		
	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.2
2 2 1.5 1 0.5
Sample Output
3 2 1.5 1 2.9 0 3.2
题目内容描述的是两个多项式的加法,原则就是对应指数的系数相加即可,但是从结果看,还需要输出该多项式总共有多少项,其实实质的意思,就是两个数组的加法,所以我们选择两个double类型数组str1和str2,分别保存两组的输入,然后进行对应项相加,也就是str2[i] = str1[i] + str1[i],结果保存在str2中,其中我们需要判断str2中有多少项非0,统计个数。

#include<stdio.h>
#include<string.h>
using namespace std;

double str1[1002], str2[1002];

int main()
{
	freopen("E://input.txt", "r", stdin);
	int n, count = 0;
	memset(str1, 0, sizeof(str1));
	memset(str2, 0, sizeof(str2));
	
	scanf("%d", &n);
	
	for(int i = 0; i < n; i ++)
	{
		int tmp;
		scanf("%d", &tmp);
		scanf("%lf", &str1[tmp]);	
	}
	
	scanf("%d", &n);
	
	for(int i = 0; i < n; i ++)
	{
		int tmp;
		scanf("%d", &tmp);
		scanf("%lf", &str2[tmp]);
	}
	
	for(int i = 0; i < 1002; i ++)
	{
		str2[i] += str1[i];
		if(str2[i] != 0)
			count ++;
	}
	
	printf("%d", count);
	
	for(int i = 1001; i >= 0; i --)
	{
		if(str2[i] != 0)
			printf(" %d %.1f", i, str2[i]);
	}
	
	printf("\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 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
题目的意思就是一张图,各边都有权值而且个点也有权值,求出给定两点之间最短路的条数并输出此路径上的点权值和的最大值。就是求出最短路的条数,若有多条相同的最短路输出最大的那个点权值和。很简单的题目,一个dfs就可以很好的解决掉。当然这个题目还可以继续增加点复杂度,就是把各条最短路的路径打印出来,这个也是很好实现的,加个pre数组,然后可以再来个dfs,当然也可以先来遍迪杰斯特拉。下面我们采用这两种方法来做,代码一:迪杰斯特克拉,代码而:dfs,当然用bfs也是可以做的,你也可以自己试一试。
代码一:迪杰斯特拉
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>

int path[502][502];
int d[502];
int exist[502];
int rescue[502];
int cnt[502], r[502];

int n, m, s, e;

int max(int a, int b)
{
	return a > b ? a : b;
}

int nearest()
{
	int max = INT_MAX;
	int index;
	
	for(int i = 0; i < n; i ++)
	{
		if(!exist[i] && d[i] < max)
		{
			max = d[i];
			index = i;
		}
	}
	return index;
}

void shortest()
{
	exist[s] = 1;
	
	for(int i = 0; i < n; i ++)
	{
		d[i] = path[s][i];
		cnt[i] = (d[i] == INT_MAX) ? 0 : 1;
		r[i] = (d[i] == INT_MAX) ? INT_MIN : rescue[i] + rescue[s];
	}
	
	r[s] = rescue[s];
	d[s] = 0;
	cnt[s] = 1;
	
	for(int i = 0; i < n-1; i ++)
	{
		int index = nearest();
		exist[index] = 1;
		
		for(int j = 0; j < n; j ++)
		{
			if(exist[j] || path[index][j] == INT_MAX)
				continue;
			
			if(d[index] + path[index][j] < d[j])
			{
				d[j] = d[index] + path[index][j];
				cnt[j] = cnt[index];
				r[j] =r[index] + rescue[j];
			} 
			else if(d[index] + path[index][j] == d[j])
			{
				cnt[j] += cnt[index];
				r[j] = max(r[index]+rescue[j], r[j]);
			}
		}
	}
	printf("%d %d\n", cnt[e], r[e]);
}

int main()
{
	freopen("E://input.txt", "r", stdin);

	
	scanf("%d%d%d%d", &n, &m, &s, &e);
	
	for(int i = 0; i < n; i ++)
	{
		for(int j = 0; j < n; j ++)
			path[i][j] = INT_MAX;
	}
	
	for(int i = 0; i < n; i ++)
		scanf("%d", &rescue[i]);
	
	for(int i = 0; i < m; i ++)
	{
		int a, b;
		scanf("%d%d", &a, &b);
		scanf("%d", &path[a][b]);
		path[b][a] = path[a][b];
	}
	
	shortest();
	return 0;
}

代码二:dfs
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>

int rescue[502], exist[502], map[502][502];

int mind, cnt, maxt, n;//cnt记录有多少条最短路径,maxt记录多条相同的最短路中最大的那个各城市的救援队数量 

void init(int n)
{
	for(int i = 0; i < n; i ++)
	{
		exist[i] = 0;
		for(int j = 0; j < n; j ++)
			map[i][j] = INT_MAX;
	}
}


void dfs(int p, int end, int dist, int numt)
{
	if(p == end)
	{
		if(dist < mind)
		{
			cnt = 1;
			mind = dist;
			maxt = numt;
		}
		else if(dist == mind)
		{
			cnt ++;
			if(maxt < numt)
				maxt = numt;
		}
		
		return ;
	}
	
	if(dist > mind)
		return ;//这个地方不剪枝的话最后一个case过不去的
	
	for(int i = 0; i < n; i ++)
	{
		if(exist[i] == 0 && map[p][i] != INT_MAX)
		{
			exist[i] = 1;
			dfs(i, end, dist+map[p][i], numt+rescue[i]);
			exist[i] = 0;
		}
	} 
}

int main()
{
	freopen("E://input.txt", "r", stdin);
	int m, st, end, x, y, d;
	mind = INT_MAX;
	cnt = 0;
		
	scanf("%d%d%d%d", &n, &m, &st, &end);
	init(n);
	for(int i = 0; i < n; i ++)
		scanf("%d", &rescue[i]);
			
	while(m --)
	{
		scanf("%d%d%d", &x, &y, &d);
		if(map[x][y] > d)
			map[x][y] = map[y][x] = d;
	}
		
	dfs(st, end, 0, rescue[st]);
	printf("%d %d\n", cnt, maxt);
		
	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 1
01 1 02
Sample Output
0 1

题目的意思是让我们建立一颗树,我们明白题目的重点在于某节点是否有孩子以及是上一层哪个节点的孩子。题目让我们输出的答案是每一层没有孩子节点的个数,我们首先需要建立这棵树,然后知道这棵树有多少层,再然后如何去保存上下层孩子节点的关系,然后从上往下扫描并进行输出!此题不难。基础题
#include<stdio.h>
using namespace std;

struct Node
{
	int parent;
	int level;
	bool has_child;//
}buf[102];

int max_level;//最大深度
int count[102];//保存每层的叶子节点数

int find_level(int i)
{
	if(buf[buf[i].parent].level == 0)
		return buf[i].level = find_level(buf[i].parent) + 1;
	else
		return buf[i].level = buf[buf[i].parent].level + 1;
}

int main()
{
	freopen("E://input.txt", "r", stdin);
	int n, m;
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= n; i ++)
	{
		buf[i].parent = 0;
		buf[i].level = 0;
		buf[i].has_child = false;
		count[i] = 0;
	}
	
	for(int i = 1; i <= m; i ++)
	{
		int k, a, b;
		scanf("%d%d", &a, &k);
		buf[a].has_child = true;
		for(int j = 1; j <= k; j ++)
		{
			scanf("%d", &b);
			buf[b].parent = a;
		}
	}
	
	buf[1].level = 1;
	
	for(int i = 2; i <= n; i ++)
		find_level(i);
	
	for(int i = 1; i <= n; i ++)
	{
		if(max_level < buf[i].level)
			max_level = buf[i].level;
	}//找到最大的深度
	
	//找到各层中的叶子节点数 
	for(int i = 1; i <= n; i ++)
		if(buf[i].has_child == false)
			count[buf[i].level] ++; 
	
	
	for(int i = 1; i <= max_level; i ++)
	{
		if(i == max_level)
			printf("%d", count[i]);
		else
			printf("%d ", count[i]);
	} 

	return 0;
} 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值