比大小
比大小
时间限制: C/C++语言2000MS其他语言4000MS内存限制: C/C++语言65536KB;其他语言589824KB题目描述:
写个函数,给定个整数数组,返回个数组。该返回数组中第i个数字为,原数组中第i个的数字至少往右走多少步才能遇到此它大的数字。如果遇不到或者已经处于最右的位置,则道为-1。
Console输入为多行,第一行为一个整数N, 1≤N≤10^6
接下来共有N行, 每行为个整数M, 0≤M≤2^32-1
输出
返回值为转换后的整数数组,一共有N行,每行为一个整数
样制输入
5
91
10
3
22
40
样例输出
-1
2
1
1
-1
#java代码
import java.util.Scanner;
public static void main(String[] args){
int count = 0,cnt=0;
Scanner scanner = new Scanner(System.in);
int a=0;
count = scanner.nextInt();
int[] arr = new int[count];
int[] index = new int[count];
while (cnt<count){
a = scanner.nextInt();
arr[cnt]=a;
cnt++;
}
boolean flag = false;
for (int i = 0; i <count ; i++) {
int step=1;
for (int j = i+1; j < count; j++) {
if (arr[j]>arr[i]){
flag = true;
index[i]=step;
break;
}else {
flag = false;
step++;
}
}
if (flag == false){
index[i]=-1;
}
flag = false;
}
for (int i:index){
System.out.println(i);
}
}