PAT乙级1039
Posted chouxianyu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT乙级1039相关的知识,希望对你有一定的参考价值。
题目链接
https://pintia.cn/problem-sets/994805260223102976/problems/994805283241443328
题解
用两个字符串表示两个箱子,一个装有的珠子,一个装想要的珠子。
如果发现两个箱子里都有某个珠子,则把这两个珠子都取出来,重复该操作至某个箱子中没有珠子了或者两个箱子里没有相同的珠子。
// PAT BasicLevel 1039
// https://pintia.cn/problem-sets/994805260223102976/problems/994805283241443328
#include <iostream>
#include <string>
using namespace std;
int main()
// 有的珠子和想要的珠子
string have,want;
cin >> have >> want;
// 有想要的并且还有珠子
while(have.length()>0 && want.length()>0)
// 遍历想要的珠子在有的珠子里找
int i = 0;
while (i < want.length())
char c = want[i];
if (have.find(c) != string::npos)
want.erase(want.begin() +i);
have.erase(have.begin()+have.find(c));
break;
i++;
// 没有想要的任意一个珠子
if(i==want.length())
break;
// 要的珠子都拿到了
if(want.length()==0)
cout << "Yes " << have.length();
// 还有珠子没拿到
else
cout << "No " << want.length();
//system("pause");
return 0;
作者:@臭咸鱼
转载请注明出处:https://www.cnblogs.com/chouxianyu/
欢迎讨论和交流!
以上是关于PAT乙级1039的主要内容,如果未能解决你的问题,请参考以下文章