A1092_B1039 To Buy or Not to Buy (20 分)

这部分的重点是:将【A-Z,a-z,0-9】转换为ASCII值,可以进行巧妙的减法使其的范围在100以内。

char change(char c){
    
    
	if(c >= '0' && c <= '9')  return c - '0';  // 0-9
	else if(c >= 'A' && c <= 'Z') return c - 'A' + 10;  //10-35
	else if(c >= 'a' && c <= 'z') return c - 'a' + 36;  //36-61
}

满分代码

#include <iostream>
#include <bits/stdc++.h> 
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

char change(char c){
    
    
	if(c >= '0' && c <= '9')  return c - '0';
	else if(c >= 'A' && c <= 'Z') return c - 'A' + 10;
	else if(c >= 'a' && c <= 'z') return c - 'a' + 36;
}

int main(int argc, char** argv) {
    
    
	
	string s1, s2;
	int hashBread[100] = {
    
    0};
	
	int id = 0;
	cin >> s1 >> s2;
	
	int len1 = s1.length();
	int len2 = s2.length();
	
	char c1;
	for(int i = 0; i < len1; i++){
    
    
		c1 = change(s1[i]);
		hashBread[c1]++;
	}
	
	for(int i = 0; i < len2; i++){
    
    
		c1 = change(s2[i]);
		hashBread[c1]--;
	}
	
	bool isEnough = true; //true则为够用 
	
	int num = 0;
	
	for(int i = 0; i < 100; i++){
    
    
		if(hashBread[i] < 0){
    
    
			isEnough = false; 
			num+=(-hashBread[i]);
		}
	}
	
	if(isEnough == false){
    
    
		cout << "No " << num;
		return 0;
	}
	
	for(int i = 0; i < 100; i++){
    
    
		if(hashBread[i]!=0){
    
    
			num += hashBread[i];
		}
	}
	
	cout << "Yes " << num;
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/alovelypeach/article/details/114282376
今日推荐