易保研机试训练营-基础营|模拟|D - 浪漫手机

题目

最近,WisKey迷上了手机铃声,但是他对音律不是很懂,所以他想着能否用计算机来随机生成铃声。当WisKey写好程序后,发现生成出来的根本不是铃声,而是噪声!
之后WisKey查阅了一些乐谱发现,其实很多铃声是以某种规律变化的,这里为了简化这个难题,他以连续3个音符来判断下个音符。
如有模式
在这里插入图片描述
在给定第一行乐谱的情况下,按模式将产生如下乐谱图形:
在这里插入图片描述
我们用0表示白色格子,用1表示黑色格子。
对于没有连续3个格子的边缘(即没有左边格子或右边格子),我们直接用白色格子代替缺少的那一个格子。

输入

第一行有一个整数T,代表有T组数据。
每组数据有一个整数M,表示要输出M行乐谱。接着有8行模式串,左边是音符模式,右边是下一个音符。最后一行是第一行乐谱。

输出

输出M行,表示M行乐谱。

样例输入

1
16
111 1
110 1
101 1
100 1
011 1
010 0
001 1
000 0
0000000000000001000000000000000

样例输出

0000000000000001000000000000000
0000000000000010100000000000000
0000000000000101010000000000000
0000000000001010101000000000000
0000000000010101010100000000000
0000000000101010101010000000000
0000000001010101010101000000000
0000000010101010101010100000000
0000000101010101010101010000000
0000001010101010101010101000000
0000010101010101010101010100000
0000101010101010101010101010000
0001010101010101010101010101000
0010101010101010101010101010100
0101010101010101010101010101010
1010101010101010101010101010101

错误代码

//D题 
#include <iostream>
#include <string>
using namespace std;
int main() {
	int T, M, index;
	cin >> T;
	for(int w = 0; w < T; ++w) {
		cin >> M;
		int mode[8] = {0};
		char temp[3];
		for(int i = 0; i < 8; ++i) {
			cin >> temp[2] >> temp[1] >> temp[0] >> index;
			mode[(temp[2] - '0') * 4 + (temp[1] - '0') * 2 + (temp[0] - '0')] = index;
		}
		//string f;
		string first, second;
		//cin >> f;
		cin >> first;
		//cout << f << endl;
		cout << first << endl;
		M--;
		//int end = f.length();
		int end = first.length();
		//char* first = new char[end];
		//for(int i = 0; i < end; ++i)
		//	first[i] = f[i];
		//char* second = new char[end];
		while(M--) {
			for(int i = 0; i < end; ++i) {
				if(i == 0)
					second[i] = mode[(first[i] - '0') * 2 + (first[i + 1] - '0')] + '0';
				else if(i == end - 1)
					second[i] = mode[(first[i - 1] - '0') * 4 + (first[i] - '0') * 2] + '0';
				else
					second[i] = mode[(first[i - 1] - '0') * 4 + (first[i] - '0') * 2 + (first[i + 1] - '0')] + '0';
			}
			cout << second;
			//for(int i = 0; i < end; ++i)
			//	cout << second[i];
			cout << endl;
			//for(int i = 0; i < end; ++i)
			//	first[i] = second[i];
			first = second;
		}
		//delete []first;
		//delete []second;	
	}	
	return 0; 
} 

正确代码

#include <iostream>
#include <string>
using namespace std;
int main() {
	int T, M, index;
	cin >> T;
	for(int w = 0; w < T; ++w) {
		cin >> M;
		int mode[8] = {0};
		char temp[3];
		for(int i = 0; i < 8; ++i) {
			cin >> temp[2] >> temp[1] >> temp[0] >> index;
			mode[(temp[2] - '0') * 4 + (temp[1] - '0') * 2 + (temp[0] - '0')] = index;
		}
		string f;
		cin >> f;
		cout << f << endl;
		M--;
		int end = f.length();
		char* first = new char[end];
		for(int i = 0; i < end; ++i)
			first[i] = f[i];
		char* second = new char[end];
		while(M--) {
			for(int i = 0; i < end; ++i) {
				if(i == 0)
					second[i] = mode[(first[i] - '0') * 2 + (first[i + 1] - '0')] + '0';
				else if(i == end - 1)
					second[i] = mode[(first[i - 1] - '0') * 4 + (first[i] - '0') * 2] + '0';
				else
					second[i] = mode[(first[i - 1] - '0') * 4 + (first[i] - '0') * 2 + (first[i + 1] - '0')] + '0';
			}
			for(int i = 0; i < end; ++i)
				cout << second[i];
			cout << endl;
			for(int i = 0; i < end; ++i)
				first[i] = second[i];
		}
		delete []first;
		delete []second;	
	}	
	return 0; 
} 

错误代码改正2.0

#include <iostream>
#include <string>
using namespace std;
int main() {
	int T, M, index;
	cin >> T;
	for(int w = 0; w < T; ++w) {
		cin >> M;
		int mode[8] = {0};
		char temp[3];
		for(int i = 0; i < 8; ++i) {
			cin >> temp[2] >> temp[1] >> temp[0] >> index;
			mode[(temp[2] - '0') * 4 + (temp[1] - '0') * 2 + (temp[0] - '0')] = index;
		}
		string first, second;
		cin >> first;
		second = first;//预留同样大小的空间 ,后续代码中没有为second开空间,画龙点睛,化腐朽为神奇
		cout << first << endl;
		M--;
		int end = first.length();
		while(M--) {
			for(int i = 0; i < end; ++i) {
				if(i == 0)
					second[i] = mode[(first[i] - '0') * 2 + (first[i + 1] - '0')] + '0';
				else if(i == end - 1)
					second[i] = mode[(first[i - 1] - '0') * 4 + (first[i] - '0') * 2] + '0';
				else
					second[i] = mode[(first[i - 1] - '0') * 4 + (first[i] - '0') * 2 + (first[i + 1] - '0')] + '0';
			}
			cout << second;
			cout << endl;
			first = second;
		}
	}	
	return 0; 
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值