10.15
1.20249.92.9.103.9.114.9.125.9.136.9.187.9.198.9.209.9.2310.9.2411.9.2512.9.2613.9.2714.9.2815.10.14
16.10.15
17.10.1618.10.1719.10.1820.10.2021.10.2122.MySQL DQL 10.2223.MySQL事件24.MySQL DDL操作数据库笔记25.MySQL DDL操作表26.MySQL DML操作数据27.MySQL 约束28.Mybatis修改29.Mybatis添加&修改30.MyBatis查询31.MySQL 多表查询32.JavaScript 对象33.JavaScript基础语法34.JavaScript引入方式35.CSS36.HTML标签37.HTML介绍&快速入门38.Mybatis参数传递&注解开发39.JavaScript BOM40.JavaScript DOM41.JavaScript 事件监听42.JavaScript 正则表达式43.Tomcat44.Servlet入门45.Servlet生命周期与方法46.Jsp if判断47.《程序员修炼之道:从小工到专家》读书笔记——148.《程序员修炼之道:从小工到专家》读书笔记——249.《程序员修炼之道:从小工到专家》读书笔记——350.《程序员修炼之道:从小工到专家》读书笔记——451.《程序员修炼之道:从小工到专家》读书笔记——552.《程序员修炼之道:从小工到专家》读书笔记——653.《程序员修炼之道:从小工到专家》读书笔记——754.《程序员修炼之道:从小工到专家》读书笔记——855.《程序员修炼之道:从小工到专家》读书笔记——956.自动生成序号函数57.用户修改密码(前端与后端)58.案例:请假条管理系统2024-11-22今天完成了对于回文数的计算
只是使用栈:
点击查看代码
#include<iostream>
#include<stack>
using namespace std;
bool isPalindrome(int num) {
// 将负数排除,因为负数不可能是回文数
if (num < 0) return false;
stack<int> stack;
int original = num;
int reversed = 0;
// 逐位将数字的前半部分压入栈中
while (num > 0) {
stack.push(num % 10);
num /= 10;
}
// 逐位比较栈顶的数字与原数
num = original; // 重置num为原始数字
while (!stack.empty()) {
int top = stack.top();
if (num % 10 != top) return false; // 如果不相等,则不是回文数
stack.pop();
num /= 10;
}
return true; // 全部数字相等,则为回文数
}
int main() {
int number;
cout << "请输入一个数字: ";
cin >> number;
if (isPalindrome(number)) {
cout << number << " 是回文数\n";
} else {
cout << number << " 不是回文数\n";
}
return 0;
}
点击查看代码
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
bool isPalindrome(int num) {
// 负数不可能是回文数
if (num < 0) return false;
stack<int> stack;
queue<int> queue;
// 提取每一位并分别存入栈和队列
int original = num;
while (num > 0) {
int digit = num % 10;
stack.push(digit);
queue.push(digit);
num /= 10;
}
// 比较栈和队列的每一位
while (!stack.empty() && !queue.empty()) {
if (stack.top() != queue.front()) {
return false; // 如果栈顶和队首元素不相等,则不是回文数
}
stack.pop();
queue.pop();
}
return true; // 所有位都相等,则为回文数
}
int main() {
int number;
cout << "请输入一个数字: ";
cin >> number;
if (isPalindrome(number)) {
cout << number << " 是回文数\n";
} else {
cout << number << " 不是回文数\n";
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具