zoukankan      html  css  js  c++  java
  • 18.4---2出现了几次(CC150)

    思路:1,先给出LTE的代码:

        public static  int countNumberOf2s(int n) {
            // write code here
            int res = 0;
            for(int i = 0; i <=n ; i++){
                res += count(i);
            }
            return res;
        }
        public static int count(int n){
            int ans = 0;
            while(n > 0){
                if(n % 10 == 2){
                    ans++;
                }
                n = n / 10;
            }
            return ans;
        }
    View Code

    2,思路是每一个位置是2的时候,有几个数。

    当着一个位置是大于2的时候,那么放2了。前面的high可以从0一直到它本身,所以是(high+1)*flag之所以*flag是因为后面的low可以0到它本身,因为大于2了。

    这个位置是==2的时候,放上去2,那么前面先放0到high-1,后面low随便取。所以,high*flag。high取它本身的时候,那么后面只能是0-low的取法,所以是:high*flag + (low+1).

    这个位置是<2的时候,放上去2,那么前面只能去0到high-1.后面随便取,所以是high*flag。

    附上代码:

        public static  int countNumberOf2s(int n) {
            // write code here
            int res = 0;
            for(int i = 0; i <=n ; i++){
                res += count(i);
            }
            return res;
        }
        public static int count(int n){
            int ans = 0;
            while(n > 0){
                if(n % 10 == 2){
                    ans++;
                }
                n = n / 10;
            }
            return ans;
        }
    View Code
  • 相关阅读:
    Sql2000分页效率
    CSS笔记
    向模态窗体传递参数和获取返回值
    css 实现div 内容垂直居中
    轻量级的数据交换格式——初识Json
    CSS 绝对定位
    前台小模块CSS布局代码
    XML常用类(淘宝API)
    表单form
    js 分页
  • 原文地址:https://www.cnblogs.com/yueyebigdata/p/5104725.html
Copyright © 2011-2022 走看看