文章

28

粉丝

0

获赞

84

访问

2.5k

头像
01序列 题解:两种方法,除二取余/模拟二进制加法
P1001 计算机考研机试入门题
发布于2026年2月12日 11:55
阅读数 105

#include <iostream>
#include <string>
using namespace std;
int main(){
  //法一,模二取余
	// for(int i=0;i<64;i++){
	// 	string str;
  //   int x=i;
	// 	while(x>0){
	// 		str=char(x%2+'0')+str;
  //     x=x/2;
	// 	}
	// 	while(str.size()<6){
	// 		str='0'+str;
	// 	}
  //   cout<<str<<"\n";
	// }
	
  //法二,模拟二进制加法
  string str="000000";
  //每次遍历i就是给str+1,二进制满二进一
  for(int i=0;i<64;i++){
    cout<<str<<"\n";
    int index=5;//从后往前
    //当前位是0那就加1,是1就将其变0,直到往前找到一个0
    while(index>=0){
      if(str[index]=='0'){
        str[index]='1';
        break;//加完1直接退出
      }
      //不是0则是1,加1后变为0
      else{
       str[index]='0';
       index--;//往前找 
      }
    }
  }
  return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发