2021/2/26 CCF练习201412(c++)

201412-1

#include <iostream>
using namespace std;
const int N=1024;
int num[N];
int main() {
    
    
	int n;
	cin>>n;
	int a[N];
	for(int i=0;i<n;i++)
	{
    
    
		cin>>a[i];
	}
	for(int i=0;i<n;i++)
	{
    
    
		num[a[i]]++;
		cout<<num[a[i]]<<" ";
	}
	cout<<endl;
	return 0;
}

201412-2(非原创!!)

#include <iostream>
using namespace std;
 
int main()
{
    
    
	int n;	//矩阵的大小
	cin >> n;
	if (!(n >= 1 && n <= 500))
	{
    
    
		return 0;
	}
	//创建动态二维数组
	int** matrix = new int*[n];
	for (int i = 0; i < n; i++)
	{
    
    
		matrix[i] = new int[n];
	}
 
	//输入矩阵中的元素值
	for (int i = 0; i < n; i++)
	{
    
    
		for (int j = 0; j < n; j++)
		{
    
    
			cin >> matrix[i][j];
		}
	}
 
	int x = 0;	//记录当前扫描到的位置坐标
	int y = 0;
	int direction = 0;	//方向记录变量 0:初始位置 1:向右 2:向下 3:向左下 4:向右上
	cout << matrix[x][y];
 
	while (!(x == n - 1 && y == n - 1))
	{
    
    
		if (direction == 0)
		{
    
    
			direction = 1;
		}
		else if (direction == 1)
		{
    
    
			if (y - 1 >= 0 && x + 1 < n)
			{
    
    
				direction = 3;
			}
			else
			{
    
    
				direction = 4;
			}
		}
		else if (direction == 2)
		{
    
    
			if (y - 1 >= 0 && x + 1 < n)
			{
    
    
				direction = 3;
			}
			else
			{
    
    
				direction = 4;
			}
		}
		else if (direction == 3)
		{
    
    
			if (y - 1 >= 0 && x + 1 < n)
			{
    
    
				direction = 3;
			}
			else if (x + 1 < n)
			{
    
    
				direction = 2;
			}
			else
			{
    
    
				direction = 1;
			}
		}
		else
		{
    
    
			if (y + 1 < n && x - 1 >= 0)
			{
    
    
				direction = 4;
			}
			else if(y + 1 < n)
			{
    
    
				direction = 1;
			}
			else
			{
    
    
				direction = 2;
			}
		}
		switch (direction)
		{
    
    
		case 1: y += 1; break;
		case 2: x += 1; break;
		case 3: y -= 1; x += 1; break;
		case 4: y += 1; x -= 1; 
		}
		cout << " " << matrix[x][y];
	}
	for (int i = 0; i < n; i++)
	{
    
    
		delete [] matrix[i];
	}
	delete [] matrix;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40395925/article/details/114137092