uva227(谜题)

题目如图

 作者嫌弃打字母太慢,改用数字,该程序完成后只需再添加一个字典(如1对T,2对R等等)

即可完成,核心问题是如何交换数值,一开始想直接用starti和startj记录每次变化后的空格的position(题中为0的位置),发现数组下标不能为变量,那用指针,用一个pos指针记录空格位置每次更新,perfect(dog)(--0--)!


#include <iostream>

#include <bits/stdc++.h>
#include<ctype.h>

using namespace std;

int main() 
{
	int a[5][5] = { 1,2,3,4,5,6,7,8,9,10,11,0,13,14,15,16,17,18,19,20,21,22,23,24,25};
	for (int i = 0; i < 5; i++)
	{
		for (int j = 0; j <5; j++)
		{
			if (j < 4)
			{
				cout << a[i][j]<<" ";
				
			}
			else
				cout << a[i][j] << endl;
		}
	}
	
	cout << endl;
	string s;
	cin >> s;
	int starti = 2, startj = 1;
	int* pos = &a[2][1];
	int len = s.length();
	for (int i = 0; i < len; i++)
	{
		int tap;
		if (s[i] == 'A')
		{
			int tap;
			tap =*pos;
			*pos = *(pos - 5);
			*(pos - 5) = tap;
			pos = pos - 5;
			continue;
		}
		if (s[i] == 'B')
		{
			int tap;
			tap = *pos;
			*pos = *(pos + 5);
			*(pos + 5) = tap;
			pos += 5;
			continue;
		}

		if (s[i] == 'L')
		{
			int tap;
			tap = *pos;
			*pos = *(pos-1);
			*(pos-1) = tap;
			pos--;
			continue;
		}
		if (s[i] == 'R')
		{
			int tap;
			tap = *pos;
			*pos = *(pos + 1);
			*(pos + 1) = tap;
			pos++;
			continue;
		}



	}

	

	for (int i = 0; i < 5; i++)
	{
		for (int j = 0; j < 5; j++)
		{
			if (j < 4)
			{
				cout << a[i][j] << " ";

			}
			else
				cout << a[i][j] << endl;
		}
	}

}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值