2016年1月21日 星期四

d044: 00640 - Self Numbers

內容 :
在1949年印度數學家 D.R Kaprekar發現了一種數字:Self-numbers。對任何正整數 n ,定義d(n)為n加上其各數字的和。例如:d(75)=75+7+5=87。給任一個正整數 n 當作一個起始點,你可以產生無限的數字序列:n, d(n), d(d(n)), d(d(d(n))),…例如:如果你從33開始,下一個數字是33+3+3=39,再下一個數字是39+3+9=51,再下一個數字是51+5+1=57。所以你可以產生以下的序列:
33, 39, 51, 57, 69, 84, 96,111, 114, 120, 123, 129, 141, ……

我們稱n為d(n)的generator。在上面的例子中33是39的generator,39是51的generator,51是57的generator,以下類推。有些數有不只一個generator,例如:101有2個generators,91和100。如果一個數沒有generator,那他就是一個self-number。比100小的self-number:1, 3, 5, 7, 9, 20, 31, 42, 53, 64, 75, 86, 97

本問題是:找出所有小於或等於1000000的self-numbers。
輸入說明 : 
No input.
輸出說明 : 
範例輸入 : help
No input. 
範例輸出:
1
3
5
7
9
20
31
42
53
64
 |
 |       <-- a lot more numbers
 |
9903
9914
9925
9927
9938
9949
9960
9971
9982
9993
 |
 |
 |
提示 : 
標籤:
出處: 
UVa640 (管理:snail)


public class d044 {
public static void main(String[] args) {
int[] arr=new int[2000000];             //只開1000000的話 後面在加會爆掉
for(int i=1;i<=1000000;i++)
arr[i]=0;
for(int i=1;i<=1000000;i++){
int total=0;
String before=String.valueOf(i);
for(int j=0;j<before.length();j++){
total+=Integer.parseInt(String.valueOf(before.charAt(j)));
}
int after=i+total;
arr[after]=1;
}
for(int i=1;i<=1000000;i++){
  if(arr[i]==0){
  System.out.println(i);
  }
}
}

}

b587: 10918 - Tri Tiling

內容 :
給你一個3 * n的地面,用1 * 2的地板磚鋪滿,問有幾種方法。
輸入說明 : 
每行有一個整數n,代表為3 * n的地面,0 ≤ n ≤ 30,n=-1時代表輸入結束
輸出說明 : 
請對每一個輸入,輸出可能的排法數
範例輸入 : help
2
8
12
-1
範例輸出:
3
153
2131
提示 : 
DP
標籤:
出處: 
uva10918 (管理:pcshic)

import java.util.Scanner;

public class UVAb587 {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n;
int[] f=new int[32];
f[0]=1; f[2]=3;
for(int i=4;i<32;i+=2)
f[i]=4*f[i-2]-f[i-4];
while((n=sc.nextInt())!=-1){
System.out.println(f[n]);
}

}

}

d091: 00476 - Points in Figures: Rectangles

內容 :
在x-y平面上,給你一些矩形和一些點,請你回答這些點落在哪些矩形內(如果有的話)。另外,在這個問題中,剛好落在邊上的點不視為落在該矩形內。
輸入說明 : 
首先是矩形的資料,每個矩形一列,第1個字元代表圖形的類別(r 代表矩形),接下來有4個數值分別代表該矩形左上角及右下角的座標。矩形的個數不會超過10個。
以一列僅含有一個*代表矩形資料結束。
接下來的每列為一個點的座標,也就是要測試的點。若點座標為9999.9 9999.9代表輸入結束(此點不需輸出)
輸出說明 : 
對每一個測試的點,若其落在某矩形內,則輸出下列格式的訊息:
Point i is contained in figure j
如果某個點沒有落在任何矩形內,則輸出:
Point i is not contained in any figure
請注意:點和矩形的編號是按照他們出現在input的順序。請參考Sample Output
範例輸入 : help
r 8.5 17.0 25.5 -8.5
r 0.0 10.3 5.5 0.0
r 2.5 12.5 12.5 2.5
*
2.0 2.0
4.7 5.3
6.9 11.2
20.0 20.0
17.6 3.2
-5.2 -7.8
9999.9 9999.9
範例輸出:
Point 1 is contained in figure 2
Point 2 is contained in figure 2
Point 2 is contained in figure 3
Point 3 is contained in figure 3
Point 4 is not contained in any figure
Point 5 is contained in figure 1
Point 6 is not contained in any figure
提示 : 
Sample Input中矩形及測試點的圖


 * 中文翻譯:Lucky 貓
標籤:
出處: 
UVa476 (管理:MAPLEWING)

import java.util.Scanner;

public class UVAd091 {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String ch;
final int X1=0,Y1=1,X2=2,Y2=3;
double[][] arr =new double[10][4];
int c=0;
while(!(ch=sc.next()).equals("*")){
arr[c][X1]=sc.nextDouble();
arr[c][Y1]=sc.nextDouble();
arr[c][X2]=sc.nextDouble();
arr[c][Y2]=sc.nextDouble();
c++;
}
double x,y;
int count=1;
while((x=sc.nextDouble())!=9999.9 && (y=sc.nextDouble())!=9999.9){
boolean flag=true;
for(int i=0;i<c;++i){
if(x>arr[i][X1] && y<arr[i][Y1] && x<arr[i][X2] && y>arr[i][Y2]){
  System.out.println("Point "+count+" is contained in figure "+(i+1));
  flag=false;
   }
}
if(flag)
System.out.println("Point "+count+" is not contained in any figure");
count++;
}

}

}