2014-04-05 71 views
-3

所以我需要找出这个问题的类。你会怎么做?寻找整数是否均匀分布

给定三个整数,a b c,其中一个小,一个中等,
一个很大。如果三个值均匀间隔打印真假,
所以中小两者的​​区别是大中小相差

(2, 4, 6) -> true           
(4, 6, 2) -> true 
(4, 6, 3) -> false 
+0

首先,在阵列中对它们进行排序,计算出[1]'和'一个[0]',然后循环之间'的差并查看是否存在'n [n] - a [n] - a [n-1]'不是计算出的第一个差异 – fge

回答

0

由于你的数组似乎是一个小数组,为什么不尝试线性遍历呢?因此,如果遍历的数组大小为(n-1),则取2个元素的abs(diff),如果diff恰好相同,则采用abs(diff)等于2等。return true否则为false。这一切是发生在一个循环...

时间O(n)

PS:我不是建议排序为您的要求是只是为了看看2个元素之间的差异是恒定的。排序是不必要的。

+0

算法对于(4,6,2)如何工作? –

0

此方法应该可以解决问题:

public boolean areEvenlySpaced(int a, int b, int c) { 
     Integer[] ints = {a, b, c}; 
     Arrays.sort(ints, new Comparator<Integer>() 
     { 
      @Override 
      public int compare(Integer x, Integer y) 
      { 
      return x - y; 
      } 
     }); 
     if ((ints[0]-ints[1]) == (ints[1]-ints[2])) return true; 
     else return false; 
    } 
+0

它会如何处理这种情况,'(4,6,2) - > true'? – sakura

+0

好点:)我修改了它并添加了数组排序。没有运行它,所以我希望没有错别字:P – Niklas

+0

使用Integer vs int的复杂性是否真的有必要? –

5

没有必要对任何排序,利用这样的小数目的输入。

由于只有三种可能性,您可以单独检查它们中的每一个,以及“或”结果。

boolean isArithmeticProgression = 
    (a - b == b - c) || (a - c == c - b) || (a - b == c - a) 
+0

+1注意他的问题表示三个整数,而不是任意数字。 –

+0

(但我对实际算法有点困惑。) –

+0

是啊,也许我应该刚才写'(A - B == b - C)||(一 - ç==Ç - B)||(A - B ==Ç - 一)'以提高可读性。我会改变它。 –

1

虽然David Wallace提到,你应该只减去值,因为你有这样一个小的组数字 - 如果你有上百号,任意大小以下的作品。

import java.util.*; 

public class EvenSpaces { 
    public static void main(String[] args) { 
    int[][] data = { 
     { 2, 4, 6 }, //-> true           
     { 4, 6, 2 }, //-> true 
     { 4, 6, 3 } //-> false 
    }; 
    for (int[] d: data) { 
     System.out.printf("%s - %s%n", toList(d), areEvenlySpaced(toList(d))); 
    } 
    } 
    public static boolean areEvenlySpaced(List<Integer> list) { 
    Collections.sort(list, new Comparator<Integer>() { 
     public int compare(Integer i1, Integer i2) { 
      return i2.compareTo(i1); 
     } 
    }); 
    if (list != null && list.size() > 1) { 
     int lastDiff = list.get(0) - list.get(1); 
     for (int i = 1; i < list.size(); i++) { 
     int diff = Math.abs(list.get(i-1) - list.get(i)); 
     if (lastDiff != diff) return false; 
     lastDiff = diff; 
     } 
     return true; 
    } 
    return false; 
    } 
    public static List<Integer> toList(int[] values) { 
    List<Integer> list = new ArrayList<Integer>(values.length); 
    for (int n : values) list.add(n); 
    return list; 
    } 
} 
-1
public boolean evenlySpaced(int a, int b, int c) { 
     int arr[]={a,b,c}; 
     Arrays.sort(arr); 
     boolean flag=false; 
     int diff=arr[1]-arr[0]; 
     for (int i = 0; i < arr.length-1; i++) { 
      if (arr[i+1]-arr[i]==diff) { 
       flag=true; 
      }else{ 
       flag=false; 
       break; 
      } 
     } 
return flag; 
} 
+0

您的回答是相当复杂的返回ARR [1] -arr [0] == ARR [2] -arr [1]; –

0

一两件事你可以做的是计算smallmediumlarge所以你的整数的顺序将不再重要。

然后,您可以简单地检查largemedium之间的差异是否与mediumsmall相同,并相应地返回true或false。

下面是一个smple代码:

static boolean spacedEvenly(int a, int b, int c) { 

    int large = Math.max(a, Math.max(b, c)); 
    int small = Math.min(a, Math.min(b, c)); 
    int medium = (a + b + c) - (large + small); 

    return ((large - medium) == (medium - small)); 
} 
0
public boolean evenlySpaced(int a, int b, int c) { 
    int[] jawn = {a, b, c}; 
    java.util.Arrays.sort(jawn); 
    if(a == b) { 
    if(b == c) return true; 
    } 
    if(Math.abs(jawn[0] - jawn[1]) == Math.abs(jawn[1] - jawn[2])) return true; 
    return false; 
} 
+1

这是不必要的复杂,这是不正确的'{-2,4,6}'。 – shmosel

+0

@shmosel'abs'应用于差异,而不是单个数字。它可以很好地处理你的测试案例。 –

+0

@JohnKugelman我纠正了。尽管我没有看到这一点。 – shmosel

-1
/* 
3. Create a method spacedEvenly thats given three ints, a b c, one of them 
is small, one is medium and one is large. Print true if the three values are 
evenly spaced. So the difference from small to medium is the same as the 
difference between medium and large. 
spacedEvenly(2, 4, 6) → true 
spacedEvenly(-1, 0, 1) → True   
spacedEvenly(6, 1, 7) → false */ 

package javaexam3; 
import java.util.Scanner; 
public class JavaExam3 { 

public static String spacedEvenly(String n) { 
Scanner input = new Scanner(System.in); 
n = ""; 

    System.out.print("What is the first integer declared as variable a?: "); 
int a = input.nextInt(); 
    System.out.print("What is the second integer declared as variable b?:"); 
int b = input.nextInt(); 
    System.out.print("What is the third integer declared as variable c?: "); 
int c = input.nextInt(); 

    if(((b - a) + b == c) && ((c - b) + a == b)) 
      { 
       n = " True"; 
      } 

    else { 
       n = " False"; 
      } 

    System.out.println("\nOkay! Are " + a + "," + b + "," + c + " evenly 
         spaced? " + n + "\n\n"); 
    return n; 
} 

public static void main(String[] args) { 
//Scanner input = new Scanner(System.in); 

String n = "nothing"; 

    System.out.print("Web Development Fundamentals\n\n "); 
    System.out.print("OK, Give me three integers 'a, b, and c'.\nIf they are 
        evenly spaced I will tell you, " 
      + "if in fact, it is true.\nIf the intgers are not even spaced," 
      + "I will declare the statement to be false! \n\nGOT IT?\n\t" 
      + "OKAY ...LETS BEGIN.\n\n"); 

    spacedEvenly(n); 

} 
}