1040 有几个PAT(25)(25 分)

这道题关键是判断A的左右有多少P和T将两边的个数相乘,开始遍历数组得出T的个数之后就从头开始每遇到T countT就自减1     P就自加1之后遇到A就计算result,将每次的result相加就是结果。 




#include

#include
using namespace std;
int main()
{
string str0;
long long  countP, countT, result;
countP = 0;
countT = 0;
result = 0;
cin >> str0;
for (int i = 0; i < str0.length(); i++)
{
if (str0[i] == 'T')
countT++;
}
for (int i = 0; i < str0.length(); i++)
{
if (str0[i] == 'P') countP++;
if (str0[i] == 'T') countT--;
if (str0[i] == 'A')
{
result = result + countP * countT;
}
}
cout << result% 1000000007 < return 0;

}

你可能感兴趣的:(1040 有几个PAT(25)(25 分))