LintCode #3 统计数字
代码(可以通过,不过很乱,需要整理):
/// <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 统计数字的更多相关文章
- 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],我们发现 ...
- NOIP2007 统计数字
1.统计数字 (count.pas/c/cpp) [问题描述] 某次科研调查时得到了 n 个自然数,每个数均不超过 1500000000(1.5*109).已知不相同的数 不超过 10000 个,现在 ...
- 每日一练之排序算法(P1097 统计数字)
某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5×10^9).已知不相同的数不超过10000个,现在需要统计这些自然数各自出现的次数,并按照自然数从小到大的顺序输出统计结果. ...
- java算法:统计数字-将数字转换成字符串,然后使用字符串String.valueOf()方法进行判断
题目: 计算数字 k 在 0 到 n 中的出现的次数,k 可能是 0~9 的一个值. 样例 样例 1: 输入: k = 1, n = 1 输出: 1 解释: 在 [0, 1] 中,我们发现 1 出现了 ...
- 统计数字noip2007
7909:统计数字 总时间限制: 1000ms 内存限制: 65536kB 描述 某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5*109).已知不相同的数不超过1000 ...
- 洛谷 P1097 统计数字
P1097 统计数字 题目描述 某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5*10^9).已知不相同的数不超过10000个,现在需要统计这些自然数各自出现的次数,并按照自 ...
- Java练习 SDUT-1704_统计数字问题
统计数字问题 Time Limit: 1000 ms Memory Limit: 32768 KiB Problem Description 一本书的页码从自然数1 开始顺序编码直到自然数n.书的页码 ...
- LintCode题解之统计数字
直接硬搜就可以了,只是需要考虑k为0的情况. public class Solution { /* * @param : An integer * @param : An integer * @ret ...
- [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出现的个数,比如如 ...
随机推荐
- C++ pair(对组)用法
类模板:template <class T1, class T2> struct pair 参数:T1是第一个值的数据类型,T2是第二个值的数据类型. 功能:pair将一对值组合成一个值, ...
- HBase最佳实践-用好你的操作系统
终于又切回HBase模式了,之前一段时间因为工作的原因了解接触了一段时间大数据生态的很多其他组件(诸如Parquet.Carbondata.Hive.SparkSQL.TPC-DS/TPC-H等),虽 ...
- 【Socket】linux网络扫描程序开发
1.mystery引入 1)系统入侵步骤:系统发现->漏洞探测->漏洞利用->痕迹清除 2)扫描器分类:主机与网络扫描器:端口服务扫描器:服务漏洞扫描器 3)T ...
- Git GUI中文乱码问题解决方法
使用Git GUI的查看代码的时候,会出现中文乱码: 在乱码的区域点击鼠标右键,选择Encoding,然后选择Unicode(UTF-8),乱码问题解决:
- maven profile 多环境
<profiles> <profile> <!-- 本地开发环境 --> <id>dev</id> <properties> & ...
- js 将数字转换成人民币大写的方法
//将数字转换成人民币大写的方法 var digitUppercase = function (n) { var fraction = ['角', '分']; var digit = [ '零', ' ...
- nexus 手动更改 私服包
替换 linux 私服下的 nexus 目录
- Yum Priorities
简介 Linux 发行版比较多,同时还有很多个人或组织维护了某些特定用途的安装/升级源.Yum Priorities 插件可以用来强制保护源.它通过给各个源设定不同的优先级,使得系统管理员可以将某些源 ...
- shell 删除文件过期文件
删除创建时间大于30天的文件 find /archivelog -ctime + | xargs rm -rf -amin n #查找系统中最后N分钟访问的文件 -atime n #查找系统中最后n* ...
- java中的元数据
java中的Annotation和C#中的Attribute相似. 写法上差别较大 @Target(ElementType.METHOD) @Retention(RetentionPolicy.CLA ...