問題描述:
螺旋圖形由小至大的正三角形依序排列而成,如圖所示
圖中的每個正三角形中的數字代表其邊長
三角形的排列方式為由內到外螺旋排列而成
每個三角形其邊長為鄰近三角形邊長之和
三角形的邊長與三角形的螺旋排序(由內到外),其序列編號與所對應的三角形邊長如下所示:
序列編號 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
邊長 | 1 | 1 | 1 | 2 | 2 | 3 | 4 | 5 | 7 | 9 | 12 | 16 |
請寫出程式,求出任意序列編號時,求出對應的正三角形的邊長為多少?
輸入說明:
輸入一個小於70的三角形序列編號,例如8。
輸出說明:
輸出序列編號8對應的三角形邊長7。
public class Problem_5 {
public static void main(String[] args) {
java.util.Scanner sc = new java.util.Scanner(System.in);
int input = sc.nextInt();
if(input <= 70){
System.out.println(Tfun(input));
}
}
public static int Tfun(int side)
{
if(side <= 2){
return 1;
}else if(side == 3 || side == 4){
return 2;
}else{
return (Tfun(side-5) + Tfun(side-1));
}
}
}
沒有留言:
張貼留言