c语言改成c++ 5

#include<iostream>usingnamespacestd;intmain(){intn,r,i;while(scanf("%d%d",&n,&r)!=EOF... #include<iostream>
using namespace std;
int main()
{
int n,r,i;
while(scanf("%d %d",&n,&r)!=EOF){
if(n<0){
printf("-");n=-n;}
if(n==0){printf("0\n");continue;}
int c=0,a[100];
while(n){
a[c]=(n%r);
c++;
n/=r;}
for(i=c-1;i>=0;i--){
if(a[i]>=10){
printf("%c",'A'+a[i]-10);}
else printf("%d",a[i]);}
printf("\n");}}或者直接打这个进制转换题的c++代码,为每个测试实例输出转换后的数,每个输出占一行。如果R大于10,则对应的数字规则参考16进制(比如,10用A表示,等等)
展开
 我来答
金色潜鸟
2019-07-23 · TA获得超过3.2万个赞
知道大有可为答主
回答量:1.3万
采纳率:89%
帮助的人:6670万
展开全部
C 语言就是 C++语言。加头文件#include <stdio.h>,就支持 scanf,printf 等输入输出了。例:
#include<iostream>
using namespace std;
#include <stdio.h>
其余一字不变即可。
紫苏干锅牛蛙
2019-07-22 · TA获得超过147个赞
知道小有建树答主
回答量:117
采纳率:77%
帮助的人:49.9万
展开全部
#include<iostream>
using namespace std;
int main() {
int n,r;
while(cin >> n >> r) {
if(n < 0) {
cout << "-";
n=-n;
}
if(n == 0) {
cout << "0" << endl;
continue;
}
int c=0, a[100];
while(n) {
a[c]=(n % r);
c++;
n/=r;
}
for(int i = c-1; i >= 0; i--) {
if(a[i] >= 10){
cout << 'A'+a[i]-10;
}
else cout << a[i];
}
cout << endl;
}
return 0;
}
本身你的代码就是C++的,想要修改的话只要把所有的printf和scanf替换为C++的流控制cout和cin就可以了,
追问
那运行结果23 12为166而答案却是1B不一样那
追答
其他进制的换算我没想那么多,给你重贴一段,牺牲了效率但是能解决问题,如果是ACM的话另说。
#include
using namespace std;
int main() {
int n, r;
while (cin >> n >> r) {
int * result;
int tempN = n;
int count = 0;
while (tempN) {
result[count] = tempN%r;
tempN = tempN/r;
count ++;
}
count --;
for (count; count >=0; --count) {
if (result[count] >= 10) {
cout << (char)((result[count]-10)+'A');
}
else {
cout << result[count];
}
}
cout << endl;
}
return 0;
}
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
White_MouseYBZ
2019-07-23 · TA获得超过4万个赞
知道大有可为答主
回答量:2.1万
采纳率:82%
帮助的人:8176万
展开全部
把乔_公子答案中的cout << 'A'+a[i]-10;改成cout << char('A'+a[i]-10);。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式