Three algorithms for finding palindrome numbers in C language

little bottle
Release: 2019-04-30 09:31:25
forward
10481 people have browsed it

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. 第一种思路:
Copy after login
#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;
}
Copy after login

2. 第二种思路:
Copy after login
#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;
}
Copy after login

3. 第三种思路:
Copy after login
#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;
}
Copy after login

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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template