Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C++ to realize palindromes

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use C++ to achieve palindromes". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn how to use C++ to achieve palindromes.

Topic description

Observation figures: 12321 123321 all share a common feature, whether reading from left to right or from right to left. Such numbers are called palindromes.

This question requires you to find some 5-or 6-digit decimal numbers. Meet the following requirements:

The sum of the digits of the number is equal to the integer entered.

Input

A positive integer n (10 < n < 100) that represents the sum of digits required to be satisfied.

Output

Several rows, each containing a 5-or 6-digit integer that meets the requirements.

The numbers are arranged in order from small to large.

If no condition is met, output:-1

Sample input 44 sample output 9989949945899855988956799888779676996777877787787796697859958868868877886688895598949949958859967776679985589994499:

The title requires that the sum of all digits of a 5-digit or 6-digit palindrome is a given number.

The first method: you can cycle through all the numbers, just traversing the first three digits, and the last two or three digits can be judged directly from the previous one.

The second method, thinking the same, use deep search to determine the first three digits, and then determine whether the sum on all bits is the given number.

Mark the number of eligible numbers, and if there is no qualified number, output-1.

AC code: # include const int N = 20 politics int a [N], n, temp;void dfs1 (int x) {if (xylene 4) {if ((a [1] + a [2]) * 2yoga [3] = = n) {temp++; printf ("% d%d%d%d%d\ n", a [1], a [2], a [3], a [2], a [1])) } return;} if (x = 1) {a [x] = 0; dfs1 (x = 1);} for (int iTunes 1; I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report