TOJ 2805.Prairie dogs III

本文介绍了一个简单的游戏模拟问题,名为PrairiedogsIII。游戏中,黑色尾草原犬鼠按照特定规则从一排中消失,直到没有任何两只相邻。通过递归算法实现了这一过程,并给出了完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://acm.tju.edu.cn/toj/showp2805.html



2805.    Prairie dogs III
Time Limit: 1.0 Seconds    Memory Limit: 65536K
Total Runs: 1016    Accepted Runs: 684



The lovely prairie dogs come to our sight again! The black-tailed prairie dog is a large, burrowing, ground squirrel belonging to a group of four other prairie dog species found only in North America. The black-tailed prairie dog is the most abundant and widely distributed prairie dog.

We suppose the prairie dogs are perfect architects, and they build their house in the farmland. One day, the black-tailed prairie dogs assemble at the farmland to play a funny game. The prairie dogs stand in a line with the length of 3order. Each time the middle third of the line of prairie dogs are disappeared. They repeat until none of the prairie dogs next to each other.

For example, if the order is 3, the initial number of prairie dogs(stands by the mark '@') is 27(33 = 27):

@@@@@@@@@@@@@@@@@@@@@@@@@@@
The middle third of prairie dogs are disappeared(replace '@' with space):
@@@@@@@@@         @@@@@@@@@
and disappear the middle third of each piece of the line:
@@@   @@@         @@@   @@@
and again:
@ @   @ @         @ @   @ @
The process stops because none of the remaining prairie dogs next to each other. Only the final result should be displayed.

Input

Each line of input will be a single number between 0 and 10, inclusive, indicating the order. The input stops when EOF(end of file) is reached.

Output

You must output of final result of the line of prairie dogs, followed by a newline. There is no whitespace before or after your output.

Sample Input

0
1
3

Sample Output

@
@ @
@ @   @ @         @ @   @ @

Problem Setter: chhot@TJURocket



Source: TJU Programming Contest 2007 Preliminary
Submit   List    Runs   Forum   Statistics

水题,上代码

#include <stdio.h>
#include <iostream>
#include <cmath>
using namespace std;
string s;
int n;
void dog(int a){
	if(s.length()>=(int)pow(3.0,n)){
		
		s+="\0";
		//cout<<s<<"haha"<<s.length()<<endl;
		return ;
	}
	if(a==0){
		return dog(1);
	}
	if(a==1){
		string t=" ";
		for(int i=1;i<s.length();i++)
			t+=" ";
		s+=t;	
		return dog(2);
	}
	if(a==2){
		int length=s.length();
		for(int i=0;i<length/2;i++)
			s+=s[i];
		//cout<<p<<"haha"<<s;
		return dog(0);
	}
}
int main(){
	while(~scanf("%d",&n)){
		s="@";
		dog(0);
		cout<<s<<endl;
	}
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值