zoukankan      html  css  js  c++  java
  • 一道C#基础题,看你能多长时间做出来?

    这是一道java面试题,现用C#来实现。具体题目是这样的:给定6个数字1,2,2,3,4,5,要求找出所有这6个数字组合成的六位数,并满足
    (1)3和5不能相连
    (2)4不能在第三位
    给出你的算法。


    我是这样做的。
    using System;
    using System.IO;
    using System.Collections;
    using System.Collections.Generic;
    using System.Text;

    namespace ConsoleApplication1
    {
        
    class Program
        
    {
            
    static void Main(string[] args)
            
    {
                
    //char[] a ={ '1','2','2','3','4','5'};
                char[] a ='1','2','2','3','4','5'};
                
    int i = 0;
                IList result
    =Program.PaiLie(a);
                IList result2 
    = new ArrayList();

                
    //StreamWriter tw = new StreamWriter("c:\\out.txt");
                
    //Console.SetOut(tw);

                
    foreach (string str in result)
                
    {
                    
    if (str.Contains("35"|| str.Contains("53"|| str.IndexOf('4'== 3)
                    
    {
                        i
    ++;
                    }

                    
    else
                        result2.Add(str);
                    
    //Console.WriteLine(str);
                }

                
    foreach (string str2 in result2)
                
    {
                    Console.WriteLine(str2);
                }

                
                
    //tw.WriteLine("i = " + i);
                
    //tw.WriteLine("result:-- " + result.Count);
                
    //tw.WriteLine("Total:-- "+result2.Count);
                Console.WriteLine("i = " + i);
                Console.WriteLine(
    "result:-- " + result.Count);
                Console.WriteLine(
    "Total:-- "+result2.Count);
            }


            
    public static IList PaiLie(char[] a)
            
    {
                IList resultStr 
    =new ArrayList();
                
    //resultStr.Add(new string(a));
                int n = a.Length;
                
    if (n == 2)
                
    {
                    resultStr.Add(
    new string(a));
                    
    char temp=a[0];
                    a[
    0= a[1];
                    a[
    1= temp;
                    resultStr.Add(
    new string(a));
                }


                
    if (n > 2)
                
    {
                    
    for (int i = 0; i < n; i++)
                    
    {
                        
    string b = new string(a);
                        b
    =b.Remove(i, 1);
                        
    char[] c = b.ToCharArray();
                        IList tempStr 
    = PaiLie(c);
                        
    foreach (string s in tempStr)
                        
    {
                            
    string xx = a[i].ToString()+s;
                            resultStr.Add(xx);
                        }

                    }

                }


                
    return resultStr;
            }

        }

    }

    去掉部分注释,可以导出到文件。

    大家可以给出更好的算法啊,多提宝贵意见!
    作者:Jackhuclan
    出处:http://jackhuclan.cnblogs.com/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    day67——前后端传输数据的编码格式、ajax传json数据/传文件、批量插入
    day66——choices参数、MTV/MVC模型、三种创建多对多的方式、AJAX
    day65——聚合函数、分组查询、F与Q查询、django开事务、orm查询优化
    dayⅢ、基本数据类型+运算符作业
    dayⅡ:编程语言+变量+垃圾回收制
    dayⅡ:变量作业
    dayⅠ:计算机基础知识
    ⅩⅥ:无参装饰器
    ⅩⅤ:作业
    ⅩⅤ:名称空间与作用域
  • 原文地址:https://www.cnblogs.com/jackhuclan/p/864552.html
Copyright © 2011-2022 走看看