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

题目描述

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;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值