您的位置:首页 > 其它

浙大pat | 牛客网甲级 1062 To Buy or Not to Buy (20)

2018-03-28 13:34 537 查看
题目描述Eva would like to make a string of beads with her favoritecolors so she
went to a small shop to buy some beads.

 There were many colorfulstrings of beads. However the owner of the
shop would only sell the strings in whole pieces.

 Hence Eva must checkwhether a string in the shop contains all the
beads she needs. She now comes to you for help:

 if the answer is"Yes", please tell her the number of extra
beads she has to buy; or if the answer is "No", pleasetell

 her the number of beadsmissing from the string.

 For the sake ofsimplicity, let's use the characters in the ranges
[0-9], [a-z], and [A-Z] to represent the colors. For

 example, the string"YrR8RrY" is the one that Eva would like
to make. Then the string "ppRYYGrrYBR2258" is okaysince it
contains

 all the necessary beadswith 8 extra ones; yet the string
"ppRYYGrrYB225" is not since there is no black beadand one
less red bead.

输入描述:
Each input file contains one test case. Each case gives in twolines the strings of no more than 1000 beads which belong to the shop

owner and Eva, respectively.

输出描述:
For each test case, print your answer in one line. If the answeris "Yes", then also output the number of extra beads Eva has to buy;or

if the answer is "No", then also output the number of beads missingfrom the string. There must be exactly 1 space between the answer

and the number.

输入例子:
ppRYYGrrYBR2258

YrR8RrY

输出例子:
Yes 8

这一题犯了一个小错误,就是int数组在声明之后一定要记得初始化,可以={0},初始化,也可以memset初始化,不初始化的话里面的值是一个非常大的负数,这样的数一定是不行的,
以后在把int值和size值进行比较的时候尽量记得把size转换成int值然后进行运算比较#include <iostream>
#include <string>
using namespace std;

void j_count(string theS, int num[])
{
for (int i = 0; i < (int)theS.size(); i++)
{
if (isdigit(theS[i]))
{
num[theS[i] - '0']++;
}
else
{
if (islower(theS[i]))
{
num[theS[i] - 'a' + 10]++;
}
else
{
num[theS[i] - 'A' + 36]++;
}
}
}
}
int main()
{
string theStore;
string theEve;
int theStoreHeads[63] = { 0 };
int theEveHead[63] = { 0 };
cin >> theStore >> theEve;
j_count(theStore, theStoreHeads);
j_count(theEve, theEveHead);
int theLeaveCount=0, theMissingCount=0;
bool canEveGet = true;

for (int i = 0; i < 62; i++)
{
if (theStoreHeads[i] >= theEveHead[i]) theLeaveCount += (theStoreHeads[i] - theEveHead[i]);
else
{
canEveGet = false;
theMissingCount += (theEveHead[i] - theStoreHeads[i]);
}
}
if (canEveGet)
{
cout << "Yes" << " "<< theLeaveCount;
}
else
{
cout << "No" << " " << theMissingCount;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: