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

有用从a到b一个一个数的思路却不超时的算法么?我tle

Posted by 00 at 2005-07-15 10:54:23 on Problem 2282
#include<iostream.h>
#include<string.h>
#include<math.h>
void main()
{
	while(1)
	{
		char a[10]={'\0','\0','\0','\0','\0','\0','\0','\0','\0','\0'},
			b[10]={'\0','\0','\0','\0','\0','\0','\0','\0','\0','\0'},t[10];
		long r[10]={0,0,0,0,0,0,0,0,0,0},i,j,begin=0,end=0;
		cin>>a>>b;
		if(a[0]=='0'&&b[0]=='0')
			break;
		strcpy(t,a);
		for(i=0;i<(long)strlen(a);i++)
		{
			begin+=(long)pow(10,strlen(a)-1-i)*(a[i]-48);
			a[i]=t[strlen(a)-1-i];
		}
		strcpy(t,b);
		for(i=0;i<(long)strlen(b);i++)
		{
			end+=(long)pow(10,strlen(b)-1-i)*(b[i]-48);
			b[i]=t[strlen(b)-1-i];
		}
		if(begin>end)
		{
			i=begin;
			begin=end;
			end=i;
			strcpy(t,a);
			strcpy(a,b);
			strcpy(b,t);
		}
		for(i=0;i<end-begin+1;i++)
		{
			for(j=0;j<(long)strlen(a);j++)
				r[a[j]-48]++;
			a[0]++;
			for(j=0;j<(long)strlen(a);j++)
			{
				if(a[j]>57)
				{
					a[j]-=10;
					if(a[j+1]=='\0')
						a[j+1]=49;
					else
						a[j+1]++;
				}
			}
		}
		for(i=0;i<10;i++)
			cout<<r[i]<<' ';
		cout<<endl;
	}
}

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