2010年9月12日日曜日

PKU Judge Online 1316 Self Numbers

■1316 Self Numbers

□Problem
自然数nに対し,d(n)をn+(各桁の総和)と定義する.例えば,d(75)=75+7+5=87.n=d(m)となるmが存在しないnをself-numberと呼ぶ.10000以下のself-numberを全て表示せよ.

□Solution
やるだけ.

□Code
  1. package p1316;  
  2.   
  3. import java.util.*;  
  4. import java.lang.*;  
  5. import java.math.*;  
  6. import java.io.*;  
  7.   
  8. import static java.lang.Math.*;  
  9. import static java.util.Arrays.*;  
  10.   
  11. public class Main{  
  12.   
  13.     Scanner sc=new Scanner(System.in);  
  14.   
  15.     final int INF=Integer.MAX_VALUE;  
  16.     final double EPS=1e-9;  
  17.   
  18.     void run(){  
  19.         int n=10000;  
  20.         int[] a=new int[n];  
  21.         for(int i=1; i<n; i++){  
  22.             int m=i;  
  23.             int s=i;  
  24.             while(m!=0){  
  25.                 s+=m%10;  
  26.                 m/=10;  
  27.             }  
  28.             if(s<n)  
  29.                 a[s]=1;  
  30.         }  
  31.         for(int i=1; i<n; i++)  
  32.             if(a[i]==0)  
  33.                 println(i+"");  
  34.     }  
  35.   
  36.     void print(String s){  
  37.         System.out.print(s);  
  38.     }  
  39.   
  40.     void println(String s){  
  41.         System.out.println(s);  
  42.     }  
  43.   
  44.     public static void main(String[] args){  
  45.         // System.setOut(new PrintStream(new BufferedOutputStream(System.out)));  
  46.         new Main().run();  
  47.     }  
  48. }  

0 件のコメント: