数组想要用数组来解决十进制转为二进制的问题? 1C

#include
using namespace std;
int main()
{
int number;
int yushu;
int shuzu[20];
int i = 0;

cout << "Please enter an integer[n](1<=n<=200000):   " << endl;
cin >> number;
while (number > 0)
{
    yushu = number % 2;
    if (yushu == 0)
    {
        shuzu[i] = 0;

    }
    else
    {
        shuzu[i] = 1;
    }

    number = (number - yushu) / 2;
    i++;
}
for (i; i >= 0; i--)
{
    cout << shuzu[i];
}
system("Pause");
return 0;

}

0

1个回答

少了一句

 #include<iostream>
using namespace std;
int main()
{
    int number;
    int yushu;
    int shuzu[20];
    int i = 0;
    cout << "Please enter an integer[n](1<=n<=200000):   " << endl;
    cin >> number;
    while (number > 0)
    {
        yushu = number % 2;
        if (yushu == 0)
        {
            shuzu[i] = 0;

        }
        else
        {
            shuzu[i] = 1;
        }

        number = (number - yushu) / 2;
        i++;
    }
    i = i - 1;      //循环结束的时候i指在数组有效值的后一位
    for (; i >= 0; i--)
    {
        cout << shuzu[i];
    }
    system("Pause");
    return 0;
}
0
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!