电话号码 C语言

题目就不说了。作为算法小白的我。直接来了一发,简单简单粗暴的。100万的数组走起。哈哈,OJ一掌拍回。LTE,LTE,LTE,LTE。。。。。。WA都 WA不出。改改改,LTE,LTE。不是方法太low是啥。用户的CPU是我的。。。。。。不得不向百度爸爸低头,原来还有个位图法(参考网站脚本之家),长见识了。一个 Int 可以占有32位,每一位就可以记录一个号码存在与否的信息 ,由于只有3与6开头,各有100万(N)个:int 0000 0000 0000 0000 0000 0000比如3开头的就只需要一个int bit[N/32+1]的数组,怎么存入信息呢?比如3456789 他的bit下标就是i=456789/32=14274,由于456789%32=21那么就把从右往左的第22位置为1通过位运算bit[i] |= 1<<(4567897&31)就可以。
然后来了一发
#include
#include
#include
#include
#include
#define N 1000000
#define M 21
#define R 9
#define Q 1510
int bit[ N / 32 + 1];
int bit2[N / 32 + 1];
struct numbers
{
	long number;
	int  amount;
};
struct numbers store[Q];
char afterTrans[R];
int main()
{
	char * transToStd2(long num);
	long transToNum(char input[M]);
	char *transToStd(char input[M]);
	for (int i = 0; i < Q; i++)
		store[i].amount = 1;
	int isAnError(char input[M]);
	char *transToStd(char input[M]);
	char input[M];
	long error = 0;
	int totalAmount = 0;
	printf("Error:\n");
	gets(input);
	do
	{
		//printf("isAnError(input):%d\n", isAnError(input));
		if (isAnError(input) == 1)//??
		{
			error++;
			printf("%s\n", input);
		}
		else
		{
		//	printf("test  num:%d  std:%s\n", transToNum(input),transToStd(input));
			int flag = 0;
			long inputNum = transToNum(input);
			if (inputNum >= 3 * N && inputNum < 4 * N)
			{
				inputNum -= 3 * N;
				if ((bit[inputNum / 32] & (1 << inputNum % 32)) >0)// 判重 
				{
					for (int j = 0; j < totalAmount; j++)
					{
						if (transToNum(input) == store[j].number)//计数 
						{
							store[j].amount += 1;
							flag = 1;
						}
					}
					if (flag == 0)//存入新的重复号码 
						store[totalAmount++].number = transToNum(input);
				}
				else//不重则相应位置为1 
					bit[inputNum / 32] |= (1 << (inputNum % 32));
			}
			if(inputNum >= 6 * N && inputNum < 7 * N)
			{
				inputNum -= 6 * N;
				if ((bit2[inputNum / 32] & (1 << inputNum % 32)) >0)
				{
					for (int j = 0; j < totalAmount; j++)
					{
						if (transToNum(input) == store[j].number)
						{
							store[j].amount += 1;
							flag = 1;
						}
					}
					if (flag == 0)
						store[totalAmount++].number = transToNum(input);
				}
				else
					bit2[inputNum / 32] |= (1 << (inputNum % 32));
			}
			//duplication judge and store
			//printf("bit[inputNum / 32] & (1 << inputNum % 32):%ld\n", bit[inputNum / 32] & (1 << inputNum % 32));
		}
	} while (gets(input) != NULL);
	//printf("totalAmount:%d\n", totalAmount);
	//printf("Test!!!!!!!\n");
	//for (int i = 0; i < index; i++)
	//printf("standard:%s\n", standard[i]);
	if (error == 0)
		printf("Not found.\n");
	printf("\n");
	//sort
	int cmp(const void *a, const void *b);
	qsort(store, totalAmount, sizeof(struct numbers), cmp);
	printf("Duplication:\n");
	if(totalAmount == 0)
		printf("Not found.\n");
	else
		for (int i = 0; i < totalAmount; i++)
			printf("%s %d\n", transToStd2(store[i].number),store[i].amount+1);
}

char * transToStd2(long num)
{
	afterTrans[R - 1] = '\0';
	for (int i = R - 2; i >= 0; i--)
	{
		if (i == 3)
		{
			afterTrans[i] = '-';
			continue;
		}
		else
		{
			afterTrans[i] = num % 10 + '0';
			num = num / 10;
		}
	}
	return afterTrans;
}

int isAnError(char input[M])
{
	int flag = 0;
	// Is it inclde Q or Z?
	for (int i = 0; i < strlen(input); i++)
	{
		if (input[i] == 'Q' || input[i] == 'Z')
		{
			flag = 1;
			break;
		}
	}
	if (flag == 1)
		return flag;
	else
	{
		int first = 0;
		for(int i=0;i= '0'&&input[i] <= '9' || input[i] == '-')
				afterTrans[index++] = input[i];
			else
			{
				if (input[i] == 'A' || input[i] == 'B' || input[i] == 'C')
					afterTrans[index++] = '2';
				else if (input[i] == 'D' || input[i] == 'E' || input[i] == 'F')
					afterTrans[index++] = '3';
				else if (input[i] == 'G' || input[i] == 'H' || input[i] == 'I')
					afterTrans[index++] = '4';
				else if (input[i] == 'J' || input[i] == 'K' || input[i] == 'L')
					afterTrans[index++] = '5';
				else if (input[i] == 'M' || input[i] == 'N' || input[i] == 'O')
					afterTrans[index++] = '6';
				else if (input[i] == 'P' || input[i] == 'R' || input[i] == 'S')
					afterTrans[index++] = '7';
				else if (input[i] == 'T' || input[i] == 'U' || input[i] == 'V')
					afterTrans[index++] = '8';
				else
					afterTrans[index++] = '9';
			}
		}
	}
	if (afterTrans[3] != '-')
	{
		for (int i = R - 2; i > 3; i--)
			afterTrans[i] = afterTrans[i - 1];
		afterTrans[3] = '-';
	}
	afterTrans[R - 1] = '\0';
	return afterTrans;
}

long transToNum(char input[M])
{
	long num = 0;
	strcpy(afterTrans, transToStd(input));
	//remove '-'.
	for (int i = 3; i <= R - 3; i++)
		afterTrans[i] = afterTrans[i + 1];
	afterTrans[R - 2] = '\0';
	//trans to number.
	for (int i = 0; i < R - 2; i++)
		num+=(afterTrans[i]-'0')*pow(10,R-2-i-1);
	return num;
}

int cmp(const void *a, const void *b)
{
	return ((struct numbers *)a)->number-((struct numbers *)b)->number;//number from smaller to bigger
}

过了两个样例,后两列LTE
都是泛滥的函数调用造成的,还有缓慢的pow

然后又来一发
#include
#include
#include
#include
#include
#define N 1000000
#define M 21
#define R 9
#define Q 1510
int bit[N / 32 + 1];
int bit2[N / 32 + 1];
int dec[10];
struct numbers
{
	long number;
	int  amount;
};
struct numbers store[Q];
char afterTrans[R];
int main()
{
	dec[0] = 1;
	for (int i = 1; i <= 9; i++)dec[i] = dec[i - 1] * 10;
	char * transToStd2(long num);
	long transToNum(char input[M]);
	for (int i = 0; i < Q; i++)
		store[i].amount = 1;
	int isAnError(char input[M]);
	char input[M];
	long error = 0;
	int totalAmount = 0;
	printf("Error:\n");
	gets(input);
	do
	{
		//printf("isAnError(input):%d\n", isAnError(input));
		if (isAnError(input) == 1)//判错
		{
			error++;
			printf("%s\n", input);
		}
		else
		{
			//printf("test  num:%d\n", transToNum(input));
			int flag = 0;
			long inputNum = transToNum(input);
			if (inputNum >= 3 * N && inputNum < 4 * N)
			{
				inputNum -= 3 * N;
				if (bit[inputNum / 32] & (1 << (inputNum & 31)))// 判重
				{
					for (int j = 0; j < totalAmount; j++)
					{
						if (inputNum == store[j].number)//计数
						{
							store[j].amount += 1;
							flag = 1;
						}
					}
					if (flag == 0)//存重
						store[totalAmount++].number = inputNum;
				}
				else
					bit[inputNum / 32] |= (1 << (inputNum & 31));
			}
			if (inputNum >= 6 * N && inputNum < 7 * N)
			{
				inputNum -= 6 * N;
				if (bit2[inputNum / 32] & (1 << (inputNum & 31)))
				{
					inputNum += 1000000;
					for (int j = 0; j < totalAmount; j++)
					{
						if (inputNum == store[j].number)
						{
							store[j].amount += 1;
							flag = 1;
						}
					}
					if (flag == 0)
						store[totalAmount++].number = inputNum;
				}
				else
					bit2[inputNum / 32] |= (1 << (inputNum & 31));
			}
			//duplication judge and store
			//printf("bit[inputNum / 32] & (1 << inputNum % 32):%ld\n", bit[inputNum / 32] & (1 << inputNum % 32));
		}
	} while (gets(input) != NULL);
	//printf("totalAmount:%d\n", totalAmount);
	//printf("Test!!!!!!!\n");
	//for (int i = 0; i < index; i++)
	//printf("standard:%s\n", standard[i]);
	if (error == 0)
		printf("Not found.\n");
	printf("\n");
	//sort
	int cmp(const void *a, const void *b);
	qsort(store, totalAmount, sizeof(struct numbers), cmp);
	printf("Duplication:\n");
	if (totalAmount == 0)
		printf("Not found.\n");
	else
		for (int i = 0; i < totalAmount; i++)
			printf("%s %d\n", transToStd2(store[i].number), store[i].amount + 1);
}

char * transToStd2(long num)
{
	afterTrans[R - 1] = '\0';
	for (int i = R - 2; i >= 0; i--)
	{
		if (i == 3)
		{
			afterTrans[i] = '-';
			continue;
		}
		else
		{
			afterTrans[i] = num % 10 + '0';
			num = num / 10;
		}
	}
	if (afterTrans[0] == '0')afterTrans[0] = '3';
	if (afterTrans[0] == '1')afterTrans[0] = '6';
	return afterTrans;
}

int isAnError(char input[M])
{
	int flag = 0;
	// Is it inclde Q or Z?
	int l = strlen(input);
	for (int i = 0; i < l; i++)
	{
		if (input[i] == 'Q' || input[i] == 'Z')
		{
			flag = 1;
			break;
		}
	}
	if (flag == 1)return flag;
	else
	{
		int first = 0;
		for (int i = 0; i= '0'&&input[i] <= '9')
			num += (input[i] - '0') * dec[n--];
		else continue;
	}
	return num;
}

int cmp(const void *a, const void *b)
{
	return ((struct numbers *)a)->number - ((struct numbers *)b)->number;//number from smaller to bigger
}


开心,
虽然已经是折扣时间了。

你可能感兴趣的:(电话号码 C语言)