zoukankan      html  css  js  c++  java
  • 题目1 转

    1、有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。

    2、用1、2、2、3、4、5这六个数字,写程序打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"的右边不能是"5"。


    C# code

    public class Program
    {

    public static void Main()
    {
    List<int> list = new List<int>();
    //n=50
    for (int i = 1; i <=50; i++)
    {
    list.Add(i);
    }
    int index = 1;
    int under = 0;
    while (list.Count > 1)
    {
    Console.WriteLine(string.Format("{0}喊了{1}",list[under],index));
    if (index == 3)
    {
    Console.WriteLine(string.Format("{0}被弹出", list[under]));
    Console.Read();
    list.RemoveAt(under);
    under--;
    index = 0;
    }
    if (under == list.Count - 1) under = -1;
    index++;
    under++;

    }
    }
    }


    public class Program
    {

    public static void Main()
    {
    List<string> list = new List<string>();
    list.AddRange(GetString(new string[]{"1","2","2","3","4","5"}));
    list.Sort();//排序,可以看清楚有没有重复
    foreach (string s in list) {
    if (s.IndexOf("4") != 2)
    {
    if(s.IndexOf("35")==-1)
    Console.WriteLine(s);
    }
    }
    }
    static string[] GetString(string[] cs)
    {
    if (cs.Length == 2)
    {
    if (cs[0] == cs[1]) return new string[] { cs[0] + cs[1] };
    return new string[] { cs[0] + cs[1], cs[1] + cs[0] };
    }
    List<string> list = new List<string>();
    bool first = true;
    for (int i = 0; i < cs.Length; i++)
    {
    List<string> tmp = new List<string>(cs);
    tmp.RemoveAt(i);
    if (tmp.IndexOf(cs[i]) != -1 && first) { first = false; continue; }
    string[] sub = GetString(tmp.ToArray());
    List<string> tmp2 = new List<string>();
    foreach (string s in sub)
    {
    tmp2.Add(cs[i] + s);
    }
    list.AddRange(tmp2.ToArray());
    }
    return list.ToArray();
    }

    }

  • 相关阅读:
    js正则表达式中的问号使用技巧总结
    380. Insert Delete GetRandom O(1)
    34. Find First and Last Position of Element in Sorted Array
    162. Find Peak Element
    220. Contains Duplicate III
    269. Alien Dictionary
    18. 4Sum
    15. 3Sum
    224. Basic Calculator
    227. Basic Calculator II
  • 原文地址:https://www.cnblogs.com/liye/p/1713154.html
Copyright © 2011-2022 走看看