文章

14

粉丝

49

获赞

0

访问

2.3k

头像
1380二进制串
我要提问
发布于2024年2月27日 10:27
阅读数 166

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main(){
  4. unsigned int n;
  5. while(cin>>n){
  6. string s; //定义字符串存储转换后的二进制串
  7. int i=0;//字符串起始存储位置
  8. while(n>0){//循环停止条件
  9. s[i++]=n%2+'0';
  10. n/=2;
  11. }
  12. cout<<s;
  13. }
  14. }

大佬们能看看这一题代码哪里出错了吗?可以运行,但是输出是一片空白

登录查看完整内容


登录后发布评论

1 条评论
snake VIP
2024年2月27日 10:58

string不是这么用的

s[i++]=n%2+'0';

改为

s+=n%2+'0';

或者定义为char s[1005] = {0};

赞(0)