Read a scenario and plan an algorithm
시나리오에서 계약으로
예약 시스템은 요청된 그룹 크기를 읽습니다. 전체 그룹을 수락하거나 거절해야 하며, 거절할 때는 사용 가능한 좌석 수가 변경되지 않아야 합니다.
| 식별자 | 유형 | 목적 |
|---|---|---|
| Remaining (남은 좌석) | INTEGER | 사용 가능한 좌석 |
| Size (크기) | INTEGER | 요청한 좌석 |
| Accepted (수락 여부) | BOOLEAN | 요청이 수용 가능한지 여부 |
입력, 결정, 상태 변화, 출력을 분리하십시오. 포괄적 경계를 명시하십시오: 남은 좌석을 모두 사용하는 그룹도 수용 가능합니다.
풀이 예제: 요청 하나
알고 있음: 남은 좌석은 7개이고 요청도 7개입니다. 현재 수용 가능 용량을 고려하여 요청을 남은 좌석과 비교하십시오. 승인은 수용 가능 용량에 따라 결정됩니다.
DECLARE Remaining, Size : INTEGER
Remaining ← 7
Size ← 7
IF Size <= Remaining THEN
Remaining ← Remaining - Size
OUTPUT "accepted"
ELSE
OUTPUT "rejected"
ENDIF
OUTPUT Remaining
출력이 승인되면 0가 됩니다. 크기를 8로 변경하면 Remaining이 7가 되어야 합니다. 두 번째 테스트는 거절된 분기를 확인합니다.
흔한 실수
- 테스트 전에 감산:
Remaining ← Remaining - Size, thenIF Remaining >= 0. 거절된 그룹은 이미 좌석을 차지했습니다. 먼저 테스트한 후, 수락된 분지에서만Remaining을 변경하십시오. Size < Remaining을 작성합니다. 정확히 남은 좌석 수가 필요한 그룹이 잘못 거절됩니다.ENDIF이후에Remaining을 업데이트합니다. 그러면 거절된 분지도 상태를 변경합니다.- 수락된 요청 하나만 테스트합니다. 거절된 분지에 대해 언급하지 않으므로, 하나 더 많은 요청을 테스트하십시오.
유도적, 이후 독립적
먼저 예약 알고리즘을 완성하십시오. 그 다음 빈 에디터에서 리프트 문제를 해결하십시오. 추론 상자에 왜 초과 요청이 총합을 변경해서는 안 되는지 설명하고, 한계치에 정확히 도달하는 요청을 추적하십시오.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
For booking seats, each request has a name, integer quantity and accepted/rejected status. Choose the types for those THREE fields in order from STRING, INTEGER, BOOLEAN, REAL.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
Finish the starter: read NumberOfGroups positive group sizes. Capacity is 15. Accept a group if it fits, output accepted/rejected, then remaining seats.
출력을 보려면 '실행'을 클릭하세요.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
A lift has a maximum total weight of 300. Read NumberOfPeople (1 to 5) then NumberOfPeople positive integer weights. Output the number admitted and final weight. Skip a person who would exceed 300; later people may still fit.
출력을 보려면 '실행'을 클릭하세요.
Explain your choice of data structure and loop. Identify one boundary case and predict its result. For a subroutine, explain what is returned or changed in the caller.
이 브라우저의 이번 세션에 저장되었습니다. 교사나 본인이 직접 검토하십시오; 이 설명은 자동으로 채점되지 않습니다.
해당 설명을 다음 기준에 따라 검토하십시오.
- My types and data structure match the data and the question.
- I justified the loop and checked its first and last iterations.
- Initial values and rejected operations preserve the intended state.
- My test includes input, purpose and expected result.
- I distinguished returning a value from printing and changing caller state.