遇見有趣的問題.但還是偏好於 程式可以解決的就交給程式吧.可以問大神的還是問大神吧. 所以簡單記錄一下這個倍數開燈關燈後.剩下有多少燈還開著的問題. 程式寫得有點趕.希望沒太大問題.
補充,一個可以查整數線性關係的網站, 直接輸入起碼的相關數字.搜尋就可以了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <?php $light = array (); for ($i=0 ;$i<100 ;$i++) { $light[$i] = array ("number" => $i + 1 , "status" => "off" ); } for ($i=0 ; $i<100 ; $i++) { for ($j=1 ; $j<=100 ; $j++) { if ($light[$i]['number' ] % $j == 0 ) { $light[$i]['status' ] = ($light[$i]['status' ] == "off" ? "on" : "off" ); } } } for ($i=0 ;$i<100 ;$i++) { if ($light[$i]['status' ] == "on" ) { echo $light[$i]['number' ]." " ; } } ?>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #!/usr/bin/node light = [] for (var i=0 ; i<100 ; i++) { light[i] = { number: i + 1 , status: "off" }; } for (var i=0 ; i<100 ; i++) { for (var j=1 ; j<=100 ; j++) { if (light[i].number % j == 0 ) { light[i].status = light[i].status == "off" ? "on" : "off" ; } } } var temp = "" ;light.forEach(function (a ) { if (a.status == "on" ) { temp += (a.number + " " ); } }); console .log(temp);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 import sysdef result () : light = {}; for i in range(0 , 100 ): light[i] = { "number" : i + 1 , "status" : "off" } for i in range(0 , 100 ): for j in xrange(1 , 101 ): if int(light[i]['number' ]) % j == 0 : if light[i]['status' ] is "off" : light[i]['status' ] = "on" else : light[i]['status' ] = "off" for i in range(0 , 100 ): if (light[i]['status' ] is "on" ): sys.stdout.write("%s " % light[i]['number' ]); if __name__ == "__main__" : result()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 use Data::Dumper;our @light = ();for (my $i=0 ; $i<100 ; $i++) { push @light, { number => $i + 1 , status => "off" }; } for (my $i=0 ; $i<100 ; $i++) { for (my $j=1 ; $j<=100 ; $j++) { if ($light[$i]->{'number' } % $j == 0 ) { $light[$i]->{'status' } = $light[$i]->{'status' } eq "on" ? "off" : "on" ; } } } for (0 ..99 ) { if ($light[$_]->{'status' } eq "on" ) { print $light[$_]->{'number' }." " ; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 import java.util.ArrayList;import java.util.HashMap;public class Light { public static void main (String args[]) { ArrayList<hashmap> light = new ArrayList</hashmap><hashmap>(); HashMap<string , String> lightDetail; for (int i=0 ; i<100 ; i++) { lightDetail = new HashMap</string><string , String>(); lightDetail.put("number" , String.valueOf(i+1 )); lightDetail.put("status" , "off" ); light.add(lightDetail); } for (int i=0 ; i<100 ; i++) { for (int j=1 ; j<=100 ; j++) { lightDetail = light.get(i); if (Integer.parseInt(lightDetail.get("number" ).toString()) % j == 0 ) { lightDetail.put("status" , lightDetail.get("status" ).equals("on" ) ? "off" : "on" ); } } } for (int i=0 ; i<100 ; i++) { lightDetail = light.get(i); if (lightDetail.get("status" ).equals("on" )) { System.out.print(lightDetail.get("number" ) + " " ); } } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 light = [] 100 .times do |i| light[i] = Hash[ "number" => i+1 , "status" => "off" ] end (0 ...100 ).each do |i| (1 ..100 ).each do |j| if light[i]["number" ] % j == 0 light[i]["status" ] = (light[i]["status" ] == "off" ? "on" : "off" ) end end end (0 ...100 ).each do |i| if light[i]["status" ] == "on" print light[i]['number' ].to_s + " " end end
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 package main import "fmt" type LightDetail struct { number int status string } func main () { var light [100 ]LightDetail; for i := 0 ; i<100 ; i++ { light[i] = LightDetail{i + 1 , "off" } } for i := 0 ; i<100 ; i++ { for j := 1 ; j<=100 ; j++ { if light[i].number % j == 0 { if light[i].status == "off" { light[i].status = "on" }else { light[i].status = "off" } } } } for i := 0 ; i<100 ; i++ { if light[i].status == "on" { fmt.Printf("%v " , light[i].number) } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 BEGIN { for (i=0 ; i<=100 ; i++) light[i] = sprintf("%s off" , i+1 ) for (i=0 ; i<100 ; i++) { for (j=1 ; j<=100 ; j++) { split(light[i], detail, " " ) if (detail[1 ] % j == 0 ) light[i] = sprintf("%s %s" , i + 1 , detail[2 ] == "on" ? "off" : "on" ) } } for (i=0 ; i<=100 ; i++) { split(light[i], detail, " " ) if (detail[2 ] == "on" ) printf detail[1 ] " " } print "" }
1 2 3 4 5 6 7 8 9 10 11 12 light = [] for i in [0 ...100 ] light[i] = number: i + 1 , status: "off" for i in [0 ...100 ] for j in [1 ..100 ] light[i].status = (if light[i].status is "on" then "off" else "on" ) if light[i].number % j is 0 light.forEach (l) -> console.log l.number if l.status is "on"
1 2 3 4 5 6 7 8 light = [{number: i + 1 ,status: "off" } for i from 0 til 100 ] for i from 0 til 100 for j from 1 to 100 light[i].status = (if light[i].status is "on" then "off" else "on" ) if light[i].number % j is 0 light.forEach (l) ~> console.log l.number if l.status is "on"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class LightDetail { constructor (public number_, public status) {} } var light = [];for (var i=0 ; i<100 ; i++) light[i] = new LightDetail(i + 1 , "off" ) for (var i=0 ; i<100 ; i++) for (var j=1 ; j<=100 ; j++) if (light[i].number_ % j === 0 ) light[i].status = light[i].status === "off" ? "on" : "off" for (var i=0 ; i<100 ; i++) if (light[i].status === "on" ) console .log(light[i].number_)