码迷,mamicode.com
首页 > 其他好文 > 详细

(hdu step 4.1.5)find the nth digit(求S串中的第n个位置上是什么数字)

时间:2015-02-16 21:01:57      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:

题目:

find the nth digit

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 689 Accepted Submission(s): 244
 
Problem Description
假设:
S1 = 1
S2 = 12
S3 = 123
S4 = 1234
.........
S9 = 123456789
S10 = 1234567891
S11 = 12345678912
............
S18 = 123456789123456789
..................
现在我们把所有的串连接起来
S = 1121231234.......123456789123456789112345678912.........
那么你能告诉我在S串中的第N个数字是多少吗?
 
Input
输入首先是一个数字K,代表有K次询问。
接下来的K行每行有一个整数N(1 <= N < 2^31)。
 
Output
对于每个N,输出S中第N个对应的数字.
 
Sample Input
6
1
2
3
4
5
10
 
Sample Output
1
1
2
1
2
4
 
Author
8600
 
Source
HDU 2007-Spring Programming Contest - Warm Up (1)
 
Recommend
8600
 



题目分析:

                S串由1 12 123 1234 12345.....这样的序列组成。求S串中第n个位置上的数字是什么。这道题可以先确定第n个位置在第几数字串,然后在求在这个数字串中的第几个位置。



代码如下:

/*
 * e.cpp
 *
 *  Created on: 2015年2月16日
 *      Author: Administrator
 */

#include <iostream>
#include <cstdio>

using namespace std;


int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int n;
		scanf("%d",&n);

		int a = 1;//a表示当前串s的数字的个数
		while(n > a){//如果第n个位置不在当前串中
			n -= a;//则将n减去当前串所包含的数字的个数
			a++;//计算下一个串的数字的个数
		}

		//执行到这里的时候,说明第n个位置在当前串s中

		n %= 9;//因为每一个都是在串1~9之间循环
		if(n == 0){//用于处理一下n==9的时候
			n = 9;
		}

		printf("%d\n",n);
	}

	return 0;
}





(hdu step 4.1.5)find the nth digit(求S串中的第n个位置上是什么数字)

标签:

原文地址:http://blog.csdn.net/hjd_love_zzt/article/details/43854447

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!