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 ...
随机推荐
- Select标签 依据value值默认选中 Jquery
网上找了非常多都是错的,不行的. 以下方法能够的 <script type="text/javascript"> $(document).ready(function( ...
- SetEvent/ResetEvent
在自己主动重置事件对象中,当WaitSingleObject/WaitForMultipleObjects接收到SetEvent发送过来的信号后则返回WAIT_OBJECT_0,此时操作系统(待定)自 ...
- [UI]抽屉菜单DrawerLayout分析(三)
在[UI]抽屉菜单DrawerLayout分析(一)和[UI]抽屉菜单DrawerLayout分析(二)中分别介绍了DrawerLayout得基本框架结构和ViewDragerHelper的作用以及手 ...
- 在 IIS 中配置 ASP.NET 应用程序
注意事项: 1.注册.NET 如果先安装.net平台,后安装IIS,那么在IIS中可能就没有出现ASP.NET版本的下拉菜单,就要手动注册: 一般.Net版本都存放在:C:\WINDOWS\Micr ...
- CentOS6.4下搭建hadoop2.2(64bit)注意事项
注:本文针对64位机器,32bit课直接tar -zxvf hadoop-2.2.0.tar.gz 解压配置即可. Step1:安装jdk(6以上版本) Step2:下载hadoop--->ht ...
- Java代码整理
- mac apktool配置
Apktool:http://ibotpeaches.github.io/Apktool/install/ 最新版本2.0.1 dex2jar: https://github.com/pxb1988/ ...
- MVC+JSON 无限滚动翻页
public partial class News { public int ID{ get; set; } public int Title{ get; set; } } ) { Response. ...
- AutoIt 函数学习之----Send函数
Send: 作用:向激活窗口发送模拟键击操作. 语法: send('按键'[,标志]) 参数: 按键:要发送的按键序列. 标志:[可选参数] 更改程序处理“按键”的方式: 标志 = 0 (默认),按 ...
- 请求(Request)的参数(Parameter)里包含特殊字符(#等)的正确处理方式
遇到一个问题 在一个地址链接(URL)里使用 url?param1=val1¶m2=val2 的方式传递参数,结果在获取参数值时发现不是当初设定的值. 具体案例 以特殊字符井号(#)为 ...