请问支撑数的支撑数用Java怎么写

IceSolitude 2020-02-18 11:02:17
题目:在已知的一组整数中,有这样一种数非常怪,它们不在第一个,也不在最后一个,而且刚好都比左边和右边相邻的数大。如:1 3 2 12 1 5 3 10 7 9 8 23 85 43,这些数中被标注红色字体的都是支撑数。将这些支撑数重新排列成一组数:3 12 5 10 9 85,其中被标注蓝色字体的数又是新数列中的支撑数,将蓝色数重新排列后又得到一组新数:12 10,这时再也找不到支撑数了,最后将12 10输出来。

我自己写了但只可以执行一次:
import java.util.Scanner;

public class Test1092 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int b, i;
b = sc.nextInt();
int a[] = new int[b];
for (i = 0; i < b - 1; i++) {
a[i] = sc.nextInt();
}

for (i = 0; i < b - 1; i++) {
if (a[i] > a[i + 1] && a[i] > a[i - 1]) {
System.out.print(a[i] + " ");
}

}

}

}
...全文
648 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
三仙半 2020-02-18
  • 打赏
  • 举报
回复
public static void main(String[] args) {
// 原始数据
Integer[] src = new Integer[] { 1, 3, 2, 12, 1, 5, 3, 10, 7, 9, 8, 23, 85, 43 };

// 保存支撑数的临时列表。其内容用原始数据填充是为了循环需要。
List<Integer> targetList = Arrays.asList(src);
// 标志变量,当前轮操作中是否找到支撑数,如果没有找到,则运算结束
boolean has = true;
Integer num;
while (has) {
// 将临时列表的数据导入成新的原始数据
src = new Integer[targetList.size()];
src = targetList.toArray(src);
printArray(src);
// 构造新一轮操作的临时列表
targetList = new ArrayList<>();

has = false;
// 循环查找支撑数
for (int i = 1; i < src.length - 1; i++) {
num = src[i];
if (num > src[i - 1] && num > src[i + 1]) {
targetList.add(num);
// 找到至少一个支撑数
has = true;
}
}

}

}

/**
* 输出数组
*
* @param data
*/
private static void printArray(Integer[] data) {
for (int i = 0; i < data.length; i++) {
System.out.print(data[i] + "\t");
}
System.out.println();
}
qq_39936465 2020-02-18
  • 打赏
  • 举报
回复
引用 楼主 qq_24662325 的回复:
我自己写了但只可以执行一次:

public class Test13 {
	//申明一个静态Integer数组
	static Integer[] s;

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int b, i;
		b = sc.nextInt();
		Integer a[] = new Integer[b];
		for (i = 0; i < b; i++) {
			a[i] = sc.nextInt();
		}
		SupportArray(a);
		for (i = 0; i < s.length; i++) {
			System.out.print(s[i] + ",");
		}
	}

	static void SupportArray(Integer[] t) {
		//申明一个list
		List<Integer> list = new ArrayList<Integer>();
		if (t.length >= 3) {
			for (int i = 1; i < t.length - 1; i++) {
				if (t[i] > t[i - 1] && t[i] > t[i + 1]) {
					//把符合条件的数添加入list
					list.add(t[i]);
				}
			}
			//如果list不为空
			if (list.size() > 0) {
				//把list转为Integer数组存入静态数组中
				s = list.toArray(new Integer[0]);
				//再次递归
				SupportArray(s);
			}
		}
	}
}

qybao 2020-02-18
  • 打赏
  • 举报
回复
你只查找了一次,要一直查找到没支撑数为止,所以你的程序再多套一层循环就可以了
import java.util.Scanner;

public class Test1092 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int b, i;
b = sc.nextInt();
int a[] = new int[b];
for (i = 0; i < b - 1; i++) {
a[i] = sc.nextInt();
}

int[] c = new int[b]; //用个临时数组保存支撑数查找结果
while (true) { //多套一层循环,直到找不到支撑数为止
int idx = 0; //c临时数组的下标
for (i = 1; i < b - 1; i++) { //这里要从1开始,否则会i-1会数组越姐
if (a[i] > a[i + 1] && a[i] > a[i - 1]) {
//System.out.print(a[i] + " ");
c[idx++] = a[I]; //把找到的支撑数保存到临时数组,c的下标跟着移动
}
}
if (idx == 0) break; //如果c的下标是0说明找不到支撑数,则推出while循环
b = idx; //获得保存支撑数的临时数组的长度
System.arraycopy(c, 0, a, 0, b); //把临时数组复制给a,重新查找
}
for (i=0; i<b; i++) { //打印最后结果
System.out.print(a[i] + " ");
}
}
}
  • 打赏
  • 举报
回复

public class Test {

public static void main(String[] args) {
int[] a = {1, 3, 2, 12, 1, 5, 3, 10, 7, 9, 8, 23, 85, 43};
int[] b = new int[a.length/2];

while (true) {
int index = 0;
for (int i = 1; i < a.length - 1; i++) {
if (a[i] > a[i - 1] && a[i] > a[i + 1]) {
b[index++] = a[i];
}
}
if (index == 0) {
break;
} else {
a = arrSub(b,index);
}
}
System.out.println(Arrays.toString(a));
}

public static int[] arrSub(int[] arr, int len) {
int[] r = new int[len];
for (int i = 0; i < len; i++) {
r[i] = arr[i];
}
return r;
}
}

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧