(Hdu step 4.1.5) find the nth digit (find the number at the nth position in the S string), hdunth
Question:
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 hypothesis: S1 = 1 S2 = 12 S3. = 123 S4 = 1234 ......... S9 = 123456789 S10 = 1234567891 S11= 12345678912 ............ S18= 123456789123456789 .................. Now we connect all strings S = 1121231234 ...... 123456789123456789112345678912 ......... Can you tell me the nth number in the S string? |
The Input is a number K, which indicates K queries. Each row in the next K Rows has an integer N (1 <=n <2 ^ 31 ). |
For each N, Output the nth number in S. |
Sample Input61234510 |
Sample Output112124 |
Author8600 |
SourceHDU 2007-Spring Programming Contest-Warm Up (1) |
Recommend8600
|
Question Analysis:
The S string is composed of a sequence such as 1 12 123 1234 12345. Evaluate the number at the nth position in the S string. To solve this problem, first determine the nth position in the nth digit string, and then find the nth position in this digit string.
The Code is as follows:
/** E. cpp ** Created on: February 16, 2015 * 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 indicates the number of numbers in the current string s while (n> a) {// If the nth position is not in the current string n-=; // then subtract n from the number of digits contained in the current string a ++; // calculate the number of digits in the Next string} // when this is executed, meaning the nth position is in the current string s n % = 9; // because each is in the string 1 ~ Loop between 9 if (n = 0) {// used to process n = 9 n = 9;} printf ("% d \ n", n );} return 0 ;}