C/C++教程

CSP2017

本文主要是介绍CSP2017,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

2017-12-1 最小差值

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int N = 1010;
int a[N];
int n;

int main()
{
    cin >> n;
    int mins = 1e9;
    for(int i = 0; i < n; i++) cin >> a[i];
    for(int i = 0; i < n; i++)
    {
        for(int j = i + 1; j < n; j++)
        {
            mins = min(mins, abs(a[j] - a[i]));
        }
    }
    cout << mins << endl;
}

2017-12-2 游戏

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
const int N = 1010;
int n, k;
bool st[N];

int main()
{
 	cin >> n >> k;
	if (n == 1)
	{
		cout << 1 << endl;
		return 0;
	}
	int size = n;
	int count = 0;
	for (int i = 1; i <= n + 1; i++)
	{
		if (i == n + 1)
			i = 1;
		if (!st[i])
		{
			count++;
			if (count % k == 0 || count % 10 == k)
			{
				st[i] = true;
				size--;
				if (size == 1)
					break;
			}
		}
	}
	for (int i = 1; i <= n; i++)
	{
		if (!st[i])
		{
			cout << i << endl;
			break;
		}
	}
	return 0;
}

2017-9-1

#include<iostream>
using namespace std;
int main()
{
	int x;
	cin >> x;
	int ans = 0;
	while (x > 0)
	{
		if (x >= 50)
		{
			ans += 7;
			x = x - 50;
		}
		else if (x >= 30)
		{
			ans += 4;
			x = x - 30;
		}
		else
		{
			ans += x / 10;
			x = 0;
		}
	}
	cout << ans;
	return 0;
}

2017-9-2

#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1010;
int n, k;
int classroom[N];
struct key
{
	int num;
	int status;
	int time;
	bool operator< (const key& t) const
	{
		if (time != t.time) return time < t.time;
		if (status != t.status) return status < t.status;
		return num < t.num;
	}
}a[N * 2];

int find(int num)
{
	for (int i = 1; i <= n; i++)
	{
		if (classroom[i] == num)
			return i;
	}
}

int main()
{
	cin >> n >> k;
	for (int i = 1; i <= n; i++)
		classroom[i] = i;
	int num, start, time;
	int cnt = 0;
	for (int i = 0; i < k; i++)
	{
		scanf("%d%d%d", &num, &start, &time);
		a[cnt++] = { num, 1, start };  //取
		a[cnt++] = { num, 0, start + time };  //放
	}
	sort(a, a + cnt);
	for (int i = 0; i < cnt; i++)
	{
		if (a[i].status == 1)
		{
			classroom[find(a[i].num)] = -1;
		}
		else
		{
			for (int j = 1; j <= n; j++)
			{
				if (classroom[j] == -1)
				{
					classroom[j] = a[i].num;
					break;
				}
			}
		}
	}
	for (int i = 1; i <= n; i++)
		if (i == 1) cout << classroom[i];
		else cout << " " << classroom[i];
	cout << endl;
}

2017-3-1

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int N = 1010;
int n, k;
int a[N];

int main()
{
	cin >> n >> k;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	int sum = 0, res = 0;
	int i = 1;
	while (i <= n)
	{
		sum += a[i];
		if (sum >= k || i == n)
		{
			sum = 0;
			res++;
		}
		i++;
	}
	cout << res << endl;
}

2017-3-2

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int n, k;
const int N = 1010;
int a[N];

int find(int x)
{
	for (int i = 1; i <= n; i++)
	{
		if (a[i] == x)
			return i;
	}
}

void move1(int x, int y)
{
	for (int i = x + 1; i <= x + y; i++)
	{
		swap(a[i], a[i - 1]);
	}
}

void move2(int x, int y)
{
	for (int i = x - 1; i >= x - y; i--)
	{
		swap(a[i], a[i + 1]);
	}
}

int main()
{
	cin >> n >> k;
	for (int i = 1; i <= n; i++) a[i] = i;
	int x, y;
	while (k--)
	{
		cin >> x >> y;
		if (y > 0)
			move1(find(x), y);
		else
			move2(find(x), -y);
		
	}
	for (int i = 1; i <= n; i++)
		if (i == 1) cout << a[i];
		else cout << " " << a[i];
	cout << endl;
}
这篇关于CSP2017的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!