解题思路请参考

代码(可以通过,不过很乱,需要整理):

/// <summary>
/// 计算n在数组[targetNum]中出现的次数
/// 形如:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
/// </summary>
/// <param name="targetNum"></param>
/// <param name="n"></param>
/// <returns></returns>
public static int GetCount(int targetNum, int n)
{
int count = ; if (n > && n <= )
{
int ratio = ; while (true)
{
int tmp = targetNum/ratio * ratio; if (tmp > )
{
count += tmp / ;
} int tmpA = targetNum - tmp; if (ratio == )
{
if (tmpA >= n)
{
count++;
}
}
else
{
int tmpB = tmpA/(ratio/);
if (tmpB > n)
{
count += *(ratio/);
}
else if (tmpB == n)
{
count += tmpA%(ratio/) + ;
}
}
ratio *= ; if (tmp == )
{
break;
}
}
}
else if (n == )
{
int ratio = ; while (true)
{
int tmp = targetNum / ratio * ratio; if (tmp > )
{
count += tmp / ;
} int tmpA = targetNum - tmp;
if (tmpA == targetNum)
{
if (tmpA > )
{
count -= ratio//;
}
else if (ratio == )
{
count = ;
}
break;
}
if (ratio == )
{
if (tmpA >= n)
{
count++;
}
}
else
{
int tmpB = tmpA / (ratio / );
if (tmpB > n)
{
count += * (ratio / );
}
else if (tmpB == n)
{
count += tmpA % (ratio / ) + ;
}
}
ratio *= ;
}
}
return count;
}

LintCode #3 统计数字的更多相关文章

  1. lintcode 中等题:digits counts 统计数字

    题目 统计数字 计算数字k在0到n中的出现的次数,k可能是0~9的一个值 样例 例如n=12,k=1,在 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],我们发现 ...

  2. NOIP2007 统计数字

    1.统计数字 (count.pas/c/cpp) [问题描述] 某次科研调查时得到了 n 个自然数,每个数均不超过 1500000000(1.5*109).已知不相同的数 不超过 10000 个,现在 ...

  3. 每日一练之排序算法(P1097 统计数字)

    某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5×10^9).已知不相同的数不超过10000个,现在需要统计这些自然数各自出现的次数,并按照自然数从小到大的顺序输出统计结果. ...

  4. java算法:统计数字-将数字转换成字符串,然后使用字符串String.valueOf()方法进行判断

    题目: 计算数字 k 在 0 到 n 中的出现的次数,k 可能是 0~9 的一个值. 样例 样例 1: 输入: k = 1, n = 1 输出: 1 解释: 在 [0, 1] 中,我们发现 1 出现了 ...

  5. 统计数字noip2007

    7909:统计数字 总时间限制:  1000ms 内存限制:  65536kB 描述 某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5*109).已知不相同的数不超过1000 ...

  6. 洛谷 P1097 统计数字

    P1097 统计数字 题目描述 某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5*10^9).已知不相同的数不超过10000个,现在需要统计这些自然数各自出现的次数,并按照自 ...

  7. Java练习 SDUT-1704_统计数字问题

    统计数字问题 Time Limit: 1000 ms Memory Limit: 32768 KiB Problem Description 一本书的页码从自然数1 开始顺序编码直到自然数n.书的页码 ...

  8. LintCode题解之统计数字

    直接硬搜就可以了,只是需要考虑k为0的情况. public class Solution { /* * @param : An integer * @param : An integer * @ret ...

  9. [CareerCup] 18.4 Count Number of Two 统计数字2的个数

    18.4 Write a method to count the number of 2s between 0 and n. 这道题给了我们一个整数n,让我们求[0,n]区间内所有2出现的个数,比如如 ...

随机推荐

  1. Sql Server 查询库表记录数

    ), RowCnt INT) EXEC sp_MSforeachtable 'INSERT INTO #temp SELECT ''?'', COUNT(*) FROM ?' desc DROP TA ...

  2. [Windows Azure] How to Deploy a Database to Windows Azure

    How to Deploy a Database to Windows Azure There are several different ways you can move an on-premis ...

  3. linux 命令(43):bash 快捷键操作

    ctrl 命令行快捷键: CTRL+A: – 快速移动到行首. 我们假设你输入了像下面这样的命令.当你在第 N 行时,你发现在行首字符有一个输入错误 $ gind . -mtime -1 -type ...

  4. 【web技术】html特效代码(二)

    html特效代码(一) html特效代码(二) 图片漂浮广告代码 <bodybgcolor="#F7F7F7"> <!--图片漂浮广告代码开始--> < ...

  5. iframe高度自适应方法大全

    http://www.3tii.com/993.html http://caibaojian.com/iframe-adjust-content-height.html http://www.jb51 ...

  6. hdu3191+hdu1688(求最短路和次短路条数,模板)

    hdu3191题意:求出次短路的长度和条数 #include<iostream> #include<cstdio> #include<cstring> #inclu ...

  7. MAC OSX 10.10 下启用自带的Apache的rewrite模块

    1.修改Apache配置文件 sudo vim /etc/apache2/httpd.conf LoadModule rewrite_module libexec/apache2/mod_rewrit ...

  8. 'Project Name' was compiled with optimization

    'Project Name' was compiled with optimizationhtml, body {overflow-x: initial !important;}html { font ...

  9. WPF中自定义路由事件

    public class MyButtonSimple: Button { // Create a custom routed event by first registering a RoutedE ...

  10. velocity单引号与双引号

    (1)最外层是用单引号包围时,双引号直接使用就可以了,两个连续的单引号表示一个单引号:#set($var2 = 'A"B''C') --> $var2 的值为 A"B'C(2 ...