神奇的数列之“Last Defence ”

题目大意:给你两个数字,之后的每个数字都等于这两个数字之差的绝对值。问这列数字有多少个不同的数字。

样例输入/输出:input  7 4;output  6;

解题思路:此题使用辗转相除法的思想。首先两个数字x,y(假定y>x),则一定存在y=kx+b;例如100和7。

然后会发现这个数列是  100 7 93 86 7 79 72 7......那么这个数列中不同的数字应该是100/7个  最后会得到  7  2...... ;

然后重复上述动作,直至出现0;

AC代码:

 1 import java.util.*;
 2 
 3 public class Main{
 4     public static void main(String[] args){
 5         Scanner sc = new Scanner(System.in);
 6         long T = sc.nextLong();
 7         long k = 1L;
 8         while(T > 0){
 9             long x = sc.nextLong();
10             long y = sc.nextLong();
11             long sum = 0L;
12             long mod = -1L;
13             if(x == 0 && y != 0){System.out.println("Case #" + k + ": 2");}
14             else if(x != 0 && y == 0){System.out.println("Case #" + k + ": 2");}
15             else if(x == 0 && y == 0){System.out.println("Case #" + k + ": 1");}
16             else{
17                 while(mod != 0){
18                     if(x < y){long t = x;x = y;y = t;}
19                     sum = sum + x/y;
20                     mod = x%y;
21                     x = mod;
22                 }
23                 sum = sum + 1;
24                 System.out.println("Case #" + k + ": " + sum);
25             }
26             k = k + 1;T = T - 1;
27         }
28     }
29 }

 一定要注意若是两个数字有一个是0,那么输出2,都是0,输出1.

原文地址:https://www.cnblogs.com/love-fromAtoZ/p/7244176.html