(算法练习)——谁是你的潜在朋友

要求:
http://codeup.cn/problem.php?cid=100000582&pid=0
说明:
这一题一开始理解错题意了,汗,读了好几遍终于明白是什么意思了= =然而代码写的跟狗啃的似的。。。主要就是hash的使用,用空间换时间,比较容易理解了
代码:

#include <stdio.h>
#include <string.h>
const int maxn = 210;
int hashTable[maxn] = {0};
struct code{
	int str[maxn];
}record[maxn];

int main(){
	int n,m,x;
	while(scanf("%d %d",&n,&m) != EOF){
		int symbol = 0;
		for(int i = 0;i <n;i++){
			getchar;
			scanf("%d",&x);
			if(hashTable[x] > 0){
				hashTable[x]++;		
			}
			else{
				hashTable[x] = 1;
			}
			record[symbol].str[i] = x;
		}
		
		for(int i = 0;i <n;i++){
			int j= record[symbol].str[i];
			//累计>=2,意味着有一个及以上的潜在朋友 
			if(hashTable[j]>=2){
				printf("%d\n",hashTable[j] - 1);
			}
			else{
				printf("BeiJu\n");
			}
		}
		//必须要重置这个hash表,不然会重复计数 
		memset(hashTable,0,sizeof(hashTable));
		symbol = 0;
		
	}
	
}
发布了105 篇原创文章 · 获赞 3 · 访问量 1973

猜你喜欢

转载自blog.csdn.net/weixin_42377217/article/details/103962109
今日推荐