# 算法训练 二进制数数

如5的二进制为101，包含2个“1”。

2 3

3

L<=R<=100000;

## 代码实现

import java.util.Scanner;
public class Main {

public static void main(String[] args) {
int res=0;
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
for(int i=n;i<=m;i++) {
String b = Integer.toBinaryString(i);
res +=geshu(b);
}
System.out.println(res);
}
static int geshu(String b) {
int sum=0;
for(int i=0;i<b.length();i++) {
if(b.charAt(i)=='1')
sum++	;
}
return sum;
}
}


# 吝啬的国度

1
10 1
1 9
1 8
8 10
10 3
8 6
1 2
10 4
9 5
3 7

-1 1 10 10 9 8 3 1 1 8

## 代码实现

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
static ArrayList<city> citys;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int M = scanner.nextInt();
while(M-->0){
int N = scanner.nextInt();
int S = scanner.nextInt()-1;
citys = new ArrayList<city>();
for(int i = 0;i < N;i++){
}
for(int i = 0;i < N-1;i++){
int a = scanner.nextInt();
int b = scanner.nextInt();
}
for(int i = 0;i < N;i++){
search Search = new search(S,i);
Search.searchs();
}
}
scanner.close();
}
static class city{
public ArrayList<Integer> nextCitys;
int id;
city(int id){
this.id = id;
this.nextCitys = new ArrayList<Integer>();
}
}
static class search{
int startCityId;
int endCityId;
ArrayList<Integer> searchsCitys = new ArrayList<Integer>();
boolean flag = true;
String ends;
search(int startCityId,int endCityId){
this.startCityId = startCityId;
this.endCityId = endCityId;
}
void searchs(){
if(startCityId != endCityId){
searchNext(startCityId,endCityId);
System.out.print(ends+" ");
}else{
System.out.print("-1 ");
}
}
void searchNext(int nowCityId,int endCityId){
for(int i = 0;i < searchsCitys.size();i++){
if(nowCityId == searchsCitys.get(i)){
return;
}
}
if(flag){
city nowCity = citys.get(nowCityId);
for(int i = 0;i < nowCity.nextCitys.size();i++){
city nextCity = citys.get(nowCity.nextCitys.get(i));
if(nextCity.id == endCityId){
flag = false;
ends = nowCity.id+1+"";
break;
}else{
searchNext(nextCity.id,endCityId);
}
}
}
}
}
}


03-07 12
03-06 5
03-06 17