队列解

回文数

别说谁变了你拦得住时间么 提交于 2020-12-22 06:57:08
/* 文件名称: 回文数2 -- 双端队列解决 创建日期: 2013/3/29 */ #include <iostream> using namespace std; const int maxNum = 111; int main() { //遍历查找回文数 for (int n = 11; n <= maxNum; n++) { //定义双端队列, 队尾指针, 队头指针 int a[10] = {0}, rear = 0, head = 0; int base = 1, m = n * n; while (m / base) { //将数据按位分解,并结果将数据压入队尾 a[rear] = (m %(base * 10) - m % base) / base; rear++; base *= 10; } //判断是否为回文数 while (a[rear - 1] == a[head]) { rear--;//队尾出列 head++;//队头出列 } //打印输出 if (rear < head) cout << n << "\t" << n * n << endl; } return 0; } 来源: oschina 链接: https://my.oschina.net/u/862164/blog/121812