算法笔记习题4.2

  • 问题 A: 谁是你的潜在朋友

 

题目描述

 

    “臭味相投”——这是我们描述朋友时喜欢用的词汇。两个人是朋友通常意味着他们存在着许多共同的兴趣。然而作为一个宅男,你发现自己与他人相互了解的机会 并不太多。幸运的是,你意外得到了一份北大图书馆的图书借阅记录,于是你挑灯熬夜地编程,想从中发现潜在的朋友。
    首先你对借阅记录进行了一番整理,把N个读者依次编号为1,2,…,N,把M本书依次编号为1,2,…,M。同时,按照“臭味相投”的原则,和你喜欢读同一本书的人,就是你的潜在朋友。你现在的任务是从这份借阅记录中计算出每个人有几个潜在朋友。

输入

 

    每个案例第一行两个整数N,M,2 <= N ,M<= 200。接下来有N行,第i(i = 1,2,…,N)行每一行有一个数,表示读者i-1最喜欢的图书的编号P(1<=P<=M)

输出

 

    每个案例包括N行,每行一个数,第i行的数表示读者i有几个潜在朋友。如果i和任何人都没有共同喜欢的书,则输出“BeiJu”(即悲剧,^ ^)

样例输入

4 5
2
3
2
1

样例输出

1
BeiJu
1
BeiJu

 

#include <stdio.h>
#include <string.h>
int main()
{
	int book[210],n,m,num[210];
	//book[i]数组表示图书i有几个人借阅 
	//num[i]数组表示读者i借阅了哪本书 
	while(scanf("%d%d",&n,&m) != EOF)
	{
		memset(book,0,sizeof(book));
		for(int i=0;i<n;i++)
		{
			scanf("%d",&num[i]);
			book[num[i]] ++;
		}
		for(int i=0;i<n;i++)
		{
			if(book[num[i]] >=2)
			{
				printf("%d\n",book[num[i]]-1);
			}
			else printf("BeiJu\n");
		}
	}
	return 0;
} 

 

  • 问题 B: 分组统计

 

题目描述

先输入一组数,然后输入其分组,按照分组统计出现次数并输出,参见样例。

输入

输入第一行表示样例数m,对于每个样例,第一行为数的个数n,接下来两行分别有n个数,第一行有n个数,第二行的n个数分别对应上一行每个数的分组,n不超过100。

输出

输出m行,格式参见样例,按从小到大排。

样例输入

1
7
3 2 3 8 8 2 3
1 2 3 2 1 3 1

样例输出

1={2=0,3=2,8=1}
2={2=1,3=0,8=1}
3={2=1,3=1,8=0}

 

 

 

  • 问题 C: Be Unique (20)

 

题目描述

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 (<=105) 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

 

#include <stdio.h>
#include <string.h>
//在num[i]中第一个只出现过一次的数字即为unique number 
int hash[100001];
int num[100001];
int main()
{
	int n;
	while(scanf("%d",&n) != EOF)
	{
		memset(hash,0,sizeof(hash));
		for(int i=0;i<n;i++)
		{
			scanf("%d",&num[i]);
			hash[num[i]] ++;
		}
		int i=0;
		for(;i<n;i++)
		{
			if(hash[num[i]] == 1)
			{
				printf("%d\n",num[i]);
				break;
			}
		}
		if(i==n)
		{
			printf("None\n");
		}
	}
	return 0;
}

 

 

  • 问题 D: String Subtraction (20)

 

题目描述

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.

 

#include <stdio.h>
#include <string.h>
char s1[10010],s2[10010],s[10010];
int main()
{
	int flag,count;
	while(gets(s1) != NULL)
	{
		gets(s2);
		int len1=strlen(s1);
		int len2=strlen(s2);
		count=0;
		memset(s,0,sizeof(s));
		for(int i=0;i<len1;i++)
		{
			flag=0;
			for(int j=0;j<len2;j++)
			{
				if(s1[i]==s2[j])
				{
					flag=1;
					break;
				}
			}
			if(flag==0)
			{
				s[count++]=s1[i];
			} 
		}
		puts(s);
	}
	return 0;
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值