题目描述:阳阳突发奇想画起了三角!...........并且在三角每个位置上填上了一个数字,然后按照Z字型给数字一个编号......

解题思路:之后补充

【笔试】-画三角

解法:

package interview;

public class Triangle {
	
	public static String solution(int n) {
		
		if(n==1)
			return "1/1";
		
		int[] f = new int[n+1];
		int x = 1,y=1;
		int turn =0,index=0;
		f[1]=1;
		
		for(int i=2;i<n+1;i++) {
			f[i]=i+f[i-1];
			if(n<=f[i]) {//表示在这一轮
				turn = i;
				index = n-f[i-1];//表示在这轮中的第几个
				if(turn%2==0) {
					x=index;
					y=turn+1-x;
				}else {
					y=index;
					x=turn+1-y;
				}
				break;
			}
		}
		return x+"/"+y;
	}
	
	public static void main(String[] args) {
		
		for(int i=1;i<22;i++) {
			String num = solution(i);
			System.out.println(num);
		}
	}
}

 

相关文章:

  • 2022-01-09
  • 2021-10-31
  • 2022-12-23
  • 2022-12-23
  • 2021-11-20
  • 2021-11-25
  • 2021-11-20
猜你喜欢
  • 2022-02-17
  • 2021-05-12
  • 2021-10-05
  • 2022-12-23
  • 2021-07-11
  • 2021-09-04
  • 2021-09-05
相关资源
相似解决方案