PTA习题:抓老鼠啊~亏了还是赚了?

最近开始做一些PTA上面的题,发现自己差的太远了,写的东西不仅长而且逻辑混乱。

附上代码。刚才看了别的人写的,只用了我的一半的代码量,就实现了很多功能,稍后会研读一下他的代码。

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
/*
	题目:抓老鼠
	作者:Unis
	时间:2018.3.3 星期六
*/
/*
	人类:
	T:放置带奶酪的捕鼠夹
	C:放置奶酪
	X:什么也不放
	捕鼠夹可以重复利用
	奶酪每块3元
	老鼠:
	if x :day+=1
	if t :day+=2 抓住老鼠money+=10 cheese-=1
	if c :
	输出:
	第一行
		!:派出老鼠吃到奶酪
		D:派出老鼠被打死
		U:派出老鼠没有收获
		-:没派出老鼠
	第二行:
		盈利
*/

//测试输入输出
//void TestIO(char str[],int x){
//	for (int j = 0; j < x; j++) {
//			printf("%c", str[j]);
//	}
//	printf("\n");
//}
//输出老鼠的状态
void PrintMouseStaus(char staus[],int x) {
	for (int j = 0; j < x; j++) {
		printf("%c", staus[j]);
	}
	printf("\n");
}
//输出抓老鼠的利润
void PrintProfit(char staus[], int x) {
	int profit = 0;
	for (int j = 0; j < x; j++) {
		if (staus[j] == 'D') {
			profit += 7;
		}
		else if (staus[j] == '!') {
			profit -= 3;
		}
	}
	printf("%d",profit);
	printf("\n");
}
//老鼠状态
void MouseStaus(char str[],int x) {
	char staus[70];
	int i = 0;
	for (int j = 0; j < x; j++) {
		//T:放置带奶酪的捕鼠夹
		if (str[j] == 'T') {
			if (i == 0) {
				staus[i++] = 'D';
			}
			else {
				if (staus[i-1] == '!') {
					staus[i++] = 'D';
				}
				else if ((staus[i - 1] == 'D' || staus[i - 1] == 'U') && staus[i - 2] != '!') {
					staus[i++] = '-';
				}
				else if (staus[i - 1] == '-' && staus[i - 2] == 'D') {
					staus[i++] = '-';
				}
				else if (staus[i-1] == 'D' && staus[i-2] == '!') {
					staus[i++] = 'D';
				}
				else {
					staus[i++] = 'D';
				}
			}
		}
		//X:什么也不放
		else if (str[j] == 'X') {
			if (i == 0) {
				staus[i++] = 'U';
			}
			else {
				if (staus[i - 1] == '!') {
					staus[i++] = 'U';
				}
				else if ((staus[i-1] == 'D' || staus[i-1] == 'U') && staus[i-2] != '!') {
					staus[i++] = '-';
				}
				else if (staus[i-1] == '-' && staus[i-2] == 'D') {
					staus[i++] = '-';
				}
				else {
					staus[i++] = 'U';
				}
			}
			
		}
		//C:放置奶酪
		else if (str[j] == 'C') {
			if (i == 0) {
				staus[i++] = '!';
			}
			else {
				if (staus[i - 1] == 'D' || staus[i-1] == 'U') {
					staus[i++] = '-';
				}
				else if (staus[i - 1] == '-' && staus[i - 2] == 'D') {
					staus[i++] = '-';
				}
				else {
					staus[i++] = '!';
				}
			}
		}

	}
	PrintMouseStaus(staus,i);
	PrintProfit(staus, x);
}

int main(void){
	char str[70];
	char ch;
	int i = 0;
	do{
		
		ch = getchar();
		if (ch == '$') break;
		str[i++] = ch;

	} while (ch != '$');
	//TestIO(str,i);
	MouseStaus(str,i);
	system("pause");
	return 0;
	}

/*
人类行为:TXXXXC
老鼠状态:D--U-!

人类状态:CTTCCX
老鼠状态:!DD--U 

人类行为:XTTCCC
老鼠状态:U-D--!

人类行为:XCT  TCX TTT XXX XTT CCC CTT
老鼠状态:U-D  D-- D-- U-U U-D !!! !DD
*/

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值