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

1019题目怎么总是wrong answer?

Posted by fyliang_wh at 2006-04-05 19:59:33
请教下1019题目?
#include <stdio.h>
#include <math.h>
int getLoc(unsigned long x);
int getNum(unsigned long x);
unsigned long bitCounts(unsigned long x);//<2147483647
int getBit(unsigned long x);

int main()
{	 
    int n;
	unsigned long x;
	int i;
    scanf("%d",&n);                           //输入的个数
	if(n>=1 && n<=10){
	    for(i=0;i<n;i++){
			scanf("%ld",&x);
			printf("%d\n",getLoc(x));
		}
	}
	//system("pause");
    return 0;
}

/*
得到x的位置
*/
int getLoc(unsigned long x){
	unsigned long t,i,p;
	t=0;
	i=1;
	p=0;
	while(t<x){
		t += bitCounts(i++);
	}
	p = x - (t - bitCounts(i-1) );	  
	return getNum(p);
}

/*计算从1到x 的顺序字符串 "123456789101112" 有多少位 */
unsigned long bitCounts(unsigned long x){
	unsigned long t=1,init_x = x;
	unsigned long r=0;
	int i;
	t = getBit(x);
	r = (init_x - pow(10,t-1) + 1) * t;
	for(i=1;i<t;i++){
		r += (pow(10,i) - pow(10,i-1)) * i;	    	
	}
	return r;
}

/*
计算顺序字符串 "123456789101112" 中第i个数
*/
int getNum(unsigned long x){
	unsigned long t,i,p;
	int r;
	char str[10];	 
	t = 0;
	i = 1;
	while(t < x){
		t = bitCounts(i++);
	}
	i--;
	p = x - bitCounts(i-1);
	sprintf(str,"%ld",i);//数转化为字符串
	r = str[p-1] - '0';
	return r;
}

int getBit(unsigned long x){
	int t=1;
	while(x >= 10){
		x /= 10;
		t++;
	}
	return t;
}

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