UVA457 - Linear Cellular Automata
# 问题描述
输入一个DNA数组,给定第一天的数值,有之后的数值为DNA[temp]里的值(temp=前一天左边的数值+前一天自身的数值+前一天右边的数值)。
例如样例,第一天第20个位置的值1,其余的数值为0,第二天,第20个位置的值为DNA[temp] (temp=第19位置的值+20位置的值+21位置的值=0+1+0=1)=DNA[1]=1。
# 思路
英文翻译题,看懂题目就不会很难,根据题意模拟计算。记得输出中最后没有回车。
# 代码
#include <iostream>
#include <cstdio>
#include <cctype>
#include <cstring>
using namespace std;
int s[55][55];
void print()
{
int i,j;
for(i=1;i<=50;i++)
{
for(j=1;j<=40;j++)
{
switch(s[i][j])
{
case 0:printf(" ");break;
case 1:printf(".");break;
case 2:printf("x");break;
case 3:printf("W");break;
}
}
printf("\n");
}
}
int main()
{
int t,a[10],i,j,temp;
cin>>t;
while(t--)
{
for(i=0;i<10;i++)
cin>>a[i];
memset(s,0,sizeof(s));
s[1][20]=1;
//考虑到第一个位置也要代入公式计算时计数应从1开始计数(而不是从0)
for(i=2;i<=50;i++)
{
for(j=1;j<=40;j++)
{
temp=s[i-1][j-1]+s[i-1][j]+s[i-1][j+1];
s[i][j]=a[temp];
}
}
print();
if(t)
cout<<endl;
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
编辑 (opens new window)
上次更新: 2022/05/18, 19:03:08