输入12,从1到12这些整数中包含1 的数字有1,10,11和12,1一共出现了5次。

网友投稿 510 2022-10-29

输入12,从1到12这些整数中包含1 的数字有1,10,11和12,1一共出现了5次。

输入12,从1到12这些整数中包含1 的数字有1,10,11和12,1一共出现了5次。

import java.util.Scanner;//题目:输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数。//例如输入12,从1到12这些整数中包含1 的数字有1,10,11和12,1一共出现了5次。public class CiShu { public static void main(String args[]) { Scanner cin = new Scanner(System.in); int a = cin.nextInt(); int result = 0; for (int i = 1; i <= a; i++) { result = result + contains1num(i); } System.out.print(result); } private static int contains1num(int num) { int count = 0; for (int i = 0; i < 6; i++)// 统计的位数 { if (num % Math.pow(10, i + 1) == Math.pow(10, i)) count++; num = (int) (num - num % Math.pow(10, i + 1)); } return count; }}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:一个类似Reddit的社区,供程序员讨论和分享他们的想法
下一篇:SpringBoot如何根据目录结构生成API接口前缀
相关文章

 发表评论

暂时没有评论,来抢沙发吧~