我不是已经给你思路了吗,就把他封装成一个方法被,不自己思考可不是好习惯
好吧,代码如下- using System;
- using System.Collections.Generic;
- namespace 输出不同数字
- {
- class Program
- {
- public static List<string> ShuZiZuHe(int zuixiaozhi, int zuidazhi){
- List<string> suoyoushuzi = new List<string>();
- for (int i = zuixiaozhi; i <= zuidazhi; i++) {
- //第二位数字
- for (int a = zuixiaozhi;a <= zuidazhi; a++) {
- if (a==i) {
- continue;
- }
- //第三位数字
- for (int b = zuixiaozhi; b <= zuidazhi; b++) {
- if (b==a || b == i) {
- continue;
- }
- suoyoushuzi.Add(String.Format("{0}{1}{2}",i,a,b));
- }
-
- }
-
- }
- return suoyoushuzi;
- }
- public static void Main(string[] args)
- {
- var suoyou = ShuZiZuHe(1,4); //调用ShuZiZuHe方法获取所有符合规则的数字组合
- foreach (var element in suoyou) {
- //遍历所有的数字组合,把他们输出出来
- Console.WriteLine(element);
- }
- Console.ReadKey();
- }
- }
- }
复制代码 |