题目1434:蓝桥杯历届试题-回文数字

#include<iostream>

using namespace std;

//计算各位之和

int totalSum(int x){

int sum=0;

while(x>0){

sum+=x%10;

x/=10;

}

return sum;

}

//判断是否为回文数

bool isPolindromt(int x){

int orignal=x,reversed=0;

while(x>0){

reversed=reversed*10+x%10;

x/=10;

}

return orignal==reversed;

}

int main(){

int n;

cin>>n;

bool found=false;

for(int i=10000;i<=999999;i++){

if(isPolindromt(i)&&totalSum(i)==n){

cout<<i<<endl;

found=true;

}

}

if(!found){

cout<<-1<<endl;

}

return 0;

}

相关推荐
张李浩4 小时前
Leetcode 054螺旋矩阵 采用方向数组解决
算法·leetcode·矩阵
big_rabbit05025 小时前
[算法][力扣101]对称二叉树
数据结构·算法·leetcode
WolfGang0073215 小时前
代码随想录算法训练营 Day11 | 二叉树 part01
数据结构
美好的事情能不能发生在我身上5 小时前
Hot100中的:贪心专题
java·数据结构·算法
myloveasuka5 小时前
Java与C++多态访问成员变量/方法 对比
java·开发语言·c++
2301_821700535 小时前
C++编译期多态实现
开发语言·c++·算法
奥地利落榜美术生灬5 小时前
c++ 锁相关(mutex 等)
开发语言·c++
xixihaha13246 小时前
C++与FPGA协同设计
开发语言·c++·算法
小小怪7506 小时前
C++中的函数式编程
开发语言·c++·算法
xixixiLucky6 小时前
编程入门算法题---小明爬楼梯求爬n层台阶一共多少种方法
算法