首页 > 学院 > 开发设计 > 正文

PAT BASIC 1003

2019-11-11 05:08:22
字体:
来源:转载
供稿:网友

注意点

1、其实就是要求P之前的A的数量乘以PT之间A的数量等于T之后A的数量 2、注意PT中间等于零的情况 3、注意PT两个字母:必须P在T之前出现,各有且仅有一次

代码

#include<iostream>#include<string>using namespace std;void checkPAT();void checkPAT(){ string str; int pos_P = -1, pos_T = -1, state = 0; int i, j, len; int num[3]; cin >> str; len = str.size(); num[0] = 0; num[1] = 0; num[2] = 0; for (i = 0; i < len; i++) { if (str[i] != 'P' && str[i] != 'A' && str[i] != 'T') { cout << "NO/n"; return; } if (str[i] == 'A') num[state]++; else if (str[i] == 'P') { if (pos_P != -1) { cout << "NO/n"; return; } pos_P = i; state++; } else if (str[i] == 'T') { if (pos_T != -1) { cout << "NO/n"; return; } pos_T = i; state++; } } if (pos_P == -1 || pos_T == -1) { cout << "NO/n"; return; } if (pos_P >= pos_T) { cout << "NO/n"; return; } if (num[1] == 0) { cout << "NO/n"; return; } if (num[0] * num[1] == num[2]) cout << "YES/n"; else cout << "NO/n"; return;}int main(){ int n, i; cin >> n; for (i = 0; i < n; i++) checkPAT(); //while (1) //{ } return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表