Problem C
题目描述
Being unique is so important to people on Mars that even their lottery is designed in a unique way. The rule of winning is simple: one bets on a number chosen from [1, 104]. The first one who bets on a unique number wins. For example, if there are 7 people betting on 5 31 5 88 67 88 17, then the second one who bets on 31 wins
输入
Each input file contains one test case. Each case contains a line which begins with a positive integer N (<=10^5) and then followed by N bets. The numbers are separated by a space.
输出
For each test case, print the winning number in a line. If there is no winner, print "None" instead.
样例输入
7 5 31 5 88 67 88 17 5 888 666 666 888 888
样例输出
31 None
思路:题意是找第一个唯一出现的数。方法:暴力解。
AC代码
#include<cstdio>
#include<cstring>
int hash[100010];
int value[100010];
int main() {
int num;
while(scanf("%d",&num)!=EOF){
memset(hash,-1,sizeof(int)*100010);
for(int i=0;i<num;i++){
scanf("%d",&value[i]);
hash[value[i]]++;
}
int flag=0;
for(int i=0;i<num;i++){
if(hash[value[i]]==0){
flag=1;
printf("%d\n",value[i]);
break;
}
}
if(flag==0){
printf("None\n");
}
}
return 0;
}
Problem D
题目描述
Given two strings S1 and S2, S = S1 - S2 is defined to be the remaining string after taking all the characters in S2 from S1. Your task is simply to calculate S1 - S2for any given strings. However, it might not be that simple to do it fast.
输入
Each input file contains one test case. Each case consists of two lines which gives S1 and S2, respectively. The string lengths of both strings are no more than 104. It is guaranteed that all the characters are visible ASCII codes and white space, and a new line character signals the end of a string.
输出
For each test case, print S1 - S2 in one line.
输入样例
They are students. aeiou
输出样例
Thy r stdnts.
思路:将出现过的s2hash处理,遍历s1。
AC代码
#include <cstdio>
#include <cstring>
char s1[10002];
char s2[10002];
int hash[1000];
int main() {
while(gets(s1)!=NULL){
gets(s2);
memset(hash,0,sizeof(int)*1000);
for(int i=0;i<strlen(s2);i++){
hash[(int)(s2[i]-' ')]++;
}
for(int i=0;i<strlen(s1);i++){
if(hash[(int)(s1[i]-' ')]!=0){
continue;
}
printf("%c",s1[i]);
}
printf("\n");
}
}
遇到的问题:
1、ascii码约在0~127,空格为32,空格开始输出可见(32~126),127为DEL;
2、gets()与scanf()不同,循环判断时应使用 gets(xx...)!=NULL 的形式。