search
HomeBackend DevelopmentC#.Net TutorialThree algorithms for finding palindrome numbers in C language

The article shared by the editor today is three algorithms for describing palindrome numbers in C language. It has certain reference value. If you are interested in palindrome numbers in C language, you can take a look. I hope it will be helpful to you.

Title description

  • Note: (These palindrome numbers do not have leading 0)
  • The 1-digit palindrome number has 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 in total;
  • 2-digit palindrome numbers are 11, 22, 33, 44, 55, 66, 77, 88, 99 9 in total;

* I would like to ask: How many n-digit palindromes are there? Please write a recursive function to solve this problem! ! !

  • [Input format] One positive integer per line, representing the number of digits
  • [Output format] One positive integer per line, representing the number of palindrome poems
  • [ Sample input】2
  • 【Sample output】9

Three algorithms for finding palindrome numbers in C language

##Input:

3
Output:
90

Input:

5
Output:
900

** Input:

10
Output:
90000 **

Input:

8
Output:
9000

Input:

1
Output:
10

Idea analysis

    Read this number through a for loop,
  1. Reverse the data through / and % operations, and then compare whether the reversed number is equal to the original number
  2. Three algorithms for finding palindrome numbers in C language

  3. Read this number through a for loop,
  4. take the first number and the last number each time, and compare the two numbers in turn to see if they are equal. , then remove these two numbers until there is one number left (an odd number of digits) or two numbers (an even number of digits)
  5. Three algorithms for finding palindrome numbers in C language

  6. Through

    mathematical relations, directly determine the number of digits and calculate the number of palindromes within this digit;

      For example: 99899
    • can be divided into two halves, take the first half 998, if it is a palindrome number, the second half must correspond to its corresponding position, 998 is a 3-digit word, ** Except for the first bit (excluding leading 0), which number has 9 choices (1-9) for the position corresponding to the second half, the other digits have 10
      choices (0-9) for the corresponding positions* *, for example, the second digit and the penultimate digit (0-9)
    • So the same number of digits can be summarized. If the number of digits is an odd number, the palindrome number is 9*10^(n/2) , note that n/2 is an integer, and the number of digits with an even number is
    • 9
      10^(n/2-1), so the palindrome number of 5-digit numbers is 910*10 =900
    • Note that there are 10 digits (0-9) for 1, which require special processing
Related tutorials:

C Video Tutorial

Code Description

1. 第一种思路:
#include <stdio.h>
#include <math.h>
int reverse(long int i,long int *terminate)        //递归函数求数值的逆序
{
    if (i<=0){              //递归出口
        return 1;       
    }
    else{
        *terminate*=10;     //每次乘10升位数
        *terminate+=i%10;      //加上个位
        reverse(i/10,terminate);        //递归每次规模缩小
    }
    return 1;
}
int main ()
{
    int n;
    scanf ("%d",&n);            //读入一个n,表示n位整数
   long int i;        
    int count=0;
    if (n==1){               //如果等于1,则有10个(0-9都是),特殊处理;
        printf ("10");
        return 0;
    }
    for (i=pow(10,n-1);i<pow(10,n);i++){       //从第一个n位数开始(10^(n-1)),到(10^n)-1
       long int terminate=0;                //定义一个逆序目标数
        reverse(i,&terminate);              //把i和逆序目标数传入
        if (terminate==i){                  //逆序后还和原数相等,则可计数
            count++;
        }
    }
    printf ("%d",count);        //输出个数
    return 0;
}

2. 第二种思路:
#include <stdio.h>
#include <math.h>
int judge(int i,int n)
{
    int first,last;
    if (n<=1){          //规模减小,直到n为1(偶数)或者0
        return 1;

    }
    else{
        first=i/pow(10,n-1);        //头位数字
        last=i%10;                  //末位数字
        if (first!=last){           //头位末尾不一样直接退出
            return 0;
        }
        int tem=pow(10,n-1);        
    judge(i%tem/10,n-2);            //剔除头尾剩下中间,位数减二

    }
}
int main ()
{
    int n;
    scanf("%d",&n);
    if (1==n){
        printf ("10");
        return 0;
    }
    int i;
    int count=0;
   long long  low=pow(10,n-1);      //循环入口
    long long high=pow(10,n);       //循环出口
    for (i=low;i<high;i++){
       if ( judge(i,n)==1){         //判断i是否为回文,计数
           count++;
       }
    }
    printf ("%d",count);
    return 0;
}

3. 第三种思路:
#include <stdio.h>
#include <math.h>
int main (){
    int n;
    scanf ("%d",&n);
    int ji=9*pow(10,n/2),ou=9*pow(10,n/2-1);
    if (n==1){
        printf ("10");
    }
    else if  (n==2){
        printf ("%d",9);
    }
    else if (n%2==1){
        printf ("%d",ji);
    }
    else if (n%2==0){
        printf("%d",ou);
    }
    return 0;
}

The above is the detailed content of Three algorithms for finding palindrome numbers in C language. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:思否. If there is any infringement, please contact admin@php.cn delete
C# and the .NET Runtime: How They Work TogetherC# and the .NET Runtime: How They Work TogetherApr 19, 2025 am 12:04 AM

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

C# .NET Development: A Beginner's Guide to Getting StartedC# .NET Development: A Beginner's Guide to Getting StartedApr 18, 2025 am 12:17 AM

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

C# and .NET: Understanding the Relationship Between the TwoC# and .NET: Understanding the Relationship Between the TwoApr 17, 2025 am 12:07 AM

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

The Continued Relevance of C# .NET: A Look at Current UsageThe Continued Relevance of C# .NET: A Look at Current UsageApr 16, 2025 am 12:07 AM

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

From Web to Desktop: The Versatility of C# .NETFrom Web to Desktop: The Versatility of C# .NETApr 15, 2025 am 12:07 AM

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C# .NET and the Future: Adapting to New TechnologiesC# .NET and the Future: Adapting to New TechnologiesApr 14, 2025 am 12:06 AM

C# and .NET adapt to the needs of emerging technologies through continuous updates and optimizations. 1) C# 9.0 and .NET5 introduce record type and performance optimization. 2) .NETCore enhances cloud native and containerized support. 3) ASP.NETCore integrates with modern web technologies. 4) ML.NET supports machine learning and artificial intelligence. 5) Asynchronous programming and best practices improve performance.

Is C# .NET Right for You? Evaluating its ApplicabilityIs C# .NET Right for You? Evaluating its ApplicabilityApr 13, 2025 am 12:03 AM

C#.NETissuitableforenterprise-levelapplicationswithintheMicrosoftecosystemduetoitsstrongtyping,richlibraries,androbustperformance.However,itmaynotbeidealforcross-platformdevelopmentorwhenrawspeediscritical,wherelanguageslikeRustorGomightbepreferable.

C# Code within .NET: Exploring the Programming ProcessC# Code within .NET: Exploring the Programming ProcessApr 12, 2025 am 12:02 AM

The programming process of C# in .NET includes the following steps: 1) writing C# code, 2) compiling into an intermediate language (IL), and 3) executing by the .NET runtime (CLR). The advantages of C# in .NET are its modern syntax, powerful type system and tight integration with the .NET framework, suitable for various development scenarios from desktop applications to web services.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.