C language classic algorithm example 4: judging the number of palindrome

insert image description here

1. Problem description

Judging the number of palindrome
The description of the problem
is shown in the following points

  1. "Palindrome" refers to a sentence that can be read both forward and backward. It is a rhetorical method and word game that have existed in ancient and modern China and abroad, such as "I am for everyone, and everyone is for me" and so on.
  2. In mathematics, there is also such a class of numbers that have such characteristics, called palindrome numbers.
  3. Suppose n is an arbitrary natural number, if the natural number n1 obtained by reversely arranging the digits of n is equal to n, then n is called a palindromic number.
  4. For example, if n=1234321, then n is called a palindromic number; but if n=1234567, then n is not a palindromic number.
  5. A palindromic number is the same number looking forward and backward.
  6. Determine whether the d-base representation of a positive integer n is a palindrome.

Notice:

  1. An even number of numbers also has a palindromic number 124421
  2. Decimals without palindromic numbers

Among them, the number of palindrome to be used in this article is shown in the following function.

  • A palindromic number is the same number looking forward and backward.
  • Determine whether the d-base representation of a positive integer n is a palindrome.

2. Algorithm example compilation environment

The compilation environment of the C language classic algorithm example in this article uses the integrated development environment: Visual Studio 2019
insert image description here

The link to the official website of Visual Studio 2019 is as follows

Visual Studio 2019 official website link
insert image description here

The features of the Visual Studio 2019 integrated development environment are

    1. Visual Studio 2019 installs the Live Share code collaboration service by default.
    1. New welcome window to help users write code quickly, improved search functionality, general performance improvements.
    1. Visual Studio IntelliCode AI Help.
    1. Better Python virtualization and Conda support.
    1. And support for .NET Core 3.0 projects including WinForms and WPF, etc.

3. The implementation process of the algorithm example

3.1, including the header file

Include the header file code as follows

#pragma once

// 包含头文件
#include <stdio.h>
#include <stdlib.h>

  • Include the C language header files to be used.

3.2. Declare the array and initialize it

Declare the array and initialize the code as follows

int num[] = {
    
     232,27,851, 12321};			// 定义输入数字变量的数组。
int scale[] = {
    
     2,10,16 };					// 定义输入进制变量的数组。
  • Defines an array num of input numeric variables.
  • Defines the array scale of the input base variable.

3.3. Declare related variables

The code for declaring related variables is as follows

	int i, j;           // 声明相关的变量
  • Declare the relevant variables i, j;

3.4. Declare function

The declaration function code is as follows

/// <summary>
/// 判断数字n是否是输入d进制的回文数
/// </summary>
/// <param name="n">数字n</param>
/// <param name="d">d代表进制, 若d = 10 , 则数字n是十进制</param>
/// <returns>如果数字n是d进制的回文数, 则返回1</returns>
int circle(int n, int d);

  • It can be judged whether the number n is a palindrome number entered in base d.
  • d stands for base, if d = 10, the number n is decimal.
  • Returns 1 if the number n is a palindromic number in base d.

insert image description here

3.5. Function definition

The definition code of the function is as follows

/// <summary>
/// 判断数字n是否是输入d进制的回文数
/// </summary>
/// <param name="n">数字n</param>
/// <param name="d">d代表进制, 若d = 10 , 则数字n是十进制</param>
/// <returns>如果数字n是d进制的回文数, 则返回1</returns>
int circle(int n, int d)
{
    
    
    int s = 0, m = n;

    while (m)
    {
    
    
        s = s * d + m % d;
        m /= d;
    }

    return s == n;
}
  • Define the declaration of the function to realize the specific function of the function.
  • Determine whether the number n is a palindrome number entered in base d.
  • d stands for base, if d = 10, the number n is decimal.
  • Returns 1 if the number n is a palindromic number in base d.

3.6. Traverse the numbers in the array to judge whether they are palindrome numbers

The code for traversing the numbers in the array and judging whether they are palindrome numbers is as follows.

	/// <summary>
    /// 遍历数组中的数字, 进行是否是回文数的判断
    /// </summary>
    /// <returns>无</returns>
    for (i = 0; i < sizeof(num) / sizeof(num[0]); i++)
    {
    
    
        for (j = 0; j < sizeof(scale) / sizeof(scale[0]); j++)
        {
    
    
            if (circle(num[i], scale[j]))       // 调用circle函数, 对数组中的存储的数字进行回文数的判断。
            {
    
    
                printf("%d --> (%d) 进制是回文数\n", num[i], scale[j]);
            }
            else
            {
    
    
                printf("%d --> (%d) 进制不是回文数\n", num[i], scale[j]);
            }
        }
        printf("\n");
    }
    printf("\n");
  • Traverse the numbers in the array to judge whether they are palindrome numbers.
  • Output whether the numbers in the array are palindrome numbers.

Press F5 to compile, and the debugging results are as follows.

insert image description here

4. The complete code of the classic algorithm example program

The complete code of the classic algorithm example program is as follows

4.1, main.h file

#pragma once

// 包含头文件
#include <stdio.h>
#include <stdlib.h>

int num[] = {
    
     232,27,851, 12321};			// 定义输入数字变量的数组。
int scale[] = {
    
     2,10,16 };					// 定义输入进制变量的数组。

/// <summary>
/// 判断数字n是否是输入d进制的回文数
/// </summary>
/// <param name="n">数字n</param>
/// <param name="d">d代表进制, 若d = 10 , 则数字n是十进制</param>
/// <returns>如果数字n是d进制的回文数, 则返回1</returns>
int circle(int n, int d);

insert image description here

4.2, main.c file

#define _CRT_SECURE_NO_WARNINGS

#include "Main.h"


/// <summary>
/// 主函数
/// </summary>
/// <returns>返回0</returns>
int main()
{
    
    
    system("color 3E");

    
    int i, j;           // 声明相关的变量

    /// <summary>
    /// 遍历数组中的数字, 进行是否是回文数的判断
    /// </summary>
    /// <returns>无</returns>
    for (i = 0; i < sizeof(num) / sizeof(num[0]); i++)
    {
    
    
        for (j = 0; j < sizeof(scale) / sizeof(scale[0]); j++)
        {
    
    
            if (circle(num[i], scale[j]))       // 调用circle函数, 对数组中的存储的数字进行回文数的判断。
            {
    
    
                printf("%d --> (%d) 进制是回文数\n", num[i], scale[j]);
            }
            else
            {
    
    
                printf("%d --> (%d) 进制不是回文数\n", num[i], scale[j]);
            }
        }
        printf("\n");
    }
    printf("\n");

    system("pause");
    return 0;
}

/// <summary>
/// 判断数字n是否是输入d进制的回文数
/// </summary>
/// <param name="n">数字n</param>
/// <param name="d">d代表进制, 若d = 10 , 则数字n是十进制</param>
/// <returns>如果数字n是d进制的回文数, 则返回1</returns>
int circle(int n, int d)
{
    
    
    int s = 0, m = n;

    while (m)
    {
    
    
        s = s * d + m % d;
        m /= d;
    }

    return s == n;
}

insert image description here

V. Summary

The C language classic algorithm example in this article: find the maximum and minimum values ​​of a two-dimensional array, the goal to be achieved is as follows

  • A palindromic number is the same number looking forward and backward.
  • Determine whether the d-base representation of a positive integer n is a palindrome.

insert image description here

The article ends here.
I hope that the C language classic algorithm example 4 in this article: judging the palindrome number.
It can inspire your love for C language and algorithm learning.

  • Your support is my greatest encouragement.

Guess you like

Origin blog.csdn.net/m0_47419053/article/details/126764924