现在的位置: 首页 > 综合 > 正文

寒假刷题之10——细菌密度

2014年09月05日 ⁄ 综合 ⁄ 共 5048字 ⁄ 字号 评论关闭
 Linear Cellular Automata 

A biologist is experimenting with DNA modification of bacterial colonies being grown in a linear array of culture dishes. By changing the DNA, he is able ``program" the bacteria to respond to the population density
of the neighboring dishes. Population is measured on a four point scale (from 0 to 3). The DNA information is represented as an array DNA, indexed from 0 to 9, of population density values and is interpreted as follows:

  • In any given culture dish, let K be the sum of that culture dish's density and the densities of the dish immediately to the left and the dish immediately to the right. Then, by the next day, that
    dish will have a population density of DNA[K].
  • The dish at the far left of the line is considered to have a left neighbor with population density 0.
  • The dish at the far right of the line is considered to have a right neighbor with population density 0.

Now, clearly, some DNA programs cause all the bacteria to die off (e.g., [0,0,0,0,0,0,0,0,0,0]). Others result in immediate population explosions (e.g., [3,3,3,3,3,3,3,3,3,3]). The biologist is interested in how
some of the less obvious intermediate DNA programs might behave.

Write a program to simulate the culture growth in a line of 40 dishes, assuming that dish 20 starts with a population density of 1 and all other dishes start with a population density of 0.

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank
line between two consecutive inputs.

For each input set your program will read in the DNA program (10 integer values) on one line.

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

For each input set it should print the densities of the 40 dishes for each of the next 50 days. Each day's printout should occupy one line of 40 characters. Each dish is represented by a single character on that
line. Zero population densities are to be printed as the character ` '. Population density 1 will be printed as the character `.'. Population density 2 will be printed as the character `x'. Population density 3 will be printed as the character
`W'.

Sample Input

1

0 1 2 0 1 3 3 2 3 0

Sample Output

bbbbbbbbbbbbbbbbbbb.bbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbbbb...bbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbbb.xbx.bbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbb.bb.bb.bbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbb.........bbbbbbbbbbbbbbbb
bbbbbbbbbbbbbb.xbbbbbbbx.bbbbbbbbbbbbbbb
bbbbbbbbbbbbb.bbxbbbbbxbb.bbbbbbbbbbbbbb
bbbbbbbbbbbb...xxxbbbxxx...bbbbbbbbbbbbb
bbbbbbbbbbb.xb.WW.xbx.WW.bx.bbbbbbbbbbbb
bbbbbbbbbb.bbb.xxWb.bWxx.bbb.bbbbbbbbbbb
 

Note: Whe show only the first ten lines of output (the total number of lines must be 50) and the spaces have been replaced with the character"b" for ease of reading. The actual output
file will use the ASCII-space character, not "b".


  很酷的题,因为第一眼看不懂。。。╮(╯▽╰)╭


  于是百度了个中文的来看。其实就是让第20个初始化为1,其他为0,然后加上相邻两边的值后所对应的dna密度值,看懂了就很容易了。。。

   scanf的&忘记打了出错了几次,竟然还有这种错误,惭愧了。

   本来使用一个数组进行计算发现计算时会用前面的值,所以使用了俩数组,百度了下复制数组的函数只找到memcpy,还要指定长度和头文件,直接用循环解决了。。。

   发现数组是对称的,所以只用了长度20的数组,输出也要分两次输出,觉得另外写一个输出的函数太麻烦,用switch搞定,结果代码好长。。。

#include <stdio.h>


int main()
{
	int a[20] = {0}, b[20] = {0}, n, dna[10], i, j;
	
	scanf("%d", &n);
	for (i = 0; i < 10; i++)
		scanf("%d", &dna[i]);
		
	a[19] = dna[1];
		
	while (n --){
		for (i = 0; i < 50; i++){
			for (j = 0; j < 20; j++)
				switch (a[j]){
				case 0:	printf(" ");	break;
				case 1:	printf(".");	break;
				case 2:	printf("x");	break;
				case 3:	printf("W");	break;
				}
			for (j = 18; j >= 0; j--)
				switch (a[j]){
				case 0:	printf(" ");	break;
				case 1:	printf(".");	break;
				case 2:	printf("x");	break;
				case 3:	printf("W");	break;
				}
			printf("\n");
			for (j = 0; j < 20; j++){
				if (j == 0)
					b[j] = dna[a[1] + a[0]];
				else if (j == 19)
					b[j] = dna[a[19] + a[18] + a[18]];
				else
					b[j] = dna[a[j] + a[j - 1] + a[j + 1]];
			}
			for (j = 0; j < 20; j++)
				a[j] = b[j];
		}
	}
	return 0;
}

 

   WA了。。。检查错误吧,我已经习惯了。。。

   好吧。。。The outputs of two consecutive cases will be separated by a blank line.

   每组之间有空行,而且我发现无法分组,while要包含整个函数,刚才忘记测试多组了。。。

   坑爹说最后一组竟然不用换行,又WA了几次。

   数组多次循环未初始化,真是粗枝大叶。。。

修改后代码:

#include <stdio.h>

int main()
{
	int n;
	scanf("%d", &n);
	
	while (n --){
		int a[20] = {0}, b[20] = {0}, dna[10], i, j;
		
		for (i = 0; i < 10; i++)
			scanf("%d", &dna[i]);
			
		a[19] = 1;
		
		for (i = 0; i < 50; i++){
			for (j = 0; j < 20; j++)
				switch (a[j]){
				case 0:	printf(" ");	break;
				case 1:	printf(".");	break;
				case 2:	printf("x");	break;
				case 3:	printf("W");	break;
				}
			for (j = 18; j >= 0; j--)
				switch (a[j]){
				case 0:	printf(" ");	break;
				case 1:	printf(".");	break;
				case 2:	printf("x");	break;
				case 3:	printf("W");	break;
				}
			printf("\n");
			for (j = 0; j < 20; j++){
				if (j == 0)
					b[j] = dna[a[1] + a[0]];
				else if (j == 19)
					b[j] = dna[a[19] + a[18] + a[18]];
				else
					b[j] = dna[a[j] + a[j - 1] + a[j + 1]];
			}
			for (j = 0; j < 20; j++)
				a[j] = b[j];
		}
		if (n)
			printf("\n");
	}
	return 0;
}

 

   揪出各种错误后老AC不了,网上找了个程序来对比,突然发现一个严重的错误!我一开始就错了,我天真地以为这个程序的输出是对称的,(之前还输出了个骷髅头,笑了一会儿)原来不是,人家是40个dishes怎么可能对称呢,我输出的只有39个。。。

   哎,又得大幅度改动了。。。

   其实不用大幅度改,只要改一些数字就ok了,然后千辛万苦终于AC。。。


AC代码

 

#include <stdio.h>

int main()
{
	int n;
	scanf("%d", &n);
	
	while (n --){
		int a[40] = {0}, b[40] = {0}, dna[10], i, j;
		
		for (i = 0; i < 10; i++)
			scanf("%d", &dna[i]);
			
		a[19] = 1;
		
		for (i = 0; i < 50; i++){
			for (j = 0; j < 40; j++)
				switch (a[j]){
				case 0:	printf(" ");	break;
				case 1:	printf(".");	break;
				case 2:	printf("x");	break;
				case 3:	printf("W");	break;
				}
			printf("\n");
			
			for (j = 0; j < 40; j++){
				if (j == 0)
					b[j] = dna[a[1] + a[0]];
				else if (j == 39)
					b[j] = dna[a[39] + a[38]];
				else
					b[j] = dna[a[j] + a[j - 1] + a[j + 1]];
			}
			
			for (j = 0; j < 40; j++)
				a[j] = b[j];
		}
		
		if (n)
			printf("\n");
	}
	return 0;
}

 

   嗯。。。思路还不严谨啊,以后还得加油了!


   于是乎,寒假刷题系列共10题,就到此为止了,刷了这些题真是受益匪浅呢,以后有机会再回顾一下,对比一下我的代码和其他人的代码,好好总结一下。

   当然,寒假刷题系列虽然结束了,但并不意味着我寒假都不刷题了,不折腾这个博客了,要知道我博客还有很多栏目都是0篇的呢(目前),我可有很多事情要做的哦。

   过几天就是春节了,不管怎么样(有没有人看),祝大家春节过得愉快,我也是,哈哈!



抱歉!评论已关闭.