算法入门经典习题2-10
2015-08-24 20:58
267 查看
算法入门经典习题2-10,在三位数中找这样的三位数,一共10个不同数字,构成每位数字都不相同的三位数。令其为a,b,c.使的a:b:c =1:2:3
我的方法:暴力求解法
然后就是对 倍数为1的那个三位数进行穷举,
for(i=102,i<329;i++) //原因数学上可以证明
temp1 = 2*i;temp2=3*i;
这样temp1,temp2就保存了题干中b,c的值,然后利用前面写的函数判断他们三个是否每位都不同即可
附上源代码:
附上运行结果:
我的方法:暴力求解法
写了三个函数, istrue() ----->判断一个三位数各位是否相同,不相同返回1 istrue1()------>判断一个数与一个三位数的各位是否相同,不相同返回1 istrue2()------>判断两个三位数各位是否相同,不相同返回1
然后就是对 倍数为1的那个三位数进行穷举,
for(i=102,i<329;i++) //原因数学上可以证明
temp1 = 2*i;temp2=3*i;
这样temp1,temp2就保存了题干中b,c的值,然后利用前面写的函数判断他们三个是否每位都不同即可
附上源代码:
#include <iostream> #define A 1 using namespace std; int istrue(int num){ //函数 int a,b,c; a = num/100; c =num%10; b = (num%100)/10; if(a!=b &&a!=c &&b!=c) return 1; return 0; } int istrue1(int k,int num){ //函数 int a,b,c; a = num/100; c =num%10; b = (num%100)/10; if(k!=a &&k!=b&& k!=c) return 1; return 0; } int istrue2(int num1,int num2){ //函数 int a,b,c; a = num1/100; c =num1%10; b = (num1%100)/10; if(istrue1(a,num2)==1 && istrue1(b,num2)==1 &&istrue1(c,num2)==1) return 1; return 0; } void main() { #if A int temp1,temp2,flag=0; for(int i=102;i<=329;i++){ //穷举模块 temp1 = 2*i; temp2 = 3*i; if(istrue(temp1)==1 && istrue(temp2)==1){ //判断temp1,temp2各位都不一样 if(istrue2(temp1,temp2)==1 && istrue2(temp1,i)==1 && istrue2(temp2,i)==1) cout<<i<<" "<<temp1<<" "<<temp2<<endl; flag =1; } } if(flag) return; cout<<"not find"<<endl; #endif }
附上运行结果:
相关文章推荐
- 单例和多例
- EditView的属性
- 强连通分量问题
- FZU 1686 神龙的难题 (DLX重复覆盖)
- 欢迎使用CSDN-markdown编辑器
- java中的异常机制(编译时异常)
- 在AndroidStudio不能找到so文件问题:couldn't find libweibosdkcore.so
- 【cocos2d-x 3.7 飞机大战】 决战南海I (七) 控制器的实现
- Native和H5两种情况的头像上传
- 别等待-想到什么就去做
- 关于Qt几个问题的答案
- Linux系统启动流程
- Android控件之ImageView
- UIScorllView和UIPageController的区别和实现图片轮播
- 套接字编程简介
- xml格式保存信息到文件
- 【cocos2d-x 3.7 飞机大战】 决战南海I (六) 保存玩家数据
- 关于矩阵的一些问题
- sping boot demo解释
- Chapter 2: Design the user experience