完美世界2022秋招客户端笔试


单项选择题20题,多项选择10题,编程题共两题

编程题第一题:

0->a, 1->b, ..., 25->z

给你一串数字字符串返回该字符串的解码组合

例:

输入:"123"

输出:3

解析:三种组合方式

①.1、2、3 -> b、c、d

②.12、3 -> b、x

③.1、23 -> m、d

using System;
using System.Collections.Generic;

class wm01
{
    List<List<string>> result = new List<List<string>>();
    List<string> path = new List<string>();
    public int solution(string s)
    {

        BackTricking(s, 0);
        return result.Count;
    }

    void BackTricking(string s, int index)
    {
        if (index > s.Length - 1)
        {
            result.Add(new List<string>(path));
        }
        for (int i = 1; i <= s.Length - index; i++)
        {
            //切割字符串
            string t = s.Substring(index, i);
            //切割字符串的首字符
            string t0 = s.Substring(index, 1);
            if (t0 == "0" && t.Length != 1)
            {
                return;
            }
            int num = int.Parse(t);
            if (num > 25) return;
            path.Add(t);
            BackTricking(s, index + i);
            path.RemoveAt(path.Count - 1);
        }
    }
}

编程题第二题:

一个正整数n

如果n是偶数可以用n/2替换n

如果n是奇数,可以用2n+2或2n-2替换n

返回n变为1的最小替换次数

例:

输入:6

输出:4

解析:

6->3->4->2->1

using System;
using System.Collections.Generic;

class wm02
{
    int min = int.MaxValue;
    public int solution(int n)
    {
        a(0, n);
        return min;
    }

    void a(int count, int n)
    {
        if (n == 1)
        {
            min = Math.Min(count, min);
            return;
        }
        if (n % 2 == 0)
        {
            a(count + 1, n / 2);
        }
        else
        {
            a(count + 1, n * 2 + 2);
            a(count + 1, n * 2 - 2);
        }
    }
}

posted @ 2022-10-09 17:04  红红吖  阅读(198)  评论(0)    收藏  举报