HOJ1087
Self Numbers
| My Tags | (Edit) |
|---|
| Source : ACM ICPC Mid-Central USA 1998 | |||
| Time limit : 5 sec | Memory limit : 32 M | ||
Submitted : 1443, Accepted : 618
In 1949 the Indian mathematician D.R. Kaprekar discovered a class of numbers called self-numbers. For any positive integer n, define d(n) to be n plus the sum of the digits of n. (The d stands for digitadition, a term coined by Kaprekar.) For example, d(75) = 75 + 7 + 5 = 87. Given any positive integer n as a starting point, you can construct the infinite increasing sequence of integers n, d(n), d(d(n)), d(d(d(n))), .... For example, if you start with 33, the next number is 33 + 3 + 3 = 39, the next is 39 + 3 + 9 = 51, the next is 51 + 5 + 1 = 57, and so you generate the sequence
33, 39, 51, 57, 69, 84, 96, 111, 114, 120, 123, 129, 141, ...
The number n is called a generator of d(n). In the sequence above, 33 is a generator of 39, 39 is a generator of 51, 51 is a generator of 57, and so on. Some numbers have more than one generator: for example, 101 has two generators, 91 and 100. A number with no generators is a self-number. There are thirteen self-numbers less than 100: 1, 3, 5, 7, 9, 20, 31, 42, 53, 64, 75, 86, and 97.
Write a program to output all positive self-numbers less than or equal 1000000 in increasing order, one per line.
题目大意为打印小于1000000以内的自私数
#include<iostream>
using namespace std; long MaxSize = ; long Dc(long Num){
long D = ;
D = Num + (Num%) + (Num/)% + (Num/)% + (Num/)% + (Num/)% +(Num/)%;
return D;
} int main(){
bool List[MaxSize];
for(long i = ;i <= MaxSize;i++){
if(!List[i]) printf("%d\n",i);
long no_self = i;
while(no_self <= MaxSize && !List[no_self]){
no_self = Dc(no_self);
if(no_self <= MaxSize)
List[no_self] = ;
}
}
return ;
}
问题出在17~21行,这段while循环体实质上并没有让外循环for的指标进行非线性变动,即while循环完全是做无用功。这与埃氏筛有本质不同。但观察到,这段代码只需要给出下一个非Self number的序号即可,因此while循环就可以全部摘去,采用在线处理算法的思想,整个算法的复杂度直接将为O(N),下面为AC代码:
/*This Code is Submitted by mathmiaomiao for Problem 1087 at 2015-08-21 23:21:22*/
#include <iostream> using namespace std; bool List[];
int main() {
long no_self = ;
for(long i = ; i < ; ++i) {
if(!(List[i])) printf("%ld\n",i);
no_self = i + (i%) + (i/)% + (i/)% + (i/)% + (i/)% +(i/)%;
List[no_self] = ;
}
printf("1000000\n");
return ;
}
HOJ1087的更多相关文章
- OJ题目分类
POJ题目分类 | POJ题目分类 | HDU题目分类 | ZOJ题目分类 | SOJ题目分类 | HOJ题目分类 | FOJ题目分类 | 模拟题: POJ1006 POJ1008 POJ1013 P ...
随机推荐
- Zookeeper介绍
Zookeeper是一个分布式的开源系统,目的是为分布式应用提供协调一致性服务. 分布式应用可以在Zookeeper提供的简单原语集之上构造更高层次的服务.比如统一命名服务.状态同步服务.集群管理.分 ...
- spring security执行流程图
今天看到非常多人转载了这篇文章,这里备注一下.原文来自CSDN我的博客. 近期在研究spring security的配置,研究了一个星期了,在官网看了下.仅仅配置出来了简单的登录,但不知如何从数据库读 ...
- gcc中-pthread和-lpthread的区别
最近在使用linux mint15,里面自带的gcc时4.7的,当我编译多线程程序时,使用-lpthread居然说没有找到线程库函数!!!然后man了一下,才发现在gcc 4.7中链接线程库使用-pt ...
- 基于HTML5 Canvas的网页画板实现教程
HTML5的功能非常强大,尤其是Canvas的应用更加广泛,Canvas画布上面不仅可以绘制任意的图形,而且可以实现多种多样的动画,甚至是一些交互式的应用,比如网页网版.这次我们要来看的就是一款基于H ...
- Oracle的总体回顾
1.多表查询:一张以上的表进行查询,称为多表查询,多表查询的时候可以为表指定别名的方式以简化查询列的编写,在多表查询中,会产生笛卡尔积,就是两张表的总数相乘得到的结果,如果要想消除笛卡尔积要通过关联条 ...
- Struts 2.x Unable to load configuration. - action
问题分析:遇到该问题一般是struts中某个配置文件没有正确配置,比如: 1.class中的TestAction没有成功加载: <constant name="struts.i18n. ...
- ResultSet与Result
微软的.NET平台上面的数据访问有一个特点,就是数据查询的结果,可以放在内存中,以XML格式进行描述,不需要一直与数据库保持在线连接,用DataSet + Data Adapter来实现! 而在JDB ...
- QF——关于iOS的强引用,弱引用及strong,retain,copy,weak,assignd的关系
强引用和弱引用: 我们已经知道OC中的内存管理是通过“引用计数器”来实现的.一个对象的生命周期取决于它是否还被其他对象引用(是否retainCount=0).但在有些情况下,我们并不希望对象的销毁时间 ...
- QF——UI之几种常用的隐藏键盘的方法
怎么在填写完UITextField之后,点击空白处,隐藏软键盘. 下面两个方法都可以隐藏键盘 [tf resignFirstResponder]; 停止textfield的第一响应者 [self.vi ...
- Mysql表锁定解决
#查看进程SELECT *FROM information_schema.processlistWHERE USER = 'root' AND state LIKE 'Waiting%';#杀掉进程K ...