计算1~2008中所有自然数中1和0的个数总数. 通过自然数的大小划分区间,将自然数每位上的数载入列表,循环计数. list = [] onecount = 0 zerocount = 0 for i in range(1,2009): if int(i/1000)!=0: #thousands list.append(int(i/1000)) list.append(int(i/100)%10) list.append(int(i/10)%10) list.append(i%10) for c
一行搞定-统计一句话中每个单词出现的个数 >>> s'i am a boy a bood boy a bad boy' 方式一:>>> dict([(i,s.split().count(i)) for i in s.split()]){'a': 3, 'boy': 3, 'i': 1, 'am': 1, 'bad': 1, 'bood': 1} >>> set([(i,s.split().count(i)) for i in s.split()])se
输入一个int型的正整数(十位数之内!嘞!),计算出该int型数据在内存中存储时1的个数. #include<bits/stdc++.h> using namespace std; int main() { int n; cin>>n; ; while(n) { ) count++; n/=; //n>>1; } cout<<count<<endl; } my C++ codes are above: print(bin(int(input())
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3. Note: T
package com.Summer_0420.cn; /** * @author Summer * 获取数值型数组中大于60的元素个数 * 给数值型数组中不足60分的加20分 */ public class TestMethod02 { public static void main(String[] args) { int [] a = {1,35,60,80,75,123,156,32,1}; show(a); } private static void show(int[] a) { i
//求两个数中不同的位的个数 #include <stdio.h> int count_different(int a, int b) { int count = 0; int c = a^b; //a,b中不同的位即为1 while (c) { count++; c = c&(c - 1); //把c中最后一个1去掉 } return count; } int main() { printf("%d\n", count_different(3,8)); //3 p