PAT Basic Level Practice 1003.我要通过(c++)

分析:这道题难度不在于如何求解,而在于如何确切的理解题意,一开始我直接强解导致有两个测试点不过,之后经过仔细研究分析,终于解决了这两个点。

题意:1.输入一个小于十的数;

           2.每个字符串不超过100个字符且无空格;

           3.每行一个输出。

算法:1.出现PAT之外的字符均输出NO;

          2.Location T - Location P -1 < 2 时输出NO;

          3.(Location T - Location P - 1)* Location P = 字符串长度 -1 - Location T 。


#include <iostream>
#include <string.h>

using namespace std;

int main()
{
	char test[10][101];
	int num,result[11];
	cin >> num;
	if (num < 10)
	{
		for (int i = 0; i < num; i++) {
			cin >> test[i];
		}
		for (int i = 0; i < num; i++) {
			int a, b, c, x = 0, y = 0, z = 0;
			a = strlen(test[i]);
			for (int j = 0; j < a; j++) {
				if (test[i][j] == 'A')
					;
				else if (test[i][j] == 'P') {
					b = j;
					if (x == 1) {
						z = 1;
						break;
					}
					x = 1;
				}
				else if (test[i][j] == 'T') {
					c = j;
					if (y == 1) {
						z = 1;
						break;
					}
					y = 1;
				}
				else {
					z = 1;
					break;
				}
			}
			if (z == 1) {
				result[i] = 0;
			}
			else
			{
				if (c - b <= 1)
					result[i] = 0;
				else {
					if ((c - b -1 )*b == a - c - 1) {
						result[i] = 1;
					}
					else
						result[i] = 0;
				}
			}
		}
		char yes[1][4] = { "YES" }, no[1][4] = { "NO" };
		for (int i = 0; i < num; i++) {
			if (result[i] == 0)
				cout << no[0] << endl;
			else if (result[i] == 1)
				cout << yes[0] << endl;
		}
	}
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值