Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

菜鸟求教怎样解决TLE问题,希望高手给出相应解决方案代码,谢谢!

Posted by soarwind at 2008-10-04 12:03:39 on Problem 1002
//我的代码,就是最普通的那种,感觉数据应该没问题
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
void optimize(char* des,char* res);
void OutPut(int);

int _tmain(int argc, _TCHAR* argv[])
{
	int t;
	char resource[30];
	char destination[10];
	int modified[100000];
	int count[100000];
	scanf("%d",&t);
	for(int i = 0;i<t;i++)
	{
		scanf("%s",resource);
		optimize(destination,resource);
		modified[i] = atoi(destination);
		count[i] = 1;
	}
	//电话号码冒泡排序
	int m,n,temp,done; 
	m = 0;
	do
	{
		done = 1;
		for(n = t-1;n>=m+1;n--)
		{
			if(modified[n]<modified[n-1])
			{
				done = 0;
				temp = modified[n];
				modified[n] = modified[n-1];
				modified[n-1] = temp;
			}
		}
		m++;
	}while((m<t)&&!done);
	done = 0;
        //记录不重复的电话号码及出现次数
	for(int i = 0;i<t;i++)
	{
		for(int j = i+1;j<t;j++)
		{
			if(modified[i] == modified[j])
			{
				count[i] = count[i] + count[j];
				modified[j] = -1;
			}
		}
	}
        //筛选并输出电话号码簿
	for(int i = 0;i<t;i++)
	{
		if((modified[i]>=0)&&(count[i]>1))
		{
			OutPut(modified[i]);
			printf(" ");
			printf("%d\n",count[i]);
			done = 1;
		}

	}
	if(!done)
	{
		printf("No duplicates.");
	}
	return 0;
}
//电话号码表达式优化函数
void optimize(char* des,char* res)
{
	while(*res)
	{
		switch(*res)
		{
		case '0':*des = '0';des++;break;
		case '1':*des = '1';des++;break;
		case 'A':case 'B':case 'C':case '2':*des = '2';des++;break;
		case 'D':case 'E':case 'F':case '3':*des = '3';des++;break;
		case 'G':case 'H':case 'I':case '4':*des = '4';des++;break;
		case 'J':case 'K':case 'L':case '5':*des = '5';des++;break;
		case 'M':case 'N':case 'O':case '6':*des = '6';des++;break;
		case 'P':case 'R':case 'S':case '7':*des = '7';des++;break;
		case 'T':case 'U':case 'V':case '8':*des = '8';des++;break;
		case 'W':case 'X':case 'Y':case '9':*des = '9';des++;break;
		default:break;
		}
		res++;
	}
}
//电话号码簿输出函数
void OutPut(int num)
{
	int former = num/10000;
	if(former>=100)
	{
		printf("%d",former);
	}
	else
	{
		if(former<10)
		{
			printf("00%d",former);
		}
		else
		{
			printf("0%d",former);
		}
	}
	printf("-");
	int latter = num%10000;
	if(latter>=1000)
	{
		printf("%d",latter);
	}
	else
	{
		if((latter<1000)&&(latter>=100))
		{
			printf("0%d",latter);
		}
		else 
		{
			if(latter<10)
			{
				printf("000%d",latter);
			}
			else
			{
				printf("00%d",latter);
			}
		}
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator