JAVAプログラミングで学ぶロッカーパズル

このQ&Aのポイント
  • 授業で学ぶJAVAプログラミングの中で、ロッカーパズルというゲームがあります。このゲームでは、100個のロッカーと100人の生徒が関与します。最初はすべてのロッカーが閉まっていますが、生徒が入ってくるたびに、特定の規則に基づいてロッカーの状態が変化します。
  • 具体的には、最初の生徒はすべてのロッカーを開けます。2人目の生徒は2番目のロッカーから始めて、2つおきにロッカーの状態を変えます。3人目の生徒は3番目のロッカーから始めて、3つおきに状態を変えるという具体的な手順です。これを100人の生徒全員が繰り返します。
  • 最終的に、どのロッカーが開いているかを判断するために、boolean型の配列を使用するJavaプログラムを作成します。ロッカーの状態は、開いている場合はtrue、閉じている場合はfalseで表されます。全ての生徒が通過した後、配列の要素の状態に基づいて開いているロッカーを特定します。
回答を見る
  • ベストアンサー

JAVA プログラミング ロッカーパズル

JAVA プログラミング ロッカーパズル 授業で英語のJAVAプログラミングをやってます。アメリカではそこそこ有名なゲームらしいのですがまったく意味がわかりません。もし、英語のわかる方、プログラミングのできる方いましたら、回答お願いします。JAVAの配列のチャプターなので配列を使ってお願いします。以下問題です。 A school has 100 lockers and 100 students. All lockers are closed on the first day of shcool. As the students enter, the first student, denoted S1, opens every locker. Then the second student, S2, begins with the second locker, denoted L2, and closes every other locker. Student S3 begins with the third locker and changes every third locker (closes it if it was open, and opens it if it is closed). Student S4 begins with locker L4 and changes every fourth locker. Student S5 starts with L5 and changes every fifth locker, and so on, until student S100 changes L100. After all the students have passed through the building and changed the lockers, which lockers are open? Write a java program to find your answer. Hint: Use an array of 100 boolean elements, each of which indicates whether a locker is open (true) or closed (false). Initially, all lockers are closed.

  • Java
  • 回答数1
  • ありがとう数4

質問者が選んだベストアンサー

  • ベストアンサー
回答No.1

http://ideone.com/0K1UW まぁ算数的によく考えたらそりゃ10になるわなぁ。 #どうせなら問題文がL1からじゃなくてL0にしてくれたら良かったのに(汗)

KaYoukAyOu
質問者

お礼

お礼遅れてすいません。 ゆっくり考えたら確かに10ですね~ 一瞬全然わからなくて焦ってたので とても助かりました。 ありがとうございます!

関連するQ&A

  • 英語の文法書の違いを教えてください。

    英語の文法書の購入したいのですが、下記2点の違いを教えてください。お手数ですがよろしくお願い致します Basic Grammar in Use Student's Book with Answers: Self-study reference and practice for students of North American English Basic Grammar in Use with Answers : Self-Study Reference andPractice for Students of English (Grammar in Use Series)

  • 訳してください!!

    The world we live in is changing fast, and our language is changing along with it. Every living language grows and changes. It changes as the people who use it create new words and find new meanings for old ones. の訳を教えてください!!自分で訳すと 私たちが住んでいる世界は速く変化している、そして、私たちの言語はそれに伴って変化している。あらゆる生きた言語は成長して、変わります。それを使用する人々が新しい単語をつくって、古い単語の新しい意味を見つけるとき、それは変化します。

  • 和訳と解説をお願いします。

    ”Thefacebook was still small. That's because you can only get it in certain schools,” he explained. ”But when Thefacebook opens at a new school, almost all the students join, and almost all of them visit the site every day.” That's because you can only get it in certain schools, のyouは誰、または何を指しているのかわかりません。文脈からyouに置き換わるものの見当がつきません。この一文の主語はThefacebookではないのですか?それならitになるとおもうのですが..... 解答よろしくお願いします。

  • 和訳をお願いします。

    Every student has gone through the process of learning something intellectually and then relearning it through experience. Science is recommended as the mode of knowing that will most benefit one's ability to establish facts, to understand the reality surrounding them, and to approach actuality with sensitivity. です。専門書を自分で逐語訳しており、どうしてもわからない場所です。お願いします。

  • 英語発音

    I sing and I shout and I swear it to the person who gave me the voice and mind. I change a wish of somebody into a song. delivers it to the end of the earth. Become a forerunner who opens the age. Carry on! believe in one's own edge. 英語得意な紳士様、発音を教えてください・・・orz

  • American Accent Trainingにおける輪ゴムを使った

    American Accent Trainingにおける輪ゴムを使った練習法について 英語の強化の一環で、American Accent Trainingという書籍を購入しました。この本の中で輪ゴムを使った練習法が出てきます。アマゾンにおけるレビューなども見たのですが、この輪ゴムを使った練習法の内容がいまいち分かりません。テキスト原文では Take a rubber band and hold it with your two thumbs. Every time you want to stress a word by changing pitch, pull on the rubber band. Stretch it out gently, don't jerk it sharply. Make a looping figure with it and do the same with your voice. Use the rubber band and strech it out every time you change pitch. every time you want to stressと every time you change pitchに輪ゴムを引っ張るとのことですが、結局たとえば下の例文の際にどこで引っ張る形でしょうか。 テキストのインストラクションを見る限りは do the same ともあるので前半と後半とでは別々のインストラクションとも読めるかと思いますが、その場合、前半と後半とでは引っ張る場所が変わるという解釈でしょうか。 感覚的にPitch変わる度に引っ張るというのは相当「忙しい」感じもしまして、このインストラクションの内容がいまいち把握できておりません。 よろしくお願いします。 例文: Dogs eat bones. ここで、Dogsとbonesが高めのPitchで強調。

  • 2つのテーブルからデータ取得

    いつもお世話になっております。 以下のようなテーブルがあります。 ---------- CREATE TABLE IF NOT EXISTS `students` ( `id` int(11) NOT NULL AUTO_INCREMENT, `student_number` bigint(20) DEFAULT NULL, `name` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL, PRIMARY KEY (`id`) ) ---------- CREATE TABLE IF NOT EXISTS `student_upload` ( `student_number` bigint(20) NOT NULL, `upload_id` int(11) NOT NULL ) ---------- データは以下のように登録されています。 students ---------- id|student_number|name 1 |11111     |いちろう 2 |22222     |じろう 3 |33333     |さぶろう student_upload ---------- student_number|upload_id 11111     |1 11111     |2 22222     |1 22222     |2 ---------- student_upload.upload_id=1 のデータを取得したいので 下記のようなSQL文を発行しているのですが、student_upload.upload_id=2 の データも取得されてしまいます。 select * from students s, student_upload up where up.upload_id=1 and up.student_number=s.student_number よろしくご教示お願いいたします。 <環境>  PHP:5.3.5  MySQL:5.0.7

    • ベストアンサー
    • MySQL
  • アメリカ小学4年生算数の問題です。

    日本の小学4年生にも分かるような問題に訳してください。 Mr. Yamato’s class has 18 students. If a class counts around by a number, and it ends with 90, what number did they count by?

  • javaのプログラミング(英語)

    現在javaのクラスを取っています(英語)。 宿題が出たのですが2時間くらいかけて作ったプログラムがいまだに完成しませんw わかる方いましたら部分的でもいいので解答をお願いしたいです。 特に2番目の26ストリングのところ。ここは数だけ宣言するのかa~zまでを先に宣言してしまうのかいまいちわかりません。4番目で入力した数に対応するアルファベット・・・と書いてあるのですが いったいどこでアルファベットを宣言するのやら。 どうかよろしくお願いします。 引き続きがんばります。 以下問題です。 Write a Java application that: ・declares a Scanner variable ・declares an array of 26 String values, and initializes each array element with the corresponding lowercase letter of the alphabet ・prompts the user to enter an integer, and uses the Scanner's nextInt method to obtain that integer from the user ・prints out the corresponding letter from the array, so if the input is zero, should print "a", and should print "z" for an input of 25. ・creates a new array of size 27, puts a string containing a space (" ") at index 0 in the new array, and copies all the other letters to positions 1..26 of the new array. The simplest way to do this is to use System.arraycopy() ・loops through the array, printing each entry as follows: if the entry is a vowel ("a", "e", "i", "o", "u"), it must be printed in uppercase otherwise, the entry must be printed as is ・computes and prints the number of non-vowel strings at the end of the loop. Requirements The program must catch the InputMismatchException from the call to nextInt, in which case the program must display an appropriate error message and end the program. The program should catch any other problems with the input, for example, a negative input number or a number larger that 26. The loop should work for any size initial array, and should not depend on the array size -- you may use array.length to find out the size of the array. You should memorize the sequence of parameters to System.arraycopy() and their meaning. This will be useful later in the semester (and may appear on quizzes or exams).

    • ベストアンサー
    • Java
  • 英文の和訳について

    こんにちは、下記の画像の英文の和訳について質問をさせてください…!m(_ _)m最後のほうのTake every parent’s desire~ですが、for their kids…の「…」って一体何なんでしょうか?!!(◎_◎;)Take every parent’s ~とfuel it with~とbig profits~がたやすいこと(child’s play)だと思っていたんですが、この「…」は後のkeep up with the Joneses,のコンマと同じで並列の意味なのでしょうか? どなたかご回答いただけると嬉しいです、よろしくお願いいたします!

専門家に質問してみよう