所以我需要弄清楚这个问题。你会怎么做?
给定三个整数,一个b c,其中一个是小的,一个是中等的
一个很大。如果三个值均匀分布,则打印为真,
所以中小的差异与中国的相同
中大差异。
(2, 4, 6) -> true
(4, 6, 2) -> true
(4, 6, 3) -> false
答案 0 :(得分:5)
没有必要进行任何排序,输入数量如此之少。
由于只有三种可能性,你可以单独检查它们,"或"结果。
boolean isArithmeticProgression =
(a - b == b - c) || (a - c == c - b) || (a - b == c - a)
答案 1 :(得分: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;
}
}
答案 2 :(得分:0)
由于您的数组似乎是一个小数组,为什么不尝试线性遍历?因此,当您遍历大小为(n-1)
的数组时,取两个元素的abs(diff)
,然后取另外两个abs(diff)
等,如果diff恰好相同,{{1 }} else return true
。所有这一切都在循环中发生......
时间false
PS:我不建议排序,因为你的要求只是看看2个元素之间的差异是不变的。排序是不必要的。
答案 3 :(得分: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;
}
答案 4 :(得分:0)
您可以做的一件事就是计算small
,medium
和large
,这样您的整体顺序就不再重要了。
然后,您只需检查large
和medium
之间的差异是否与medium
和small
相同,相应地返回true或false。
下面是一个简单的代码:
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));
}
答案 5 :(得分: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;
}
答案 6 :(得分:0)
boolean method (int a, int b, int c)
{
return ( a*3==a+b+c || b*3==a+b+c || c*3==a+b+c );
}
//如果空格为偶数,则给定的整数之一是其总和的平均值。
答案 7 :(得分:0)
尝试以下代码:
int temp;
if(a > b) {
temp = a;
a = b;
b = temp;
}
if(b > c) {
temp = b;
b = c;
c = temp;
}
if(a > b) {
temp = a;
a = b;
b = temp;
}
return b - a == c - b;
答案 8 :(得分:0)
if(a==b&&b!=c||a==c&&c!=b||b==c&&c!=a){
return false;
}
return a-b==b-c||b-c==c-a||c-a==a-b;
}
这应该对您有用。
答案 9 :(得分:0)
public boolean evenlySpaced(int a, int b, int c) {
int small = Math.min(a,b);
small = Math.min(small,c);
int large = Math.max(a,b);
large = Math.max(large,c);
int medium = (small + large)/2;
boolean flag = false;
if(a == medium || b == medium || c == medium)
flag = true;
return ((large - medium) == (medium - small) && flag);
}
答案 10 :(得分:-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);
}
}
答案 11 :(得分:-1)
public boolean evenlySpaced(int a, int b, int c) {
int max = Math.max(a, Math.max(b,c));
int min = Math.min(a, Math.min(b,c));
int mid = (a+b+c) - max - min;
return max - mid == mid - min;
}