编程语言
首页 > 编程语言> > C#练习题答案: 滑稽的算法任务【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

C#练习题答案: 滑稽的算法任务【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

作者:互联网

滑稽的算法任务【难度:2级】:

答案1:

using System;
using System.Linq;

public class NumbersFinder
{
    public static int Generator(int size, int position)
    {
      if (position < 1 || position > Math.Pow(size, size)) return -1;
      var sum = "";
      position--;
      for (var i = 0; i < size; i++)
      {
        sum = ((position % size) + 1).ToString() + sum;
        position /= size;
      }
      return int.Parse(sum);
    }
}​

答案2:

using System;
using System.Collections.Generic;


public class NumbersFinder
{
    public static int Generator(int size , int position){
        if ( Math.Pow( size , size ) < position || position < 1 )
            return -1;
        List<string>  arr = new List<string>();
        for (int i = 0; i < size; ++i)
            arr.Add((i+1).ToString());
        List<string>  newArr;
        while (arr.Count != Math.Pow(size,size)){
            newArr = new List<string>();
            for (int j = 0; j < arr.Count; j++)
                for (int i = 0; i < size; i++)
                    newArr.Add(arr[j]+(i+1));
            arr.Clear();
            arr.AddRange(newArr);
        }
        return Convert.ToInt32(arr[position - 1]);
    }
}​

答案3:

using System;
public class NumbersFinder
{
    public static int Generator(int n , int m){
       if(n==1)return m==1?1:-1;
       if(m>Math.Pow(n,n)||m<1)return -1;
       m--;
       string r="";
       for(int p=(int)Math.Pow(n,n-1);p>0;p/=n){
         r+=m>=p?m/p+1:1;
         m=m%p;
       }
       return int.Parse(r);
    }
}​

答案4:

using System;

public class NumbersFinder
{
    public static int Generator(int size, int position){       
        int f, i=0, c=(int) Math.Pow(10,size-1)-1;
        char t=(char) (size+48);
        string s="";
        while (i<position&amp;&amp;s.Length<=size) {
          s=++c+"";
          f=0;
          for (int j=0; j<size; j++) if (s[j]<'1'||s[j]>t) {f=1; break;}
          if (f==0) i++;
        }
        return s.Length==size?c:-1;
    }
}​

答案5:

public class NumbersFinder
{
  public static int Generator(int Q,int S)
  {
    int R = 0,C = 1,F = 0;
    for (--S;F++ < Q;C *= 10,S /= Q) R += C * (1 + S % Q);
    return 0 < S ? -1 : R;
  }
}​




标签:练习题,arr,return,景越,C#,int,position,public,size
来源: https://blog.csdn.net/weixin_45444821/article/details/101389793