第11周 项目6-回文.素数(4)
来源:互联网 发布:阿里巴巴上传宝贝软件 编辑:程序博客网 时间:2024/06/06 18:05
/* * Copyright (c) 2014, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作 者:呼亚萍 * 完成日期:2014年 11 月 10 日 * 版 本 号:v1.0 * * 问题描述:编制main函数,完成:输出1000以内的所有素数,输出1000以内的所有回文数,输出1000以内的所有回文素数;输出1000以内的所有可逆素数 * 输入描述:相应的程序 * 程序输出:输出1000以内被要求的数字 */<p>#include <iostream>#include<cmath>using namespace std;int reverse (int );bool isPrimer(int );bool ispalindrome(int );int main(){</p><p> int n; cout<<"(1)输出1000以内的所有素数:"<<endl; for (n=2; n<1000; ++n) { if(isPrimer(n)) cout <<n<<" "; } cout<<endl; int m; cout<<"(2)输出1000以内的所以回文数:"<<endl; for(m=2; m<1000; ++m) { if(ispalindrome(m))</p><p> cout <<m<<" ";</p><p> } cout <<endl; int q; cout<<"输出1000以内的所有回文素数:"<<endl; for (q=2; q<1000; ++q) { if (ispalindrome(q)&&isPrimer(q)) cout <<q<<" "; } cout <<endl; cout<<"输出1000以内的所有可逆素数:"<<endl; for (q=2; q<1000; ++q) { if (ispalindrome(q)&&isPrimer(reverse(q))) cout<<q<<" "; } cout<<endl;</p><p>return 0;}</p><p>#include<cmath>bool isPrimer(int n){ bool Primer=true; int k=int(sqrt(n)); for(int i=2; i<=k; i++) { if(n%i==0) { Primer=false; break; } } return Primer;}</p><p>bool ispalindrome (int m){ bool ispalindrome=false; if(m==reverse(m)) ispalindrome=true; return ispalindrome;}int reverse (int x){ int m=0; while ( x>0) { m=m*10+x%10; x=x/10; } return m;}</p>
运算结果:
知识点总结:
对于多个运算要求相同时,可输入一个int,调用函数可以嵌套使用。
学习心得:
在别人的程序中,发现自己的错误。
0 0
- 第11周 项目6-回文.素数(4)
- 第11周 项目6-回文.素数(4)
- 第11周项目6-回文、素数-回文素数
- 第11周项目6-回文,素数
- 第11周项目6-回文、素数
- 第11周项目6:回文,素数
- 第11周项目6-回文素数
- 第11周项目6回文数、素数(4、输出1000以内所有的素数)
- 第11周项目6(4-4)-回文素数
- 第11周项目6-回文、素数 回文数
- 第11周项目6-回文、素数-回文数
- 第11周项目6(4-1)-回文素数
- 第11周项目6(4-2)-回文素数
- 第11周项目6(4-3)-回文素数
- 第11周项目6-4:求解回文素数
- 第11周项目六 回文~素数(6)
- 第11 周 项目6-回文.素数(1)
- 第11周 项目6-回文.素数(2)
- Android activity第一次启动Edittext不弹软键盘
- 紐巴倫 tcgk OZo tBPdb
- Windows下Git Bash中文乱码
- 今天用了一天实现了 java中的简易的堆
- 基于PHP的crontab定时任务管理
- 第11周 项目6-回文.素数(4)
- QT--QSocketNotifier类介绍
- 11-11前的电商
- 【Leetcode】Valid Sudoku in JAVA
- swap的使用技巧
- Linux系统下面的交叉编译浅析
- 简单记录下document.write() 和 $().html()的区别
- linklist----reverse
- valid parentheses