The first question of the second online programming challenge monthly competition of the Ares cup: The number of replies

Source: Internet
Author: User

Question details:


Njzy learns the return string and then associates it with the return number. He wants to count all the return numbers in a range. Given a closed interval [a, B], calculate the number of replies in this interval.

For example, [20, 30], there is only one input number, which is 22.

 


Input description:

The input includes multiple groups of test data. Each group of test data includes two integers, A and B,

(0 <A <= B <10 ^ 6 ).

Output description:

Output the corresponding answer for each group of trial data.

 



Q & A description:


Input example:

1

10

20

30

300

400

Output example:

9

1

10


Solution:

Total [I] indicates the number of replies between 1 and I. After preprocessing, the number of replies between A and B is required, directly output total [B]-total [A-1. The first time this question was submitted, it was a tragedy because the array had crossed the border.

Const int maxn = 1000000 + 10; int total [maxn], after preprocessing the total array, from 1 to maxn, out of bounds, the maximum array subscript is maxn-1, ah, tragedy.

ASIDE: this month, I wrote very few blog posts and was busy reviewing the final exam. I had to raise my score.


Code:

# Include <iostream> # include <string. h> using namespace STD; const int maxn = 1000000; int num [10]; int total [maxn + 10]; // total [I] indicates the number of replies between 1 and I. bool Pd (INT N) // determines whether it is the number of replies {int K = 0; int temp = N; while (temp) {num [++ K] = TEMP % 10; temp/= 10;} For (INT I = 1; I <= K/2; I ++) {If (Num [I]! = Num [k-I + 1]) return false;} return true;} void CAL (int n) // preprocessing total [I] {int K = 0; for (INT I = 1; I <= N; I ++) {If (Pd (I) K ++; Total [I] = K ;}} int main () {total [0] = 0; CAL (maxn); int A, B; while (CIN> A> B) {cout <total [B]-total [A-1] <Endl;} return 0 ;}

Later, I can see on the Internet whether it is a better function for people to infer the number of replies:

For example:

Bool Pd (int n) // infer whether it is the number of input files {int temp = N; int m = 0; while (temp) {M = m * 10 + TEMP % 10; // The Reverse Order of M is n temp/= 10;} return n = m ;}


The first question of the second online programming challenge monthly competition of the Ares cup: The number of replies

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.