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
Windows 11 系统下的五款最佳免费 C++ 编译器推荐Windows 11 系统下的五款最佳免费 C++ 编译器推荐Apr 23, 2023 am 08:52 AM

C++是一种广泛使用的面向对象的计算机编程语言,它支持您与之交互的大多数应用程序和网站。你需要编译器和集成开发环境来开发C++应用程序,既然你在这里,我猜你正在寻找一个。我们将在本文中介绍一些适用于Windows11的C++编译器的主要推荐。许多审查的编译器将主要用于C++,但也有许多通用编译器您可能想尝试。MinGW可以在Windows11上运行吗?在本文中,我们没有将MinGW作为独立编译器进行讨论,但如果讨论了某些IDE中的功能,并且是DevC++编译器的首选

C++报错:变量未初始化,应该如何解决?C++报错:变量未初始化,应该如何解决?Aug 21, 2023 pm 10:01 PM

在C++程序开发中,当我们声明了一个变量但是没有对其进行初始化,就会出现“变量未初始化”的报错。这种报错经常会让人感到很困惑和无从下手,因为这种错误并不像其他常见的语法错误那样具体,也不会给出特定的代码行数或者错误类型。因此,下面我们将详细介绍变量未初始化的问题,以及如何解决这个报错。一、什么是变量未初始化错误?变量未初始化是指在程序中声明了一个变量但是没有

C++编译错误:未定义的引用,该怎么解决?C++编译错误:未定义的引用,该怎么解决?Aug 21, 2023 pm 08:52 PM

C++是一门广受欢迎的编程语言,但是在使用过程中,经常会出现“未定义的引用”这个编译错误,给程序的开发带来了诸多麻烦。本篇文章将从出错原因和解决方法两个方面,探讨“未定义的引用”错误的解决方法。一、出错原因C++编译器在编译一个源文件时,会将它分为两个阶段:编译阶段和链接阶段。编译阶段将源文件中的源码转换为汇编代码,而链接阶段将不同的源文件合并为一个可执行文

如何优化C++开发中的文件读写性能如何优化C++开发中的文件读写性能Aug 21, 2023 pm 10:13 PM

如何优化C++开发中的文件读写性能在C++开发过程中,文件的读写操作是常见的任务之一。然而,由于文件读写是磁盘IO操作,相对于内存IO操作来说会更为耗时。为了提高程序的性能,我们需要优化文件读写操作。本文将介绍一些常见的优化技巧和建议,帮助开发者在C++文件读写过程中提高性能。使用合适的文件读写方式在C++中,文件读写可以通过多种方式实现,如C风格的文件IO

C++编译错误:无法为类模板找到实例化,应该怎么解决?C++编译错误:无法为类模板找到实例化,应该怎么解决?Aug 21, 2023 pm 08:33 PM

C++是一门强大的编程语言,它支持使用类模板来实现代码的复用,提高开发效率。但是在使用类模板时,可能会遭遇编译错误,其中一个比较常见的错误是“无法为类模板找到实例化”(error:cannotfindinstantiationofclasstemplate)。本文将介绍这个问题的原因以及如何解决。问题描述在使用类模板时,有时会遇到以下错误信息:e

iostream头文件的作用是什么iostream头文件的作用是什么Mar 25, 2021 pm 03:45 PM

iostream头文件包含了操作输入输出流的方法,比如读取一个文件,以流的方式读取;其作用是:让初学者有一个方便的命令行输入输出试验环境。iostream的设计初衷是提供一个可扩展的类型安全的IO机制。

c++数组怎么初始化c++数组怎么初始化Oct 15, 2021 pm 02:09 PM

c++初始化数组的方法:1、先定义数组再给数组赋值,语法“数据类型 数组名[length];数组名[下标]=值;”;2、定义数组时初始化数组,语法“数据类型 数组名[length]=[值列表]”。

C++中的信号处理技巧C++中的信号处理技巧Aug 21, 2023 pm 10:01 PM

C++是一种流行的编程语言,它强大而灵活,适用于各种应用程序开发。在使用C++开发应用程序时,经常需要处理各种信号。本文将介绍C++中的信号处理技巧,以帮助开发人员更好地掌握这一方面。一、信号处理的基本概念信号是一种软件中断,用于通知应用程序内部或外部事件。当特定事件发生时,操作系统会向应用程序发送信号,应用程序可以选择忽略或响应此信号。在C++中,信号可以

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment