算法倒数

时间:2017-01-02 19:14:03

标签: c++ c algorithm visual-c++ inversion

这是倒数的公式吗?

例如123-321?

数字是K

inv K = K%100 + K / 10%10 * 10 + K%10 * 100

我不确定我是否完全写过,我需要这个正确的学校任务图形算法

编辑:是的我是愚蠢的xD我是初学者xD

3 个答案:

答案 0 :(得分:3)

这个逻辑会帮助你。变量inverse是输出。

        int num = 123;
        double inverse = 0;
        while (num != 0)
        {
            inverse = inverse * 10;
            inverse = inverse + num % 10;
            num = num / 10;                
        }

答案 1 :(得分:1)

如果您希望代码可以使用任何数字,请考虑转换为字符串,然后将其反转!

int invert( int input )
{
    std::stringstream str;
    str << input;

    std::string s = str.str();
    std::reverse(s.begin(),s.end());

    return atoi( s.c_str() );
}

答案 2 :(得分:0)

Given a counter c
A max counter m
A variable for the max element, e
And an array a
For every element in the array a
    If the element matches the previous element
        increment the count c, and:
          if the count c > m:
            set m to c, and e to the current value
    If it doesn't match the previous, reset the count to 1

Then after all this, the result is e, c times

输出 #include <stdio.h> #include <stdlib.h> int main(void) { int i = 123045 , j = 0,k=0; while( i != 0 ) { j=i%10; k = k *10 + j; i /=10; } printf("%d\n", k); return 0; }