1. 程式人生 > >zoj 1180 Self Numbers(大數,靈活題)

zoj 1180 Self Numbers(大數,靈活題)

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.


Sample Output

1
3
5
7
9
20
31
42
53
64
|
| <-- a lot more numbers
|
9903
9914
9925
9927
9938
9949
9960
9971
9982
9993
|
|

找出1000000以內所有不能由其他數字(abc+a+b+c)組成的數

因為a+b+c+...最大也就是9+9+9+9+9+9=54,所以直接暴力即可

#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<cmath>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define ll long long
using namespace std;
int main(){
	for(int i=1;i<=1000000;++i){
		int u=0;
		for(int j=1;j<=54&&i-j>0;++j){
			int p=i-j,s=0;
			while(p>0){
				s+=p%10;
				p/=10;
			}
			if(s==j){
				u=1;
				break;
			}
		}
	  	if(u==0)
			cout<<i<<endl;
	}
	return 0;
}
不過上面的程式碼效率沒有下面的高:
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;
int hash[1000005];
void deal(int n){
    int rec = n;
    while( n > 0 ){
        int c = n % 10;
        n /= 10;
        rec += c;
    }
    hash[rec] = 1;
}
int main(){
    for(int i=1;i<=1000000;++i){
        if(!hash[i])
            printf("%d\n",i);
        deal(i);
    }
    return 0;
}

相關推薦

zoj 1180 Self Numbers大數靈活

In 1949 the Indian mathematician D.R. Kaprekar discovered a class of numbers called self-numbers. For any positive integer n, define d(n)

*【CodeForces - 214D 】Numbers dp組合數學

題幹: Furik loves writing all sorts of problems, especially such that he can't solve himself. You've got one of his problems, the one Furik gave to

2016女生賽 HDU 5710 Digit-Sum數學思維

accep ger sizeof memory fin left total src view Digit-Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Oth

【CodeForces - 215C 】Crosses 思維圖形

題幹: There is a board with a grid consisting of n rows and m columns, the rows are numbered from 1 from top to bottom

【CodeForces - 244A 】Dividing Orange 構造

題幹: One day Ms Swan bought an orange in a shop. The orange consisted of n·k segments, numbered with integers from 1 to n·k. There

【牛客 - 302哈爾濱理工大學軟體與微電子學院第八屆程式設計競賽同步賽低年級】小樂樂切割方塊思維

題幹:   小樂樂的作業本是2n*2n的方格本。 某天小樂樂的童鞋,想要考驗一下小樂樂。 他將小樂樂的一張方格紙中的某個格子(x,y)塗成黑色, 小樂樂能否在將4*4的方格本沿著方格邊緣且切割線與黑色方格不存在公共交點的情況下將方格本切割成兩部分。 兩部分可以通過旋

【UVA - 227】Puzzle 模擬

題幹:  Puzzle  A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contained 24 small squares of equal size.

leetcode筆記14771

程式:string longestCommonPrefix(vector<string>& strs) {        string result;       if(strs.size()>0){                  int minSize=strs[0].size

【CF#192 A】Funky Numbers 二分查詢

題幹: As you very well know, this year's funkiest numbers are so called triangular numbers (that is, integers that are representable as , w

1001 】Exponentiation Java大數高精度

題幹: Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of

zoj3987 Numbers大數+貪心

Numbers Time Limit: 2 Seconds      Memory Limit: 65536 KB DreamGrid has a nonnegative integer . He would like to divide into nonnegati

UVA ~ 11809 ~ Floating-Point Numbers 數學思維

題意 思路 假設該數字表示為X∗2YX*2^YX∗2Y,那我們要求解的就是X∗2Y=A∗10BX*2^Y=A*10^BX∗2Y=A∗10B,M=(X的位數),E=(Y的位數) 如果直接去計算這個數

大數相乘演算法相加相減

所謂大數相乘,就是指數字比較大,相乘的結果超出了基本型別的表示範圍,所以這樣的數不能夠直接做乘法運算。 假設有A和B兩個大數,位數分別為a和b。根據我們平常手動計算乘法的方式可以看出,最終的結果的位數c一定小於等於a+b。 由於數字無法用一個整形變數儲存,很自然的想到用字串

ZOJ 3736 Pocket Cube暴力魔方

魔方的題,暴力睡過。1930ms,差點久掛了。 #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <cmath> #include &

用單鏈表進行求平均數去掉最大數最小數

package DataStructureTestmain; import DataStructureTestSinglyLinkedList.Node; import DataStructure

CF 549H. Degenerate Matrix二分退化矩陣靈活

Note In the first sample matrix B is  In the second sample matrix B is  從頭到尾都理解錯了題目意思。。 題意:對A矩陣每個值做更改,使得得到的矩陣B為退化矩陣(ad-bc=0),求四個更改的差值裡的絕對值最大的差值的最小值。

STL語法——映射:map 反片語AnanagramsUVa 156

count lower code cin mos abc 最終 定義 lead Description Most crossword puzzle fans are used to anagrams--groups of words with the same let

用vue開發一個app2main.js

.html 第一次用 courier ace 第一次 router -s 提示 新建 昨天跟著vue的官網搭建了vue的一個腳手架,我也是第一次用VUE一切都在摸索階段。 今天試著看下裏面腳手架裏面有點什麽東西 先看看main.js

ccs3新特性---borderBackground部分

指定 其他 round 分隔 接收 source 縮小 20px 圓形 boder屬性新特性: border-radius 設置或檢索對象使用圓角邊框 border-top-left-radius 設置或檢索對象左上角圓角邊框 borde

Linux安裝yum的痛苦路程失敗慎入

1-1 一個 h+ thread php read centos lin png   1,在網上下載了一個yum 的 rpm文件(yum-3.2.29-81.el6.centos.noarch.rpm),我在 http://www.rpmfind.net/linux/rpm