題目連結
題意:
每組測資有若干高度的牆,Mario站在第一個
計算從第一個到最後的過程,高度增加與減少的次數
例如:
高度 1 ~ 2 增加
高度 3 ~ 2 減少
高度 2 ~ 2 不計算
解法:
開一個陣列,用一個變數存當前的高度,依序判斷即可
程式(Java):
題意:
每組測資有若干高度的牆,Mario站在第一個
計算從第一個到最後的過程,高度增加與減少的次數
例如:
高度 1 ~ 2 增加
高度 3 ~ 2 減少
高度 2 ~ 2 不計算
解法:
開一個陣列,用一個變數存當前的高度,依序判斷即可
程式(Java):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class UVa11764 { | |
public static void main(String args[]) { | |
Scanner sc = new Scanner(System.in); | |
int T = sc.nextInt(); | |
for (int i = 1; i <= T; i++) { | |
int N = sc.nextInt(); | |
int arr[] = new int[N]; | |
int h = 0; | |
int l = 0; | |
for (int j = 0; j < N; j++) | |
arr[j] = sc.nextInt(); | |
int current = arr[0]; | |
for (int j = 1; j < N; j++) { | |
if (current < arr[j]) | |
h++; | |
else if(current > arr[j]) | |
l++; | |
current = arr[j]; | |
} | |
System.out.println("Case " + i + ": " + h + " " + l); | |
} | |
} | |
} |
留言
張貼留言