菜鸡的进阶之路
因为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;
}