Phone Numbers(C题)

题面:在这里插入图片描述

题目大意:

Vasya有一个电话簿,里面存着他好友的电话号码,但是有一些是重复的,请你整理一下

  1. 如果同一个人某一个电话号码是另一个电话号码的末尾的一部分,比如‘321’‘21’是同一个电话号码‘321’。

数据范围:

1<= n <=20
电话号码和名字长度不大于10
对于每一行数据最多10个电话号码

解题思路:

数据不大,老老实实模拟就行,因为要从电话的后面开始比较,所以我就先把号码翻转一下,再进行比较,会比较方便。
代码量较大,可能有比较好的方法写把,得去瞅瞅dl的代码了。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define rep 1000 //重复号码或者是号码的字串 
struct person{
	char name[11];
	char phone_n[201][11];
	int sum;
}p[20];
int n = 0;//已存人数
char pnumber[11];
//检查name
int c(char *name)
{
	for(int i = 0;i<n;i++)
		if(!strcmp(name,p[i].name))
			return i;
	return n++;
}
//置换字符串
void sw(char *phone,int l)
{
	for(int i = 0;i<l/2;i++)
	{
		char ch;
		ch = phone[i];
		phone[i] = phone[l-i-1];
		phone[l-i-1] = ch;
	}
}
//查重
int C(struct person a)
{
	int d = a.sum;
	int l1 = strlen(pnumber),l2;
	int i,j,x = -1;
	for(i = 0;i<d;i++)
	{
		l2 = strlen(a.phone_n[i]);
		sw(a.phone_n[i],l2);
		if(l1>l2)//被检查号码较长
		{
			for(j = 0;j<l2;j++)
			if(a.phone_n[i][j] != pnumber[j])
				break;
			if(j == l2)return i;//新的为旧的母串!!!


		}
		else if(l1<l2)//被检查号码较短
		{
			for(j = 0;j<l1;j++)
				if(a.phone_n[i][j] != pnumber[j])
					break;
			if(j == l1) x =rep ;//重复


		}
		else//被检查号码相等长度
		{
			for(j = 0;j<l1;j++)
				if(a.phone_n[i][j] != pnumber[j])
					break;
			if(j == l1) x = rep;//重复
		}
		if(x == rep)break;
	}
	if(x == rep)
		return rep;
	else return d;
}

int main()
{
	int t,temp;
	int k;//目前活动对象
	char name[11];

	scanf("%d",&t);
	for(int i = 0;i<t;i++)
	{
		scanf("%s %d",name,&temp);
		k = c(name);//查找是否是已经存储的某个人
		strcpy(p[k].name,name);
		for(int j = 0;j<temp;j++)
		{
			scanf("%s",pnumber);
			sw(pnumber,strlen(pnumber));
			int q = C(p[k]);
			sw(pnumber,strlen(pnumber));
			if(q!=rep)
			{
				if(q == p[k].sum)p[k].sum++;
				strcpy(p[k].phone_n[q],pnumber);
			}
		}
	}
	//输出
	printf("%d\n",n);
	for(int i = 0;i<n;i++)
	{
		printf("%s %d ",p[i].name,p[i].sum);
		for(int j = 0;j<p[i].sum;j++)
			printf("%s ",p[i].phone_n[j]);
		printf("\n");
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值