Java\Scala\Python三种语言求解经典编程实例(三)—— 不重复的3位数
2016-06-07 22:40
931 查看
相关文章推荐:
Java\Scala\Python三种语言求解经典编程实例(一)——求水仙花数
Java\Scala\Python三种语言求解经典编程实例(二)—— 尾数前移
配置参数文章:
快速配置Linux下的编程环境变量
安装Python交互式数据处理工具——IPython
Linux下创建并执行python脚本
Java\Scala\Python三种语言求解经典编程实例(三)—— 不重复的3位数
需求:0~9这10个数字可以组成多少不重复的3位数?
1、Java求解
2、scala求解,增加的三层for循环
3、python求解,强大的for循环与列表操作
最后结果:
648
Java\Scala\Python三种语言求解经典编程实例(一)——求水仙花数
Java\Scala\Python三种语言求解经典编程实例(二)—— 尾数前移
配置参数文章:
快速配置Linux下的编程环境变量
安装Python交互式数据处理工具——IPython
Linux下创建并执行python脚本
Java\Scala\Python三种语言求解经典编程实例(三)—— 不重复的3位数
需求:0~9这10个数字可以组成多少不重复的3位数?
1、Java求解
public class Not3Repeat{ public static void main(String[] args){ int count = 0; for(int a = 1 ; a <= 9 ; a++){ for(int b = 0 ; b <= 9 ; b++){ if (a==b) continue; for(int c = 0; c <=9; c++){ if(c != a && c != b){ System.out.println(a+" " + b +" "+c); count++; } } } } System.out.println(count); } }
2、scala求解,增加的三层for循环
object Not3Repeat extends App{ var count = 0; for ( a <- 1 to 9; b <- 0 to 9 if a != b; c <- 0 to 9 if c != a && c != b){ println(a + " " + b+" " + c) count = count + 1 } println(count) }
3、python求解,强大的for循环与列表操作
#!/usr/bin/env python #coding:utf-8 #0~9这10个数字可以组成多少不重复的3位数? l = range(10) count = 0 for a in l[1:]: for b in l: if a == b: continue #过滤 a == b for c in l: if c != a and c != b: # 过滤 a == c, b == c print a,b,c count += 1 print 'count:',count
最后结果:
648
相关文章推荐
- Python 序列中的易错点
- python-day0003-搭建基于python的selenium自动化测试---元素定位
- leetcode 114. Flatten Binary Tree to Linked List-二叉树变链表|指针
- python
- Python日志模块logging
- Python基础之运算操作符总结
- 欢迎使用CSDN-markdown编辑器
- Win7 Python2.7 安装教程
- Python 格式化输出 —— %r 与 %s 的区别(__repr__ 与 __str__)
- Python(2)-第二天
- Python(1)-第一天
- Python 装饰器 学习笔记
- 实战 virtualenv 搭建多版本 Python 环境
- Python-GraphLab简单应用
- python 中list、tuple等有序序列
- Python正则表达式模块re
- python的多线程、多进程代码示例
- python的多线程、多进程代码示例
- filename.whl is not supported wheel on this platform
- python实现汉诺塔问题