跳到正文

个人记录

翻硬币游戏

OftenSendACat约 2 分钟
原文
文章信息与版本

文章信息

收录
2026年2月13日
更新
2026年2月13日
篇幅
约 2 分钟

版本

  1. v2当前
  2. v1

规则:

  1. 游戏会给出两个字符串,* 表示硬币正面, o 表示硬币背面

  2. 每一轮可以输入一个正整数 xx,表示将第 xx 和第 x+1x + 1 的硬币翻过来。如果输入的数字过大或过小则无效

  3. 游戏目的是通过翻转第一行相邻的两个硬币来使第一行与第二行的排列方式相同

例如:

将 **oo* 变成 ***** 只需要输入一个 3 将第 33 和第 44 个硬币反转,使两个字符串相同。

代码:

C++
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
#define ll long long
string s,e;
ll x;
void f(ll m){
	if(e[m] == 'o'){
		e[m] = '*';
	}
	else{
		e[m] = 'o';
	}
	if(e[m + 1] == 'o'){
		e[m + 1] = '*';
	}
	else{
		e[m + 1] = 'o';
	}
}
void func(ll m){
	if(s[m] == 'o'){
		s[m] = '*';
	}
	else{
		s[m] = 'o';
	}
	if(s[m + 1] == 'o'){
		s[m + 1] = '*';
	}
	else{
		s[m + 1] = 'o';
	}
}
int main(){
	srand(time(0));
	ll tmp = (rand() % 11 + 10);
	for(ll i = 0;i <= tmp;i ++){
		ll a = rand();
		if(a & 1){
			s += 'o';
		}
		else{
			s += '*';
		}
	}
	e = s;
	ll tmp2 = (rand() % 67 + 66);
	for(ll i = 1;i <= tmp2;i ++){
		ll a = rand();
		f(a % (s.size() - 1));
	}
	cout << s << endl << e << endl;
	while(s != e){
		cin >> x;
		if(x >= s.size() || x < 1){
			system("cls");
			cout << s << endl << e << endl;
			continue;
		}
		x --;
		func(x);
		system("cls");
		cout << s << endl;
		cout << e << endl;
	}
	cout << "Great!" << endl;
	system("pause");
	return 0;
}

注意:用 exe 文件玩效果更好呦 ~

原文