POJ 1316 (筛选法模拟)

来源:互联网 发布:淘宝退货太多会怎么样 编辑:程序博客网 时间:2024/05/16 23:01


采用筛选法模拟,设筛为数组g[],其中g[y]=x 表明y是x递增序列的一个数。故y为非自数。


#include <stdio.h>const unsigned N = 10000;unsigned g[N];unsigned sum_digit (unsigned x){  //计算d(x);if (x < 10) return x;unsigned sum=0;while (x){sum += x % 10;x /= 10;}return sum;}void sequence (unsigned x){  //找出一以x开头的一个递增序列while (x < N){unsigned next = x + sum_digit (x);if (next > N || g[next]!=next)return;g[next] = x;x = next;}}int main (){for (unsigned i=1;i<=N;i++)g[i]=i; //初始所有数为自数。for (unsigned i=1;i<=N;i++)sequence (i);for (unsigned i=1;i<N;i++)if (g[i] == i)printf ("%u\n",i);return 0;}

0 0
原创粉丝点击