CCF 2015-03

CCF 历年题目集

A. 图像旋转

按照题目写就行

#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int a[N][N];
int main()
{
	int n,m;
	cin >> n >> m;
	for (int i = 0 ; i < n ; i ++ )
		for (int j = 0 ; j < m ; j ++ )
			cin >> a[i][j];
		
	for (int j = m-1 ; j >= 0 ; j -- )
	{
		for (int i = 0 ; i < n ; i ++ )
		{
			cout << a[i][j] << ' ';
		}
		cout << endl;
	}
	return 0;
}

B. 数字排序

这个随便怎么搞都行,这里我用的结构体的做的

#include <bits/stdc++.h>
using namespace std;
map<int,int> mp;
struct node
{
	int a,b; // a 是数,b 是次数
}st[1005];
int main()
{
	int n;
	cin >> n;

	int x;
	int c=1;
	for (int i = 0 ; i < n ; i ++ )
	{
		cin >> x;
		if(mp[x] != 0) st[mp[x]].b ++ ;
		else
		{	
			mp[x] = c;
			st[c] = {x,1};
			c ++ ;
		}
	}
	sort(st+1,st+1+n,[&](node &a,node &b){
		if(a.b != b.b) return a.b > b.b;
		else return a.a < b.a;
	});
	for (int i = 1 ; i < c ; i ++ ) cout << st[i].a << ' ' << st[i].b << endl;
	return 0;
}

C. 节日

这里将总天数求出来,然后按照规则求解

#include <bits/stdc++.h>
using namespace std;
int month[] = {31,28,31,30,31,30,31,31,30,31,30,31};
int main()
{
	int day=0;
	int a,b,c,y1,y2;
	cin >> a >> b >> c >> y1 >> y2;
	for (int i = 1850 ; i <= y2 ; i ++ )
	{
		if(i % 400 == 0 || (i % 4 == 0 && i % 100 != 0)) month[1] = 29;
		else month[1] = 28;
		for (int j = 1 ; j <= 12 ; j ++ )
		{
			if(i >= y1 && j == a)
			{
				int w = (1+day) % 7;
				int cnt = 0;
				for (int d = 1 ; d <= month[j-1] ; d ++ )
				{
					if(w == c - 1) cnt ++ ;
					if(cnt == b) 
					{
						printf("%04d/%02d/%02d\n",i,j,d);
						break;
					}
					w = (w + 1) % 7;
				}
				if(cnt < b) puts("none");
			}
			day += month[j-1];
		}
	}
	return 0;
}

D. 网络延时

该问题可以抽象为求树的直径,注意不能按照题目的编号,因为电脑和交换机编号会重复

#include <bits/stdc++.h>
using namespace std;
const int N = 10010 * 2;

struct Edge
{
	int id,w;
};

vector<Edge> h[N];
int dist[N];

void dfs(int u,int father,int distance)
{
	dist[u] = distance;
	for (int i = 0 ; i < h[u].size() ; i ++ )
	{
		if(h[u][i].id != father)
			dfs(h[u][i].id,u,distance + h[u][i].w);
	}
	return ;
}	

int main()
{
	int n,m;
	cin >> n >> m;

	int a;
	for (int i = 0 ; i < n-1 ; i ++ )
	{
		cin >> a;
		h[i+2].push_back({a,1});
		h[a].push_back({i+2,1});
	}
	for (int i = 1 ; i <= m ; i ++ )
	{
		cin >> a;
		h[i+n].push_back({a,1});
		h[a].push_back({i+n,1});
	}
	dfs(1,-1,0);
	int u = 1;
	for (int i = 1 ; i <= n+m ; i ++ )
		if(dist[u] < dist[i])
			u = i;
	
	dfs(u,-1,0);
	u = 0;
	for (int i = 1 ; i <= n+m ; i ++ )
		if(dist[u] < dist[i])
			u = i;
	
	cout << dist[u] << endl;
	return 0;
}

E. 最小花费

暂时不会~

上一篇:12月补题记录


下一篇:3.2vector容器