题目
最近,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;
}