哈希表训练

菜鸡的进阶之路
在这里插入图片描述
因为pat现在好像不支持gets了,所以用gets会报错,换成cin.getline没毛病

#include<stdio.h>
#define maxsize 10001

//什么时候用string类,什么时候要用字符串数组
int main(){
    char s[maxsize];
    int count[200]={};
    char target[]="PATest";
    gets(s);
    for(int i=0; s[i]!='\0';i++){
        ++count[(int)s[i]];
    }
    
    while(1){
        int flag=0;//标志位用来判断循环结束的
        for(int i=0; target[i]!='\0';i++){
            if(count[(int)target[i]]>0){
                printf("%c",target[i]);
                --count[(int)target[i]];
                flag=1;
            }
        }
        if(flag==0) break;

        
    }
    return 0;
    
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
这道题做下来虽然不难但是有坑,倒数二个循环条件i<200,之前判断条件写字符串检查到末尾,就会有部分不正确的结果,这个问题我研究研究在更新

#include<iostream>
#define maxsize 1001
using namespace std;

int main(){
    char sell[maxsize];
    char need[maxsize];
    int count[200]={};
    cin.getline(sell,maxsize);
    cin.getline(need,maxsize);
    //记录卖的珠子的个数
    for(int i=0; sell[i]!='\0'; i++){
        ++count[(int)sell[i]];
    }
   
    for(int i=0; need[i]!='\0'; i++){
        --count[(int)need[i]];
    }
    int c=0;
    for(int i=0; i<200; i++){
        if(count[i]<0){
            c+=count[i];
        }
    }
    if(c<0){
        printf("No %d\n",-c);
    }
    else{
        for(int i=0; i<200; i++){
               c+=count[i]; 
        }
        printf("Yes %d\n",c);
    
    }
    return 0;    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值