(Hdu step 4.1.5) find the nth digit (find the number at the nth position in the S string), hdunth

Source: Internet
Author: User

(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 Input
61234510
 
Sample Output
112124
 
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 ;}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.