CCF 2015-12


CCF 历年题目集

A. 数位之和

#include 
using namespace std;
int main()
{
	string s;
	cin >> s;

	int res = 0;
	for (unsigned i = 0 ; i < s.size() ; i ++ )
	{
		res += s[i] - '0';
	}
	cout << res << endl;
	return 0;
}

B. 消除类游戏

遍历每一个位置,判断该位置是否在上下或者左右有连着三个相同颜色的棋子,如果有则标记,最后消除被标记的棋子

#include 
using namespace std;
const int N = 50;

int n,m;
int a[N][N];
bool vis[N][N];

int main()
{
	cin >> n >> m;
	for (int i = 1 ; i <= n ; i ++ )
		for (int j = 1 ; j <= m ; j ++ )
			cin >> a[i][j];

	for (int i = 1 ; i <= n ; i ++ )
	{
		for (int j = 1 ; j <= m ; j ++ )
		{
			if(a[i][j-1] == a[i][j] && a[i][j] == a[i][j+1])
			{
				vis[i][j-1] = vis[i][j] = vis[i][j+1] = true;
			}
			if(a[i-1][j] == a[i][j] && a[i][j] == a[i+1][j])
			{
				vis[i-1][j] = vis[i][j] = vis[i+1][j] = true;
			}
		}
	}
			

	for (int i = 1 ; i <= n ; i ++ )
	{
		for (int j = 1 ; j <= m ; j ++ )
		{
			if(!vis[i][j]) cout << a[i][j] << ' ';
			else cout << 0 << ' ';
		}
		cout << endl;
	}
	return 0;
}

C. 画图

按照题目模拟,需要画线的直接画,填充则用 dfs 进行填充

#include 
using namespace std;
const int N = 110;

int m,n,k;
char c[N][N];
int dx[] = {0,0,1,-1},dy[] = {1,-1,0,0};

void dfs(int x,int y,char ch)
{
	c[x][y] = ch;

	for (int i = 0 ; i < 4 ; i ++ )
	{
		int a = x + dx[i] , b = y + dy[i];
		if(c[a][b] == '-' || c[a][b] == '|' || c[a][b] == '+' || c[a][b] == ch) continue;
		if(a < 0 || a >= n || b < 0 || b >= m) continue;
		dfs(a,b,ch);
	}
	return ;
}

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

	for (int i = 0 ; i < n ; i ++ )
		for (int j = 0 ; j < m ; j ++ )
			c[i][j] = '.';

	while(k -- )
	{
		char ch;
		int type;
		int x1,y1,x2,y2;

		cin >> type;
		if(type == 1) 
		{
			cin >> y1 >> x1 >> ch;
			x1 = n - x1 - 1;
			dfs(x1,y1,ch);
		}
		else 
		{
			cin >> y1 >> x1 >> y2 >> x2;
			x1 = n - x1 - 1;
			x2 = n - x2 - 1;
			if(x1 > x2) swap(x1,x2);
			if(y1 > y2) swap(y1,y2);
			if(x1 == x2)
			{
				for (int i = y1 ; i <= y2 ; i ++ )
				{
					if(c[x1][i] == '|') c[x1][i] = '+';
					else if(c[x1][i] == '+') continue;
					else c[x1][i] = '-';
				}
			}
			else if(y1 == y2)
			{
				for (int i = x1 ; i <= x2 ; i ++ )
				{
					if(c[i][y1] == '-') c[i][y1] = '+';
					else if(c[i][y1] == '+') continue;
					else c[i][y1] = '|';
				}
			}
		}
	}
	for (int i = 0 ; i < n ; i ++ )
	{
		for (int j = 0 ; j < m ; j ++ )
		{
			cout << c[i][j];
		}
		cout << endl;
	}
	return 0;
}

D. 送货

题目可以概括为,是否可以不重不漏的经过所有路径并且经过所有点,即欧拉路径的模板题

#include 
using namespace std;
const int N = 10010 , M = 100010;

int n,m;
set g[N];
int p[N];
int ans[M],top;

int find(int x)
{
	if(p[x] != x) p[x] = find(p[x]);
	return p[x];
}

void dfs(int u)
{
	while(g[u].size())
	{
		int t = *g[u].begin();
		g[u].erase(t),g[t].erase(u);
		dfs(t);
	}
	ans[++ top] = u;
}

int main()
{
	cin >> n >> m;
	for (int i = 1 ; i <= n ; i ++ ) p[i] = i;
	while(m -- )
	{
		int a,b;
		cin >> a >> b;
		g[a].insert(b);
		g[b].insert(a);
		p[find(a)] = find(b);
	}

	int s = 0;
	for (int i = 1 ; i <= n ; i ++ )
	{
		if(find(i) != find(1))
		{
			puts("-1");
			return 0;
		}
		else if(g[i].size() % 2) s ++ ;
	}

	if(s != 0 && s != 2 || s == 2 && g[1].size() % 2 == 0)
	{
		puts("-1");
		return 0;
	}

	dfs(1);

	for (int i = top ; i ; i -- )
		cout << ans[i] << ' ';

	return 0;
}

E.

暂时不会~

CCF