Combine arrays, strings and searches
문제에서 필요한 것만 저장하십시오
Use a 1-D array for one list. Use a 2-D array for a fixed grid of related values. An index identifies a position; the value stored there may be completely different.
풀이 예제: 첫 번째 가장 긴 단어
알고 있는 단어는 red, blue, gold입니다. 가장 좋은 단어의 인덱스를 유지하여 이름과 길이를 연결해 두십시오. 같은 길이는 첫 번째 단어를 유지해야 하므로 >로 비교하십시오.
DECLARE Words : ARRAY[1:3] OF STRING
DECLARE I, Best : INTEGER
Words[1] ← "red"
Words[2] ← "blue"
Words[3] ← "gold"
Best ← 1
FOR I ← 1 TO 3
IF LENGTH(Words[I]) > LENGTH(Words[Best]) THEN
Best ← I
ENDIF
NEXT I
OUTPUT Words[Best]
흔한 실수
- 최장 길이만 유지. 단어의 길이는 알 수 있지만 어떤 단어가인지 모를 수 있음. 인덱스를 유지;
Words[Best]는 두 가지 모두 제공. >=작성. 동점일 경우 첫 번째가 아닌 마지막 동일한 단어가 승리.- 첫 번째 항목이 아닌 0 또는
""에서 검색 시작. 답은 데이터 내의 항목이어야 함. - 인덱스 변수에 값 저장:
Best ← Words[I]는 INTEGER에 STRING을 넣음.Best ← I를 작성.
패턴 전사
답변에 개수, 값 또는 인덱스 중 무엇이 필요한지 선택하십시오. 하나의 항목을 테스트하고, 일치하지 않는 경우와 반복되는 동일한 일치를 테스트하십시오. 모든 부하 데이터에 대해 최대값 검색을 0으로 시작하는 것이 실패하는 이유를 설명한 후 새로운 입력으로 문제를 해결하십시오.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
Read NumberOfEntries (1 to 5), then NumberOfEntries non-empty names into Names. Count names beginning with uppercase A.
출력을 보려면 '실행'을 클릭하세요.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
Read NumberOfEntries (1 to 5), then NumberOfEntries non-empty words into an array. Output the longest word; on a tie output the first.
출력을 보려면 '실행'을 클릭하세요.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
Array [4, 9, 4, 2] is searched left to right for 4. Found starts at 0 and is updated only while still 0. Give Found after each of the four comparisons.
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.