输入包括两行,第一行包含整数n(2 ≤ n ≤ 50),即数列的长度。 第二行n个元素x[i](0 ≤ x[i] ≤ 1000),即数列中的每个整数。
如果可以变成等差数列输出"Possible",否则输出"Impossible"。
3 3 1 2
Possible
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int min = 1000;
int sum = 0;
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int n = in.nextInt();
for(int i=0; i<n ; i++){
int b = in.nextInt();
sum += b;
min = Math.min(min, b);
}
int sum2 = (n-1)*n/2;
if( (sum-n*min) % sum2 == 0){
System.out.print("Possible");
}else{
System.out.print("Impossible");
}
}
}
} import java.util.HashSet;
import java.util.Scanner;
/**
* @Author: coderjjp
* @Date: 2020-05-13 16:53
* @Description: 等差数列
* @version: 1.0
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int num[] = new int[n];
int min = 0, max = 1000;
for (int i = 0; i < n; i++){
num[i] = sc.nextInt();
if (num[i] < min)
min = num[i];
if (num[i] > max)
max = num[i];
}
if (max == min){
System.out.println("Possible");
return;
}
if ((max - min) % (n - 1) != 0){
System.out.println("Impossible");
return;
}
int dis = (max - min) / (n - 1);
HashSet<Integer> set = new HashSet<>();
for (int i = 0; i < n; i++){
if (!set.add(num[i] - dis)){
System.out.println("Impossible");
return;
}
}
System.out.println("Possible");
}
} import java.util.*;
public class Main
{
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
while(sc.hasNextInt())
{
int n=sc.nextInt();
int [] arr=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
Arrays.sort(arr);
int d=arr[0]-arr[1];//公差
boolean flag=true;
for(int i=1;i<arr.length-1;i++)
{
int cur=arr[i]-arr[i+1];
if(cur!=d)
{
flag=false;
System.out.println("Impossible");
break;
}
}
if(flag)
{
System.out.println("Possible");
}
}
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner cin=new Scanner (System.in);
int n=cin.nextInt();//数组长度;
int a[]=new int[n];
for(int i=0;i<n;i++) {
a[i]=cin.nextInt();
}
Arrays.sort(a);
int left=1;
int right=n-1;
int temp=a[1]-a[0];
int x=0;
while (left<=right) {
if(a[left]-a[left-1] !=temp || a[right]-a[right-1] !=temp) {
x=1;
break;
}
left++;
right--;
}
if(x==0)System.out.print("Possible");
else if(x==1)System.out.print("Impossible");
}
}
import java.util.Scanner;
public class Main { public static Scanner scan = new Scanner(System.in); public static void main(String[] args) { int num = scan.nextInt(); int array[] = new int[num]; for (int i = 0; i < array.length; i++) { array[i] = scan.nextInt(); } for (int i = 0; i < array.length-1; i++) { for (int j = 0; j+1 < array.length-i; j++) { if(array[j]>array[j+1]) { int temp=array[j]; array[j]=array[j+1]; array[j+1]=temp; } } } int a= array[1]-array[0]; int count=0; for (int i = 1; i < array.length-1; i++) { if(array[i+1]-array[i]==a) { count++; continue; } } if(count==num-2) { System.out.println("Possible"); }else { System.out.println("Impossible"); } }
}
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
Boolean flag = true;
if (n > 2) {
Arrays.sort(arr);
int num = arr[1] - arr[0];
for (int i = 2; i < arr.length; i++) {
if (arr[i] - arr[i - 1] != num) {
flag = false;
break;
}
}
}
if (flag) {
System.out.println("Possible");
} else {
System.out.println("Impossible");
}
}
}
排序完之后,然后依次判断每个项是否都相差公差即可:
private static String solution(int[] arr) {
Arrays.sort(arr);
int d = arr[1] - arr[0]; // 公差
for (int i = 2; i < arr.length; i++) {
if (arr[i - 1] + d != arr[i]) {
return "Impossible";
}
}
return "Possible";
}
思路比较简单,等差数列,肯定是从大到小或者从小到大,将输入的数组排序, 然后将排列的数组间俩俩互减,然后将互减结果与公差比较,是一致输出对应结果 importjava.util.Arrays; import java.util.Scanner; public class shulie { public static void main(String[] args){ Scanner s=new Scanner(System.in); int a=s.nextInt(); int [] b=new int[a]; for (int i=0;i<a;i++){ b[i]=s.nextInt(); } for (int i=0;i<a;i++){ for (int j=i;j<a;j++){ if (b[i]>b[j]){ int temp; temp=b[i]; b[i]=b[j]; b[j]=temp; } } } Boolean flag=true; Arrays.sort(b); int d=b[1]-b[0]; for (int j=2;j<b.length;j++){ if (b[j]-b[j-1]!=d){ flag=false; } } if (flag){ System.out.print("Possible"); } else{ System.out.print("Impossible"); } } }
//已排好序的数组,判断后一项与前一项的差是否恒为常数
import java.util.Scanner;
import static java.util.Arrays.sort;
public class DengChaShuLie {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
int min = 1001;
int sum = 0;
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
sum+=arr[i];
if (arr[i] < min) {
min = arr[i];
}
}
int d = (sum-nmin)2/(n*(n-1));
sort(arr);
int i;
for (i = 0; i < n; i++) {
if (arr[i] != min + d * i) {
System.out.println("Impossible");
break;
}
}
if (i == n) {
System.out.println("Possible");
}
}
}
import java.util.Arrays;
import java.util.Scanner;
public class Main{
public static void result(int[] m){
Arrays.sort(m);
int d=m[1]-m[0];
int count=0;
for (int i = 0; i length-1; i++) {
if (m[i+1]==m[i]+d){
count++;
}
}
if (count==m.length-1){
System.out.println("Possible");
}else
System.out.println("Impossible");
}
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
int[] m=new int[n];
for (int i = 0; i ; i++) {
m[i]=scanner.nextInt(); }
Main.result(m); }
} import java.util.*;
public class Main{
public static void main(String[] args){
try(Scanner in = new Scanner(System.in)){
int n = Integer.parseInt(in.nextLine()),i = 0;
int[] a = new int[n];
while(in.hasNextInt()){
a[i++] = in.nextInt();
}
System.out.println(helper(a));
}
}
public static String helper(int[] a){
Arrays.sort(a);
int d = a[1] - a[0],i = 2;
while(i < a.length){
if(a[i] - a[i - 1] != d) return "Impossible";
i++;
}
return "Possible";
}
}
import java.util.Scanner;
import java.util.Arrays;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int num = in.nextInt();
int[] arr = new int[num];
for(int i = 0; i < num ;i++){
arr[i] = in.nextInt();
}
Arrays.sort(arr);
//需要定义一个布尔变量标记是否成功
boolean flag = true;
int d = arr[1] - arr[0];
for(int i = 2;i<arr.length;i++){
if(arr[i] - arr[i-1] != d){
flag = false;
}
}
if(flag){
System.out.println("Possible");
}else{
System.out.println("Impossible");
}
}
}