//实现10进制到任意进制之间的转换
#include
using namespace std;
int main()
{
int n,a,i=0;
cout << "请输入一个数" << endl;
cin >> n;
cout << "请输入你想转换的进制" << endl;
cin >> a;
if (a <=2 && a >= 9)
{
cout << "请输入2-9之间的进制" << endl;;
}
int* p = new int[20];
do
{
p[i++] = n % a;
n = n / a;
if (n == 1)
break;
} while (n != 0);//进制取余进行换算
for (int i; i >= 0; i--)
{
cout << p[i] << " ";
}
delete[]p;
return 0;
}
for (int i; i >= 0; i--)
改为
for (i--; i >= 0; i--)
该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下:
在您的代码中,可能存在中文字符与编译器字符集不匹配的问题,导致输出出现乱码。您可以尝试以下方法解决:
1、 在代码文件开头添加如下注释,指定编译器使用的字符集:
// -*- coding: utf-8 -*-
这样可以将编译器字符集设置为 UTF-8,确保中文字符能够正确输出。
2、 修改输出语句,将中文字符转换为英文字符,例如将 "请输入一个数" 改为 "Please enter a number"。
修改后的代码如下所示:
// -*- coding: utf-8 -*-
// 实现10进制到任意进制之间的转换
#include <iostream>
using namespace std;
int main()
{
int n, a, i = 0;
cout << "Please enter a number: " << endl;
cin >> n;
cout << "Please enter the base you want to convert to: " << endl;
cin >> a;
if (a <= 2 || a >= 9)
{
cout << "Please enter a base between 2 and 9." << endl;
return 0;
}
int* p = new int[20];
do
{
p[i++] = n % a;
n = n / a;
if (n == 1)
break;
} while (n != 0);
for (int j = i - 1; j >= 0; j--)
{
cout << p[j];
}
cout << endl;
delete[] p;
return 0;
}
希望能够帮助到您。
如果以上回答对您有所帮助,点击一下采纳该答案~谢谢