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出现的个数,比如如 ...
随机推荐
- 群主微信sdk说明地址
群主微信sdk说明地址官网地址:http://weixin.senparc.com/ 源代码及最新更新:https://github.com/JeffreySu/WeiXinMPSDK 最新DLL发布 ...
- 【Spring】Spring,我的零散使用杂记
通过Java类设置配置信息,JavaConfig Spring常用的通过XML或者@Controller.@Servoce.@Repository.@Component等注解注册Bean,最近看Spr ...
- spark读取hdfs数据本地性异常【转】
在分布式计算中,为了提高计算速度,数据本地性是其中重要的一环. 不过有时候它同样也会带来一些问题. 一.问题描述 在分布式计算中,大多数情况下要做到移动计算而非移动数据,所以数据本地性尤其重要,因此我 ...
- Java获取某个月的天数
https://blog.csdn.net/wtopps/article/details/48262221 ********************************************** ...
- CentOS 6.5 yum安装mysql5.6或其他版本【默认yum只能安装mysql 5.1】 by jason
by jason [备份配置文件] CentOS 6.5 默认yum只能安装mysql 5.1 安装前要检查机器原来是否安装过mysql,如有安装需要先进行数据备份.清理. [root@snails ...
- JS操作MongoDB
JavaScript处理MongoDB,更新数据: #!/bin/bash mongo=/home/zhangzhenghai/cluster/mongodb/bin/mongo if true; t ...
- 【机器学习】K近邻算法——多分类问题
给定一个训练数据集,对新的输入实例,在训练数据集中找到与该实例最邻近的K个实例,这K个实例的多数属于某个类,就把该类输入实例分为这个类. KNN是通过测量不同特征值之间的距离进行分类.它的的思路是:如 ...
- ORA-12541:TNS:无监听程序 配置Oracle Myeclipse无法连接上 花费一天时间解决掉的
背景:自己机子做oracle服务器,其他机子可以ping得通我的机子,但是jdbc就是连不上,后来用plsql连出现无监听程序.... 我昨天重新安装Oracle后,用PL/SQL Developer ...
- Android Studio OkHttpClient使用
本次来记录下OkHttpClient的使用,OkHttpClient是用来完成android 客户端对服务端请求的工具. 首先记住,使用网络的时候一定要加入权限,加入到AndroidMainfest. ...
- [转]JSP页面的动态包含和静态包含示例及介绍
原文地址:http://www.jb51.net/article/53659.htm 一.静态包含 本文介绍JSP静态包含语句,即使用JSP的include指令来完成的包含操作.JSP中,有两种包含其 ...