目录
1.书内样例
#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <map>
using namespace std;
int hashtable(char martix[],int len){
int id=0;
for(int i=0;i<len;i++){
id = id*26+(martix[i]-'A');
}
return id;
}
int main(){
int n,m;
scanf("%d %d",&n,&m);
char martix[100][5];
int hash[26*26*26+10];
for(int i=0;i<n;i++){
scanf("%s",&martix[i]);
int id = hashtable(martix[i],3);
hash[id]++;
}
char inq[5];
for(int i=0;i<m;i++){
scanf("%s",inq);
int id = hashtable(inq,3);
printf("%d\n",hash[id]);
}
return 0;
}
2.旧键盘
#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <map>
using namespace std;
int main(){
char str1[1000],str2[1000];
gets(str1);
gets(str2);
int len1 = strlen(str1);
int len2 = strlen(str2);
bool hash[128] = {false};// 用来判断是否输出过
for(int i=0;i<len1;i++){
char c1=str1[i];
if(c1>='a'&&c1<='z'){
c1-=32;
}
int j;//后面要用j进行判断
for(j=0;j<len2;j++){
char c2=str2[j];
if(c2>='a'&&c2<='z'){
c2-=32;
}
if(c1==c2){
break;
}
}
if(j==len2&&hash[c1]==false){
printf("%c",c1);
hash[c1] = true;
}
}
return 0;
}
3.旧键盘打字
#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <map>
using namespace std;
int main(){
bool hash[256] ;
memset(hash,true,sizeof(hash));//一开始初始化为全true
char s1[100000],s2[100000];
gets(s1);
gets(s2);
int len1=strlen(s1);
int len2=strlen(s2);
for(int i=0;i<len1;i++){
if(s1[i]>='A'&&s1[i]<='Z'){//如果是大写,就先转为小写再赋值
s1[i] = s1[i]-'A'+'a';
}
hash[s1[i]] = false;
}
for(int i=0;i<len2;i++){
if(s2[i]>='A'&&s2[i]<='Z'){//如果是大写,就判断小写字母和+号是否损坏;否则直接判断是否损坏
char low = s2[i]-'A'+'a';
if(hash[low]==true&&hash['+']==true){
printf("%c",s2[i]);
}
}else if(hash[s2[i]]==true){
printf("%c",s2[i]);
}
}
printf("\n");
return 0;
}
4.到底买不买
先用一个数组把拥有的计数,再对需要的一一比较,如果没有那么miss加一
#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <map>
using namespace std;
int main(){
char whole[1010],target[1010];
gets(whole);
gets(target);
int len1= strlen(whole);
int len2= strlen(target);
int hash[80] = {0},miss=0;
for(int i=0;i<len1;i++){
hash[whole[i]]++;
}
for(int i=0;i<len2;i++){
hash[target[i]]--;
if(hash[target[i]]<0){
miss++;
}
}
if(miss>0){
printf("No %d\n",miss);
}else{
printf("Yes %d\n",len1-len2);
}
return 0;
}
5.输出PATest
#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <map>
using namespace std;
int main(){
char str[10004],dict[6]={'P','A','T','e','s','t'};
char hash[6] = {0};
gets(str);
int len=strlen(str),sum=0;
for(int i=0;i<len;i++){
for(int j=0;j<6;j++){
if(str[i]==dict[j]){
hash[j] ++;
sum++;
}
}
}
while(sum>0){
for(int i=0;i<6;i++){
if(hash[i]>0){
printf("%c",dict[i]);
hash[i]--;
sum--;
}
}
}
return 0;
}
6.Be Unique
#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <map>
using namespace std;
int main(){
int n;
scanf("%d",&n);
int martix[10002];
int hash[10002]={0};
for(int i=0;i<n;i++){
scanf("%d",&martix[i]);
hash[martix[i]]++;
}
int i;
for(i=0;i<n;i++){
if(hash[martix[i]]==1){
printf("%d\n",martix[i]);
break;
}
}
if(i==n){
printf("None\n");
}
return 0;
}
7.继续(3n+1)猜想
#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <map>
using namespace std;
bool cmp(int a,int b){
return a>b;
}
int main(){
int n;
scanf("%d",&n);
int a[102];
bool hash[1000]={0};
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
int x = a[i];
while(x!=1){
if(x%2==1){
x = (3*x+1)/2;
}else{
x/=2;
}
hash[x] = true;
}
}
int b[1000] = {0};
int count = 0;
for(int i=0;i<n;i++){
if(hash[a[i]]==false){
b[count++] = a[i];
}
}
sort(b,b+count,cmp);
for(int i=0;i<count;i++){
printf("%d ",b[i]);
}
return 0;
}
8.Fine Coins
#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <map>
using namespace std;
bool cmp(int a,int b){
return a>b;
}
int main(){
int n,m;
scanf("%d %d",&n,&m);
int martix[10002];
int hash[1000]={0};
for(int i=0;i<n;i++){
scanf("%d",&martix[i]);
hash[martix[i]]++;
}
for(int i=0;i<1000;i++){
if(hash[i]&&hash[m-i]){
if(i==m-i&&hash[i]<=1){
continue;
}
printf("%d %d\n",i,m-i);
return 0;
}
}
printf("No Solution\n");
return 0;
return 0;
}