| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
出了一个小问题,还请赐教我是在做1019题时遇到问题的。计算机判断时给我的结果是“Exceed Time Limit”,但我不觉得这个程序能执行1秒钟还完不成。但又不好测试(还不会仿真往计算机里输入和读取数据)。程序本身的结果经过测试应该是没问题的。希望好心的诸位提提意见,看看我哪里做的不对头? 谢谢!!
题目是:
Number Sequence
Time Limit:1000MS Memory Limit:10000K
Total Submit:1433 Accepted:322
Description
A single positive integer i is given. Write a program to find the digit located in the position i in the sequence of number groups S1S2...Sk. Each group Sk consists of a sequence of positive integer numbers ranging from 1 to k, written one after another.
For example, the first 80 digits of the sequence are as follows:
11212312341234512345612345671234567812345678912345678910123456789101112345678910
Input
The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by one line for each test case. The line for a test case contains the single integer i (1 ≤ i ≤ 2147483647)
Output
There should be one output line per test case containing the digit located in the position i.
Sample Input
2
8
3
Sample Output
2
2
Source
First Iran Nationwide Internet Programming Contest Problem D
===================================================================
我给出了程序:
#include <iostream>
using namespace std;
int GetDigitByPosition(int nPosition)
{
int i, nMax, nLength, nFactor, nDigit;
nMax = 1;
nFactor = 1000000;
while(true)
{
for(i = 1; i <= nMax; i++)
{
if(i < 10)
{
nLength = 1;
}
else if(i < 100)
{
nLength = 2;
}
else if(i < 1000)
{
nLength = 3;
}
else if(i < 10000)
{
nLength = 4;
}
else if(i < 100000)
{
nLength = 5;
}
else if(i < 1000000)
{
nLength = 6;
}
else
{
return -1;
}
if((nPosition - nLength) <= 0)
{
goto find;
}
else
{
nPosition -= nLength;
}
}
nMax++;
}
find:
while((i / nFactor) == 0)
{
nFactor /= 10;
}
while(true)
{
nDigit = i / nFactor;
if(nPosition == 1)
{
return nDigit;
}
else
{
nPosition--;
i %= nFactor;
nFactor /= 10;
}
}
}
int main()
{
int nNum = 0;
int i, nPosition[10], nDigit[10];
cin>>nNum; //load the number of test cases
for(i = 0; i < nNum; i++)
{
cin>>nPosition[i];
nDigit[i] = GetDigitByPosition(nPosition[i]);
}
//output the result
for(i = 0; i < nNum; i++)
{
cout<<nDigit[i]<<endl;
}
return 0;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator