Algo195 算法训练 1的个数
题目如下:
这题很简单,摆明了让你暴力。但暴力也分麻烦不麻烦。我这里就选择把所有数拼接成一个字符串,最后一行算出1的个数
package algo;
import java.util.Scanner;
/**
* @Description: 算法训练 1的个数
* @ClassName: Algo195
* @author: fan.yang
* @date: 2020/07/23 09:27
*/
public class Algo195 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
StringBuffer sb = new StringBuffer();
for(int i = 1;i <= n;i++){
sb.append(i);
}
//运用stream
System.out.println(sb.chars().filter(e -> e == '1').count());
}
}