English 中文(简体)
A. 挂图9 - 9 3
原标题:highest number with trailing 9 s

我的朋友和我是最近与网络方案规划中的一名同事一起毕业的。 就幸运而言,我们决定研究随机的C++编码挑战,并几乎完成了以下挑战,但它并没有通过所有测试案例。 我感到我们非常接近,但很可能出现需要纠正的简单错误。 希望得到任何帮助。

基本上,我们正在寻找人数最多的人群,其中9人处在一系列的min和max。 任何范围的最低数字是0,最高为3 000 000 000 000 000美元,因此我们正在使用大小。 我们的测试案例:

案例1: min = 460, max = 680, 回答=599

案例2: min = 18696, max = 18702, 回答=18699

案件3: min = 1255, max = 2999, 回答=2999

我们最后一点(案例3)的工作是公正的,但其他事情不是经过的。

#include<stdio.h>
#include<cmath>
#include<string>

int main()
{
    size_t min = 460;
    size_t max = 680;
    size_t answer= max;
    size_t tempAnswer = max;
    size_t tempMax = max; // temporary placeholder to not modify max
    size_t lastDigit = 0;
    size_t tempLook = 0;
    int count = 0;

    // following variable gets the length of number (ex: 3000 = 3) if we add 1 to function call we get the real length, but we dont need the first digit.
    int sizeOfMax = static_cast<int>(std::floor(std::log10(max)));

    for (int i = 0; i < sizeOfMax; i++)
    {
        lastDigit = tempMax % 10; // looks at last digit in max

        if (lastDigit == 9)
        {
            count++;
            tempMax = tempMax / 10;
        }
        else {
            for (int j = count; j > 0; j--)
            {
                tempMax *= 10;
                tempLook = max % 10;
                tempMax += tempLook;
            }

            if (count != sizeOfMax - count)
            {
                count = 0;
                tempMax -= 1;
                tempAnswer = tempMax;
                i = -1;
            }
        }

        if (answer < min)
        {
            answer = max;
        }
        else {
            answer = tempAnswer;
        }
    }

    printf("%zi", answer);
    return 0;
}

再次感谢任何帮助。 如果需要用更多的信息更新我的职位,我会一度知道该职位需要增加哪些信息。 我们也希望得到解释,说明我们纠正错误的情况,以便我们理解原因。 再次感谢!

如果最后的Digit是第9号,则我试图略微修改该法典,但我的答复却被 wild弃。

我还寻找了类似的问题/回答,但没有结果。

问题回答

I don t understand. Your answer to Case 1 and Case 2 seem correct to me. When you say "highest number with the most trailing 9 s", what takes priority, more 9 s or higher number? For example, should 199 beat 219? It has more trailing 9 s but it s not higher. I presume that s the case, but if it is, then surely "599" is the correct answer to Case 1, and 18699 is the correct answer to Case 2, no?

同样,我必须诚实,似乎你的法典过于复杂,难以解决当前的问题。 我不使用<代码>tempMax,tempAnswercount/code>,而是简化了该代码以直接更新<代码>answer。 当你发现有比目前的<代码>answer<>/code>多出9条。

我知道这确实是直接解决你的问题,但我想提出一个替代办法,因为我个人认为你的法典存在一些更大的问题,我甚至可能开始加以辩论。 该法典在从母体到最高之间逐个编号。 对于每个数字,如果最后一位数不是9位,则进行核对。 如果没有,它就检查了9条线索有多少。 如果它至少有1个线索,9个线索,所有其他数字均为0,它相应地更新了答案:

#include <iostream>

size_t highestNumberWithTrailing9s(size_t min, size_t max) {
    size_t answer = max;

    for (size_t i = min; i <= max; ++i) {
        if (i % 10 != 9) {
            size_t trailing9s = 0;
            size_t temp = i;

            while (temp % 10 == 9) {
                ++trailing9s;
                temp /= 10;
            }

            if (trailing9s > 0 && temp == 0) {
                answer = i;
            }
        }
    }

    return answer;
}

int main() {
    // Test cases
    std::cout << highestNumberWithTrailing9s(460, 680) << std::endl;  // Output: 599
    std::cout << highestNumberWithTrailing9s(18696, 18702) << std::endl;  // Output: 18699
    std::cout << highestNumberWithTrailing9s(1255, 2999) << std::endl;  // Output: 2999

    return 0;
}

我不能读一下你从你的法典中真正想要做什么(算法)。 (似乎过于复杂)

但是,......也许它看起来好像是灯塔吗?

#include <iostream>

//Extract the value of target digit.
//ex) Extract( 13847, 100 ) returns 8.
inline int Extract( size_t Val, size_t D )
{   return ( Val % (D*10) ) / D;    }

//Change the value of target digit.
//ex) Change( 13847, 1000, 9 ) return 19847.
inline size_t Change( size_t Val, size_t D, int Dx )
{   return Val - D*Extract(Val,D) + D*Dx;   }

//
size_t Test( size_t min, size_t max )
{
    int nDigits = static_cast<int>( std::log10(max) + 1 );

    //ex) if max= 18702, Ans initialized as 99999.
    size_t Ans = pow( 10, nDigits ) - 1;

    //ex) if max= 18702, D initialized as 10000.
    size_t D = pow( 10, nDigits-1 );
    while( D >= 1 )
    {
        for( int Dx=Extract(max,D); Dx>=Extract(min,D); --Dx )
        {
            Ans = Change( Ans, D, Dx );
            if( Ans <= max )return Ans; //Answer found
        }
        D /= 10;    //to next digit
    }

    return 0;  //will not reach here (?)
}

//
int main()
{
    std::cout << Test( 460, 680 ) << std::endl; //599
    std::cout << Test( 18696, 18702 ) << std::endl; //18699
    std::cout << Test( 1255, 2999 ) << std::endl;   //2999
    return 0;
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签