盛宝算法面试题

Please read through below two problems and solve both with whatever programming language of your choice. We don’t want to cost you a lot of time. Please spend no more than 45 minutes and submit whatever you have including incomplete code.

Problem #1

You are given a positive integer N. We are interested in find numbers with following property:

Say the number is M (M > 0). We keep incrementing it and adding it to our accumulative sum, until the sum equals N, no more no less.

M + (M + 1) + (M + 2) + … + (M + k) = N.

Please output how many numbers there are for a given N.

Sample Input #1: N = 10

Sample Output #1: 2

Explanation: There are 2 numbers that meet our requirement.

1: 1 + 2 + 3 + 4 = 10

10: 10 = 10

Sample Input #2: N = 125

Sample Output #2: 4

Explanation: There are 4 numbers that meet our requirement.

8: 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 = 125

23: 23 + 24 + 25 + 26 + 27 = 125

62: 62 + 63 = 125

125: 125 = 125



#include <iostream>
using namespace std;

int CalcSeqSum(int n);

int main()
{
    int N,i=0,j=0;
    int iTotalStyle = 0;  //计算方法

    std::cout << "请输入的需要计算的累加和N,按回车结束" << endl;
    std::cin >> N;

    for (i = 0; i <= N; i++)
    {
        for (j = i; j <= N; j++)
        {
            if ((CalcSeqSum(j) - CalcSeqSum(i)) == N)
            {
                std::cout << i+1 << ":" << j << "\n";
                iTotalStyle++;
            }
        }
    }
    std::cout << "共"<< iTotalStyle << "种解法!\n";
}


//求1:n的等差数列和
int CalcSeqSum(int n)
{
    return (1 + n) * n / 2;
}

Problem #2

You are given an integer K and a string S of length N, consisting of lowercase letters from the first K letters of the English alphabet. Find the number of palindrome strings of length N which are lexicographically smaller than S and consist of lowercase letters from the first K letters of the English alphabet.

A palindrome is a string that is the same written forwards and backwards. For example, annaracecaraaa and x are all palindromes, while abfrog and yoyo are not.

Sample Input #1: S = “bc”, K = 3 ([a-c])

Sample Output #1: 2

Explanation: The palindromes are ["aa", "bb"].

Sample Input #1: S = “abcdd”, K = 5 ([a-e])

Sample Output #1: 8

Explanation: The palindromes are ["aaaaa", "aabaa", "aacaa", "aadaa", "aaeaa", "ababa", "abbba", "abcba"].

Sample Input #1: S = “d”, K = 5 ([a-e])

Sample Output #1: 3

Explanation: The palindromes are ["a", "b", "c"].

 

 /// <summary>
    /// 计算打印回文
    /// </summary>
    private static void TestPalinDromes()
    {
        Console.WriteLine("Please Input S: ");

        var strS = Console.ReadLine();
        var strLen = strS.Length;

        Console.WriteLine("Please Input K: ");
        int K = 3;
        var strK = Console.ReadLine();
        if (!int.TryParse(strK, out K) || K <= 0)
        {
            Console.WriteLine("Please Input valid K !");
            return;
        }

        char aa = 'a';

        var condition = new List<List<char>>();
        int ii = 0;
        char cn = 'a';
        char max = (char)(cn + K - 1);
        var chs = new List<char>();
        do
        {
            cn = (char)(aa + ii);
            chs.Add(cn);
            ii++;
        } while (cn < max);
        for (int j = 0; j < strLen; j++)
        {
            //Console.WriteLine("索引:" + string.Concat(chs));
            condition.Add(chs);
        }

        var CombineStrs = new List<string>();

        var item = new StringBuilder();

        var allCombinations = AllCombinationsOf(condition);

        var combinationStrs = allCombinations.Select(x => string.Concat(x));
        CombineStrs.AddRange(combinationStrs.Where(x => string.Compare(strS, x) >= 1 && isPlaindromes(x)));

        Console.WriteLine($"Total : {CombineStrs.Count}");
        Console.WriteLine("The palindromes are:");
        foreach (var combination in CombineStrs) //打印结果
        {
            Console.WriteLine(combination);
        }
    }

    private static bool isPlaindromes(string inputStr)
    {
        if (inputStr == null || inputStr == "")
            return false;
        var strLen = inputStr.Length;
        if (strLen == 1)
            return true;
        if (strLen % 2 == 0)
        {
            int half = strLen / 2;
            var front = new StringBuilder().Append(inputStr.Take(half).ToArray()).ToString();
            var back = new StringBuilder().Append(inputStr.Skip(half).Reverse().ToArray()).ToString();
            // Console.WriteLine($"front={front},back={back}");
            if (front == back)
                return true;
        }
        else
         if (strLen % 2 == 1)
        {
            int half = strLen / 2;
            var front = new StringBuilder().Append(inputStr.Take(half).ToArray()).ToString();
            var back = new StringBuilder().Append(inputStr.Skip(half + 1).Reverse().ToArray()).ToString();
            // Console.WriteLine($"front={front},back={back}");
            if (front == back)
                return true;
        }
        return false;
    }

    private static List<List<T>> AllCombinationsOf<T>(List<List<T>> sets)
    {
        var combinations = new List<List<T>>();

        foreach (var value in sets[0])
            combinations.Add(new List<T> { value });

        foreach (var set in sets.Skip(1))
            combinations = AddExtraSet(combinations, set);

        return combinations;
    }

    private static List<List<T>> AddExtraSet<T>
         (List<List<T>> combinations, List<T> set)
    {
        var newCombinations = from value in set
                              from combination in combinations
                              select new List<T>(combination) { value };

        return newCombinations.ToList();
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值