For loop divide numbers

守給你的承諾、 提交于 2019-12-04 04:19:13

问题


I'm an amateur when it comes to C++ but I've already received a task which is over my knowledge.

Task is to enter numbers n,m. Programme must take it as an interval, in which it checks if there is any number which is a sum of numbers with the same exponent.

EDIT:(18.10.15) Turns out I didn't understood my task correctly. Here it is:

"User enter two numbers. Programme takes it as the interval in which it checks all the numbers. If there's a number in interval which all digit's sum of SAME exponent is that number, then programme shows it."

For example, I enter 100 and 200. In this interval there's 153. 153 = 1^3 + 5^3 + 3^3 (1+125+27) Programme shows 153.

cin >> n;
cin >> m;
    for (int i=n; i<=m; i++)
    {
        for (int k=n; k<=i; k++)
        {
                a = n % 10; //for example, I enter 153, then a=3
                f = n /= 10; //f=15
                b = f % 10;  //b=5
                f = f /= 10; //f=1
                c = f % 10; //c=1
                f = f /= 10;
                d = f % 10;

                for (int j=1; j<=5; j++)
                {
                    a = a * a;
                    b = b * b;
                    c = c * c;
                    d = d * d;
                    if (a + b + c + d == n)
                    {
                        cout << n << endl;
                    }
                }
        }
    }

Any help will be appreciated.


回答1:


Task is to enter numbers n,m. Programme must take it as an interval, in which it checks if there is any number which is a sum of numbers with the same exponent.

Assuming the range is given as [n, m), then here's your program:

return (n != m);

Any number can be seen as a sum of numbers with the same exponent. For example:

0 = 0 ^ 3 + 0 ^ 3 + 0 ^ 3
1 = 1 ^ 3 + 0 ^ 3
2 = 1 ^ 3 + 1 ^ 3
3 = 1 ^ 3 + 1 ^ 3 + 1 ^ 3

and so on. This is true even for negative numbers.

So in any non-empty range there exists at least 1 such number.




回答2:


"All I know is how to get the programm to check each number separately" "Programme must not use arrays."

    for (int i = n; i <= m; i++) {
        ...
        int x = (int)Math.log10(i);
        int rest = i;
        for (int p = x; p>=0; p--) {
            int digit = rest / (int)Math.pow(10,p);
            rest = i % (int)Math.pow(10,p);

            //3802 = 3*10^3 + 8*10^2 + 0*10^1 + 2*10^0 
        }
    }

    ...

Sorry, is Java no C++




回答3:


Sorry that I answer so late and that I phrased my question poorly - English isn't my native language.

But turns out I didn't understood my task correctly. Here it is:

"User enter two numbers. Programme takes it as the interval in which it checks all the numbers. If there's a number in interval which all digit's sum of SAME exponent is that number, then programme shows it."

For example, I enter 100 and 200. In this interval there's 153. 153 = 1^3 + 5^3 + 3^3 (1+125+27) Programme shows 153.

cin >> n;
cin >> m;
    for (int i=n; i<=m; i++)
    {
        for (int k=n; k<=i; k++)
        {
                a = n % 10; //for example, I enter 153, then a=3
                f = n /= 10; //f=15
                b = f % 10;  //b=5
                f = f /= 10; //f=1
                c = f % 10; //c=1
                f = f /= 10;
                d = f % 10;

                for (int j=1; j<=5; j++)
                {
                    a = a * a;
                    b = b * b;
                    c = c * c;
                    d = d * d;
                    if (a + b + c + d == n)
                    {
                        cout << n << endl;
                    }
                }
        }
    }


来源:https://stackoverflow.com/questions/33113810/for-loop-divide-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!