C语言的棘手面试问题

发布于 2024-10-24 19:42:49 字数 910 浏览 3 评论 0原文

在下面的面试问题中:

给定一个数字n,给我这些数字 (其中 3..5 和偶数个 数字),其相加将返回 原始号码。结果数字 应尽可能平衡, 意味着不是返回 35,例如,返回 4 和 <代码>4。例如:

<前><代码>7 = 3 + 4 16 = 4 + 4 + 4 + 4 而不是 3 + 5 + 4 + 4 24 = 12 + 12 或 6 + 6 + 6 + 6

我想到了以下方法:

splitnumber(int n)
{
    //check if the number is even
    if(n%2==0)
    {
        print(n/2,n/2);
        //check if x=2^m multiple exists or
        // not..like 4,8,16 etc
        print (n/x...n/x);
    }
    else //else if the no is odd... this part is incomplete
    {
        if(n-3>0)
        {
            print (3);

        }

        n-=3;
        if(n>0)
        {
            if (n>5)
            {
                print(3)
                n-=3;
            }
        }
    }
}

但我仍然无法完成所有情况...当答案有不平衡解时我应该如何检查?

In the following interview question :

Given a number n, give me the numbers
(among 3..5 and an even number of
numbers) whose adding would return the
original number. The resulting numbers
should be as balanced as possible,
meaning that instead of returning 3
and 5, for instance, return 4 and
4. Ex:

7 = 3 + 4
16 = 4 + 4 + 4 + 4 rather than 3 + 5 + 4 + 4
24 = 12 + 12 or 6 + 6 + 6 + 6

I thought of the following method:

splitnumber(int n)
{
    //check if the number is even
    if(n%2==0)
    {
        print(n/2,n/2);
        //check if x=2^m multiple exists or
        // not..like 4,8,16 etc
        print (n/x...n/x);
    }
    else //else if the no is odd... this part is incomplete
    {
        if(n-3>0)
        {
            print (3);

        }

        n-=3;
        if(n>0)
        {
            if (n>5)
            {
                print(3)
                n-=3;
            }
        }
    }
}

but still I am not able to complete all the cases... How should I check when the answer has unbalanced solution??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

§对你不离不弃 2024-10-31 19:42:49
if (n < 4) print n;
else
    switch (n % 4)
        case 0: *print n/4 4's*
        case 1: *print n/4 - 1 4's* print 5
        case 2: *print n/4 - 1 4's* print 3 print 3
        case 3: *print n/4 4's* print 3

C# 中的实现效率稍低

if (n < 4) Console.WriteLine(n);
else
    switch (n % 4)
    {
        case 0:
            Console.WriteLine(String.Join(" ", new string('4', n / 4).ToArray()));
            break;
        case 1:
            Console.WriteLine(
                (String.Join(" ", new string('4', n/4).ToArray().Skip(1)) + 
                " 5").TrimStart());
            break;
        case 2:
            Console.WriteLine(
                (String.Join(" ", new string('4', n/4).ToArray().Skip(1)) + 
                " 3 3").TrimStart());
            break;
        case 3:
            Console.WriteLine(String.Join(" ", new string('4', n/4).ToArray() + 
                " 3"));
            break;

    }
if (n < 4) print n;
else
    switch (n % 4)
        case 0: *print n/4 4's*
        case 1: *print n/4 - 1 4's* print 5
        case 2: *print n/4 - 1 4's* print 3 print 3
        case 3: *print n/4 4's* print 3

Slightly inefficient implementation in C#

if (n < 4) Console.WriteLine(n);
else
    switch (n % 4)
    {
        case 0:
            Console.WriteLine(String.Join(" ", new string('4', n / 4).ToArray()));
            break;
        case 1:
            Console.WriteLine(
                (String.Join(" ", new string('4', n/4).ToArray().Skip(1)) + 
                " 5").TrimStart());
            break;
        case 2:
            Console.WriteLine(
                (String.Join(" ", new string('4', n/4).ToArray().Skip(1)) + 
                " 3 3").TrimStart());
            break;
        case 3:
            Console.WriteLine(String.Join(" ", new string('4', n/4).ToArray() + 
                " 3"));
            break;

    }
陈独秀 2024-10-31 19:42:49

这是我的解决方案,其中结果将完美平衡并检测不可能的情况:

vector<int> recursive_splitnumber(int n) {

    if (n <= 5) {
        return vector<int>(1,n);
    }

    int unbalancer = 0;
    vector<int> result1, result2;
    do {
        int val1, val2;
        if (n%2 == 0) {
            val1 = n%2 + unbalancer;
            val2 = n%2 - unbalancer;
        }
        else {
            val1 = (n-1)%2 + 1 + unbalancer;
            val2 = (n-1)%2 - unbalancer;
        }

        result1 = recursive_splitnumber(val1);
        result2 = recursive_splitnumber(val2);

        // Concatenate the result of the even and odd splits
        result1.insert(result1.end(),result2.begin(),result2.end());

        ++unbalancer;

    } while (result1.size()%2 != 0 && unbalancer <= 1);
    return result1;
}

bool splitnumber(int n) {
    vector<int> split = recursive_splitnumber(n);
    if (split.size()%2 == 0) {
        copy(split.begin(), split.end(), ostream_iterator<int>(cout, " "));
        return true;
    } else
        return false;
}

该解决方案还将考虑数字 22 等情况,其中平衡除法给出 11+11(11 是无法使用给定的数字表示的数字)规则),细分将按 10+12 进行,然后是 5+5+6+6,最后是 5+5+3+3+3+3。

Here is my solution where the result will be perfectly balanced and with detection of impossible cases:

vector<int> recursive_splitnumber(int n) {

    if (n <= 5) {
        return vector<int>(1,n);
    }

    int unbalancer = 0;
    vector<int> result1, result2;
    do {
        int val1, val2;
        if (n%2 == 0) {
            val1 = n%2 + unbalancer;
            val2 = n%2 - unbalancer;
        }
        else {
            val1 = (n-1)%2 + 1 + unbalancer;
            val2 = (n-1)%2 - unbalancer;
        }

        result1 = recursive_splitnumber(val1);
        result2 = recursive_splitnumber(val2);

        // Concatenate the result of the even and odd splits
        result1.insert(result1.end(),result2.begin(),result2.end());

        ++unbalancer;

    } while (result1.size()%2 != 0 && unbalancer <= 1);
    return result1;
}

bool splitnumber(int n) {
    vector<int> split = recursive_splitnumber(n);
    if (split.size()%2 == 0) {
        copy(split.begin(), split.end(), ostream_iterator<int>(cout, " "));
        return true;
    } else
        return false;
}

That solution will also take into account cases like the number 22 where the balanced division gives 11+11 (11 being a number that cannot be represented using the given rules), the subdivision will be done as 10+12, then 5+5+6+6 and finally 5+5+3+3+3+3.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文