การนับช่วงเวลาเดินมากกว่า 10,000 ก้าวติดต่อกัน
โจทย์
Write a program to determine the longest streak of consecutive days where the user walked more than 10,000 steps.
Input:
- The first line contains an integer n, representing the number of days tracked.
- The second line contains n integers, where each integer indicates the step count for a specific day.
Output:
- A single integer represents the longest streak of consecutive days with more than 10,000 steps.
| Input | Output |
|---|---|
| 7 8000 12000 15000 9500 13000 18000 7000 | 2 days |
| 6 8000 10600 8500 9000 9500 15000 | 1 day |
| 4 8000 8500 9000 9500 | 0 day |
โค้ด
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int days = scanner.nextInt();
int[] steps = new int[days];
for (int i = 0; i < steps.length; i++) {
steps[i] = scanner.nextInt();
}
int currentSteaks = 0;
int longestSteaks = 0;
for (int step : steps) {
if (step >= 10000) {
currentSteaks++;
longestSteaks = Math.max(longestSteaks, currentSteaks);
} else {
currentSteaks = 0;
}
}
System.out.printf("%d day%s", longestSteaks, longestSteaks > 1 ? "s" : "");
}
}คำอธิบาย
1. การประกาศคลาสและรับจำนวนวัน
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int days = scanner.nextInt(); // จำนวนวันที่บันทึก2. การสร้างและรับข้อมูลจำนวนก้าว
int[] steps = new int[days];
for (int i = 0; i < steps.length; i++) {
steps[i] = scanner.nextInt();
}- สร้างอาเรย์เก็บจำนวนก้าวตามจำนวนวัน
- วนลูปรับจำนวนก้าวแต่ละวัน
3. การนับช่วงเวลาเดินติดต่อกัน
int currentSteaks = 0; // จำนวนวันติดต่อกันปัจจุบัน
int longestSteaks = 0; // จำนวนวันติดต่อกันที่มากที่สุด
for (int step : steps) {
if (step >= 10000) { // ถ้าเดินมากกว่าหรือเท่ากับ 10,000 ก้าว
currentSteaks++; // เพิ่มจำนวนวันติดต่อกัน
longestSteaks = Math.max(longestSteaks, currentSteaks); // อัพเดตค่าสูงสุด
} else {
currentSteaks = 0; // รีเซ็ตเป็น 0 เมื่อเดินน้อยกว่า 10,000 ก้าว
}
}- ใช้ตัวแปรสองตัวในการติดตาม:
currentSteaks: นับจำนวนวันติดต่อกันปัจจุบันlongestSteaks: เก็บจำนวนวันติดต่อกันที่มากที่สุด
- เมื่อพบวันที่เดินมากกว่า 10,000 ก้าว:
- เพิ่ม currentSteaks
- อัพเดต longestSteaks ถ้า currentSteaks มากกว่า
- เมื่อพบวันที่เดินน้อยกว่า 10,000 ก้าว:
- รีเซ็ต currentSteaks เป็น 0
4. การแสดงผล
System.out.printf("%d day%s", longestSteaks, longestSteaks > 1 ? "s" : "");- แสดงจำนวนวันที่มากที่สุด
- ใช้ Ternary operator เพื่อเพิ่ม ‘s’ เมื่อมีมากกว่า 1 วัน
แผนภาพการทำงาน
ตัวอย่างการทำงาน
ตัวอย่างที่ 1
Input:
จำนวนวัน: 5
จำนวนก้าว: 12000 11000 10500 9800 12500
การทำงาน:
วันที่ 1: 12000 ✓ (currentSteaks = 1, longestSteaks = 1)
วันที่ 2: 11000 ✓ (currentSteaks = 2, longestSteaks = 2)
วันที่ 3: 10500 ✓ (currentSteaks = 3, longestSteaks = 3)
วันที่ 4: 9800 ✗ (currentSteaks = 0, longestSteaks = 3)
วันที่ 5: 12500 ✓ (currentSteaks = 1, longestSteaks = 3)
Output: 3 daysตัวอย่างที่ 2
Input:
จำนวนวัน: 4
จำนวนก้าว: 9000 9500 9800 9600
การทำงาน:
วันที่ 1: 9000 ✗ (currentSteaks = 0, longestSteaks = 0)
วันที่ 2: 9500 ✗ (currentSteaks = 0, longestSteaks = 0)
วันที่ 3: 9800 ✗ (currentSteaks = 0, longestSteaks = 0)
วันที่ 4: 9600 ✗ (currentSteaks = 0, longestSteaks = 0)
Output: 0 dayปรับปรุงล่าสุด