1.Write a program that sieves out all prime numbers between 2 to 10000 using Eratosthenes’s sieve algorithm. Your program should have an integer array that set all the value to 1 (prime) in the beginning. After running Eratosthenes’s sieve algorithm, set all non-prime number to 0 (non-prime). Finally, print out the number of primes between 2 to 10000, and list all the prime numbers between 500 and 1000.
這題主要是用Eratosthenes’s sieve algorithm(埃拉托色尼的篩子算法)寫一個程式,要2~1萬之間的質數最後並列出500~1000之間的質數
程式輸出是這樣
The number of primes between 2 to 10000: 1229
List all the prime numbers between 500 and 1000:
503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607
613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709
719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823
827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937
941 947 953 967 971 977 983 991 997
請按任意鍵繼續 . . .
2.這題是訂機票問題,假設這架飛機有10個位置
1~5是頭等艙,6~10是經濟艙,乘是一開始會有3個選項
輸入1是訂頭等艙,輸入2是訂經濟艙,輸入3是查詢目前位置狀況
Please type 1 for “first class”
Please type 2 for “economy”
Please type 3 to show seat status
輸出是下面狀況
Please type 1 for "first class"
Please type 2 for "economy"
Please type 3 to show seat status
1
***** boarding pass *****
* seat no:1 *
* section:first class *
*************************
Please type 1 for "first class"
Please type 2 for "economy"
Please type 3 to show seat status
2
***** boarding pass *****
* seat no:6 *
* section:economy *
*************************
Please type 1 for "first class"
Please type 2 for "economy"
Please type 3 to show seat status
3
seat 1 2 3 4 5 6 7 8 9 10
status 1 0 0 0 0 1 0 0 0 0
Please type 1 for "first class"
Please type 2 for "economy"
Please type 3 to show seat status
而當經濟艙或頭等艙位置滿了會輸出first class section is full或是economy section is full
兩個都滿的話會關閉程式,上面兩題都要用陣列寫
拜託高手幫忙
This entry passed through the Full-Text RSS service - if this is your content and you're reading it on someone else's site, please read the FAQ at fivefilters.org/content-only/faq.php#publishers.
留言列表