求一个int类型数字对应的二进制数字中1的最大连续数,例如3的二进制为00000011,最大连续2个1
数据范围:数据组数:,
进阶:时间复杂度:,空间复杂度:
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); while(sc.hasNext()){ int input = sc.nextInt(); String bi = Integer.toBinaryString(input); String subs = "1"; int cnt = 1; for(int i=1; i<=bi.length(); i++) { if(bi.contains(subs)) { cnt = subs.length(); subs += "1"; } } System.out.println(cnt); } } }
import java.util.Scanner; public class Main{ public static void main(String []args){ Scanner sc=new Scanner(System.in); while(sc.hasNext()){ int a=sc.nextInt(); System.out.println(getNums(a)); } } private static int getNums(int a){ if(a==0) return 0; String str=Integer.toBinaryString(a); int count=0; int max=0; for(int i=0;i+1<str.length();i++){ if(str.charAt(i)=='1'&&str.charAt(i)==str.charAt(i+1)){ count++; if(count>max){ max=count; } } else{ count=0; } } return max+1; } }
while True: try: b_nums=list(map(int,bin(int(input())).replace('0', ' ' ).replace('b', ' ').split())) print(len(str(max(b_nums)))) except: break
import java.util.Scanner; //转二进制字符串,双指针法 // public class Main{ // public static void main(String[] args){ // Scanner sc = new Scanner(System.in); // while(sc.hasNext()){ // int num = sc.nextInt(); // String s = Integer.toBinaryString(num); // int left=0, right=0; // int maxLen=0; // while(right<s.length()){ // if(s.charAt(right)=='1'){//1处 // right++; // }else{//0处 // if(right-left>maxLen){//更新最大值 // maxLen=right-left; // } // if(left!=right) left=right;//更新left // left++; // right++; // } // } // //处理末尾连续1的情况 // if(right-left>maxLen) maxLen=right-left; // System.out.println(maxLen); // } // } // } //位运算法 public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int num = sc.nextInt(); int maxLen=0, temp=0; while(num!=0){ if((num&1)==1){//遇到末尾1,temp加1 temp++; maxLen = maxLen>temp ? maxLen:temp; }else{//遇到末尾0,temp置零,重新计数 temp=0; } num >>>= 1;//逻辑右移 } System.out.println(maxLen); } } }
while True: try: n = int(input()) count = 0 res = 0 while n: count += n&1 n >>= 1 if not n&1: if count > res: res = count count = 0 print(res) except: break思路:从后往前找,在碰到0之前一直计数,如果连续1的计数超过当前记录值,则赋给记录的变量;一旦碰到0,计数恢复为0,再碰到1重新计数
用0当作分隔符,再计算每片的长度就可以了
while True: try: n = int(input()) bn = bin(n).replace("0b","").split("0") m = 0 for i in bn: if len(i)>m: m = len(i) print(m) except: break
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); while(input.hasNext()) { int num =input.nextInt(); String str=dexToBinary(num); char[] chars =str.toCharArray();//将字符串转换为字符数组\ int count=0,maxcount=0; for (int i = 0; i < chars.length; i++) { if(chars[i]=='1') { count++; } else { maxcount = Math.max(maxcount, count); count = 0; } } maxcount = Math.max(maxcount, count); System.out.println(maxcount); } input.close(); } public static String dexToBinary(int decNum) { String binary = ""; //转换好的二进制字符串 while(decNum!=0) { int temp=decNum%2; //取余数 binary = temp +binary;//每取一个余数,就往前拼接 decNum/=2; } //System.out.println(binary); return binary; } }
# 思路:二进制字符串中只有1和0两种字符,那么我们以‘0’作为分隔符(刀)来切割二进制串。 # 遇到每个'0'就切一刀,若刀和刀之间没有内容,则为空字符串,也包含在结果当中。 # 如:字符串'111000101100111111'.split('0')得到['111', '', '', '1', '11', '', '111111'] # 我们只需统计结果列表中最大的长度是几,即为本题答案。 while True: try: ten = int(input().strip()) bin = format(ten, 'b') print(max(map(len, bin.split('0')))) except: break
#include <iostream> using namespace std; //通过取余数的操作可以查看二进制数中有多少个1 int main() { int n; while(cin>>n) { int max = 0; int len = 0; while(n>0) { if(n%2 == 1) len++; else { max = max<len?len:max; len = 0; } n = n/2; } max = max<len?len:max; cout<<max<<endl; } return 0; }
#include <iostream> using namespace std; int main(){ int a; while(cin >> a){ int count = 0, maxcount = 0; while(a){ if(a & 1){ ++count; } else{ count = 0; } maxcount = max(count, maxcount); a >>= 1; } cout << maxcount << endl; } return 0; }
while True: try: n = (str(bin(int(input())))).replace('0b','') result = 0 for i in range(1,len(n)+1): if '1' * i in n: result = i else: break print(result) except: break精华: if '1' * i in n: