USACO-Section 1.2 Daul Palindromes[...]
来源:互联网 发布:禁锢网络剧百度云 编辑:程序博客网 时间:2024/06/07 06:02
2017-05-29
题目大意:
输入两个整数N,S(1<=N<=15,0<S<10 000),求从S+1起的前N个多(>=2)进制(二进制~十进制)下的回文数。 回文数是指从左到右和从右到左看起来一样的数,而且它的左右两端不能为数字0。比如33就是回文数,而0110则不是。有的数字在10进制下不是回文数,如26。但是26在3进制下为222,是回文数。
题解:
题目要求按从小到大的顺序输出前N个满足条件的十进制数,只要循环递增S的值,符合在两个进制下为回文数就将其输出即可。
代码如下:
/*ID: madara01PROG: dualpalLANG: C++*/#include <iostream>#include <fstream>#include <string>#define cin fin#define cout fout#define MAX 10using namespace std;bool isPalindrome(string s) //判断s表示的数是否为回文数{ int sl = s.length(); int l = sl/2 +1; for(int i = 0; i<l; i++) { if(s[i] != s[sl-i-1]) return false; } return true;}string astring(int a) { //把int转化成string -_-||因为提交编译错误,目前先这么吧... char c = a+'0'; string m; m = m + c; return m;}bool result(int number) //判断number是否为符合题意的解{ int base,ac = 0; //base表示进制,ac表示符合的进制数 int temp; string s; for(base = 2; base <= 10; base ++) { s = ""; temp = number; if(temp % base == 0) continue; //末端为'0'不可能是回文数 while(temp) { s = s + astring(temp%base); temp = temp / base; } if(isPalindrome(s)) ac++; if(ac >= 2) return true; } return false;}int main(int argc, char **argv){ int n,s; ofstream fout ("dualpal.out"); ifstream fin ("dualpal.in"); cin >> n >> s; s ++; //所求的解为大于s的数 while(n>0) { while(!result(s)) s++; cout << s << endl; n--; s++; } return 0;}
阅读全文
0 0
- USACO-Section 1.2 Daul Palindromes[...]
- USACO Section 1.2 Dual Palindromes
- USACO Section 1.2 Dual Palindromes
- USACO section 1.2 Dual Palindromes
- USACO Section 1.2 Dual Palindromes
- USACO section 1.2.4 Dual Palindromes
- USACO Section 1.2.5 Dual Palindromes
- USACO Section 1.5 Prime Palindromes
- USACO Section 1.5 Prime Palindromes
- USACO Section 1.5 Prime Palindromes
- USACO Section 1.5 Prime Palindromes
- USACO Training Section 1.2 Dual Palindromes 解题报告&AC代码
- USACO section 1.5.2 Prime Palindromes
- USACO section 1.5 Prime Palindromes(模拟)
- [USACO Section 1.5] Prime Palindromes (模拟)
- Section 1.2 Dual Palindromes
- Section 1.2 Dual Palindromes
- USACO 1.2 Dual Palindromes
- [教程] ListView的使用和封装——实战React Native中级案例01
- Python博客总结4
- poj 2104 <排序分块,区间第k大>/<第一次用主席树>2个方法
- Redis单机版与集群版的搭建
- 越简单越好:看看新QNX技术概念车--使用rightware的kanziUI设计
- USACO-Section 1.2 Daul Palindromes[...]
- UVA230解题报告
- 增加收入的 6 种方式
- 第34课: Stage划分和Task最佳位置算法源码彻底解密
- C#两个日期控件的比较,出现奇怪的现象
- Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
- 通达OA二次开发 小飞鱼应用OA工作流二次开发智能公文系统(二)(图文)
- Magento2开发文档教程
- Matrix Decompressing UVA