itoa函数怎么用啊

img

img


用了这个函数之后,自己运行程序是没问题的,但提交上去的话,有语法错误还是什么意思?

itoa非标准库,不可移植。可以自行编写
代码仅供参考,谢谢!

img

#include<stdio.h>

void my_itoa(int a, char *s)
{
    char str[1000];
    int i = 0, j = 0;
    while (a)
    {
        *(str + i++) = a % 10 + 48;
        a /= 10;
    }
    *(str + i) = '\0';

    for (i -= 1; i >= 0; i--)
    {
        *(s + j++) = *(str + i);
    }
    *(s + j) = '\0';
}

int main()
{
    int a, b;
    char s_r7[4] = "000", s_r9[4] = "000";

    for (int i = 100; i < 1000; i++)
    {
        my_itoa(i, s_r7);

        a = (s_r7[0] - 48) * 49 + (s_r7[1] - 48) * 7 + (s_r7[2] - 48);
        b = (s_r7[2] - 48) * 81 + (s_r7[1] - 48) * 9 + (s_r7[0] - 48);

        s_r9[0] = s_r7[2];
        s_r9[1] = s_r7[1];
        s_r9[2] = s_r7[0];
        s_r9[3] = '\0';

        if (a == b &&
            s_r9[0] < '9' &&
            s_r9[1] < '9' && 
            s_r9[2] < '9' && 
            s_r7[0] < '7' && 
            s_r7[1] < '7' && 
            s_r7[2] < '7')
        {
            fprintf(stdout, "十进制数:%d\n", a);
            fprintf(stdout, "七进制数:%s\n", s_r7);
            fprintf(stdout, "九进制数:%s\n", s_r9);
            fprintf(stdout, "………………………………………………\n");
        }

    }

    return 0;
}

可能需要你自己写这个itoa函数吧

因为itoa不是一个标准的c函数,它是windows特有的,所以你自己运行是没问题的

int XtoD(int s, int x)
{
    int d = 0, t, dx=1;
    while (s > 0)
    {
        t = s % 10;
        d += t * dx;
        s /= 10;
        dx *= x;
    }
    return d;
}

int main(void)
{
    int s7, n9, d10, g, s, b;
    for (b = 1; b < 10; b++)
    {
        for (s = 0; s < 10; s++)
        {
            for (g = 1; g < 10; g++)
            {
                s7 = b * 100 + s * 10 + g;
                n9 = g * 100 + s * 10 + b;
                d10 = XtoD(s7, 7);
                if (d10 == XtoD(n9, 9))
                {
                    cout << d10 << "\n"
                         << s7 << "\n"
                         << n9 << endl;
                     return 0;
                }
            }
        }
    }

    return 0;
}