zoukankan      html  css  js  c++  java
  • 数1游戏

    一.题目

    1 给定一个十进制的正整数,写下从1开始,到N的所有整数,然后数一下其中出现“1”的个数。
    2 要求:
      (1) 写一个函数 f(N) ,返回1 到 N 之间出现的“1”的个数。例如 f(12)  = 5。
      (2)在32位整数范围内,满足条件的“f(N) =N”的最大的N是多少。
     
    二.设计思想
      (1)一位数时
        f(0)=0;f(1)=1;f(2-9)=1;
      (2)二位数时
        f(10)=1+(0+1)=2;
        f(11)=(1+1)+(1+1)=4;
        f(12)=(1+1)+(2+1)=5;
        f(13)=(1+1)+(3+1)=6;
        f(23)=(2+1)+10=13;
        f(33)=(3+1)+10=14;
        f(93)=(9+1)+10=20;
      (3)三位数时
        f(123)=个位出现的1数+十位出现的1数+百位出现的1数
      (4)N = abcde时
        以c位为例
        if(c==0)  num=ab*100;
        if(c==1)  num=ab*100+de+1;
        if(c>=2)  num=(ab+1)*100
    三.源代码
    # include <iostream>
    using namespace std;
    # include <math.h>
     
    int Count1Num(int Digit)
    {
        int figure = 1;               //标记计数1的位数
        int curOfDigit = 0;        //当前位数字
        int lowerOfDigit = 0;    //较低位数字
        int higherOfDigit =  0;  //较高位数字
        int count = 0;
        
        while(Digit / figure != 0)
        {
                                                //取数字
            curOfDigit = (Digit / figure) % 10;
            lowerOfDigit = Digit - (Digit / figure * figure);
            higherOfDigit = Digit / (figure * 10);
            
            if(Digit <= 0)
                return 0;
            
            if(0 == curOfDigit)                    //当前数字为0时计数
            {
                count += higherOfDigit * figure;
            }
            else if(1 == curOfDigit)              //当前数字为1时计数
            {
                count += higherOfDigit * figure + lowerOfDigit + 1;
            }
            else
            {
                count += (higherOfDigit+1) * figure;
            }
            figure = figure * 10;                //数字左移一位
        }
        return count;
    }
    
    void main()
    {
        int Digit;
        int max=0;
        
        while((cout<<"    需要结束测试请输入0
    请输入要测试的数值(tip:输入自然数):"<<endl)&&(cin>>Digit))
        {
            if(Digit == 0)
                break;
            
            cout<<"从数字1到数字"<<Digit<<"包含1个数为:"<<Count1Num(Digit)<<endl;
        }
    }

    四.截图

    五.总结

     
  • 相关阅读:
    【权限维持】window几种隐藏技术
    Flash XSS 漏洞实例
    nginx_lua_waf 部署、测试记录
    WAF Bypass数据库特性(Access探索篇)
    WAF Bypass数据库特性(MSsql探索篇)
    WAF Bypass数据库特性(Mysql探索篇)
    WAF Bypass数据库特性(Oracle探索篇)
    WAF Bypass 笔记(SQL注入篇)
    如何关闭Struts2的webconsole.html
    Windows Server 2008 R2 WSUS服务器的详细配置和部署
  • 原文地址:https://www.cnblogs.com/littilsaber/p/4472895.html
Copyright © 2011-2022 走看看