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

请教,这代码过了3725却在这WA了,不知道哪的问题

Posted by 200731000623 at 2009-09-18 03:04:36 on Problem 2171
#include"iostream"

using namespace std;

__int64 table[20];		//保存10^i
__int64 m,k;

void init()
{
	table[0]=1;
	int i;
	for(i=1;i<19;i++)
		table[i]=table[i-1]*10;
	table[19]=1;
	table[19]=~(table[19]<<63);
}

__int64 getrank(__int64 v)		//记算从10^i到v有多少个,(i)为v的位数-1
{
	int i;
	for(i=0;table[i]<=v;i++);
	return v-table[i-1]+1;
}

int check(__int64 v)		//检查是否是10^i,要特殊处理,因为10^i只能排在i+1位。
{
	int i;
	for(i=0;i<19;i++)
		if(table[i]==v)
			return i;
		else
			if(table[i]>v)
				return -1;
			return -1;
}

int main()
{
	init();
	while(EOF!=scanf("%I64d%I64d",&m,&k))
	{
		int temp=check(m);
		if(temp>=0)
		{
			if(temp+1==k)
			{
				printf("%I64d\n",k);
			}
			else
			{
				puts("0");
			}
			continue;
		}
		__int64 r=0;
		__int64 t=m;
		while(t)
		{
			r+=getrank(t);
			t/=10;
		}
		if(r==k)
		{
			printf("%I64d\n",m);
			continue;
		}
		if(r>k)
		{
			puts("0");
			continue;
		}
		k-=r;
		t=m*10;		//当k<r时,要增加n,每次增加一个位数,排在m前面的只有比m*10小的。
		while(true)
		{
			r=getrank(t)-1;
			if(r>=k)
				break;
			k-=r;
			t*=10;
		}
		int i;
		for(i=0;table[i]<=t;i++);
		printf("%I64d\n",table[i-1]+k-1);
	}
	return 0;
}

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