USACO4.4.1 Shuttle Puzzle (shuttle)

找规律。
4 35 642 1357 642 35 4 (n=3)

5 46 753 2468 97531 2468 753 46 5 (n=4)

其实就是n*2+1组等差数列。
先求出1到n+1组,然后根据对称性求出剩下的。

可以观察到:奇数组为降序,偶数组为升序;奇数组首项为n+i,偶数组首项为n-i+2。


/*
ID:shijiey1
PROG:shuttle
LANG:C++
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <stack>
#include <vector>

using namespace std;

int n;
int main() {
	freopen("shuttle.in", "r", stdin);
	freopen("shuttle.out", "w", stdout);
	int cnt = 0;
	scanf("%d", &n);
	stack<vector<int> > ans;
	for (int i = 2; i <= n + 1; i++) {
		vector<int> s;
		if (i % 2) {
			for (int j = n + i; j >= n - i + 2; j-= 2) {
				s.push_back(j);
			}
		} else {
			for (int j = n - i + 2; j <= n + i; j+= 2) {
				s.push_back(j);
			}
		}
		int l = s.size();
		for (int i = 0; i < l; i++) {
			cout << s[i];
			cnt++;
			if (!(cnt % 20)) {
				cout << endl;
			} else cout << ' ';
		}
		ans.push(s);
	}
	ans.pop();
	while (!ans.empty()) {
		vector<int> s = ans.top();
		ans.pop();
		int l = s.size();
		for (int i = 0; i < l; i++) {
			cout << s[i];
			cnt++;
			if (!(cnt % 20)) {
				cout << endl;
			} else cout << ' ';
		}
	}
	cout << n + 1 << endl;
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值