四、利用 Python、C、C++、Java、C#五種程式語言的任一種程式語言,撰寫一程式用以模擬大樂透的電腦選號(6 個介於 1 到 49 之間不同的號碼)。 輸出時要由小至大排列。(26 分)

詳解 (共 8 筆)

錯在考古 贏在高普
錯在考古 贏在高普
詳解 #5865121
2023/07/01
//可直接執行//以下使用C++撰寫:#...
(共 1221 字,隱藏中)
前往觀看
龍貝爾
龍貝爾
詳解 #5875498
2023/07/09
#include <stdio.h...
(共 818 字,隱藏中)
前往觀看
Phil(112高普雙榜)
Phil(112高普雙榜)
詳解 #5789779
2023/04/26

其C++程式碼如下-

     #include<iostream>

     #include<ctime>

     using namespace std;

          int main()

         {

            srand(time(NULL));

            int arr[6]={};

            int temp;

            for(int i=0;i<6;i++){

                 int random=(rand()%49)+1;

                 arr[i]=random;

            }

            for(int k=6;k>0;k--){  //氣泡排序法

                 for(int m=0;m<k;m++){

                       if(arr[m]>arr[m+1]){

                           temp=arr[m];

                           arr[m]=arr[m+1];

                           arr[m+1]=temp;

                        }

                  }

              }

           for(int j=0;j<6;j++){

                cout<<"第"<<j+1<<"個樂透號碼為:"<<arr[j]<<endl;

           }

           return 0;

}

努力中(112高考上岸)
努力中(112高考上岸)
詳解 #5864849
2023/07/01

#Python

import random

def generate_lottery_numbers( ):
      numbers = random.sample(range(1, 50), 6) #隨機從1到49中選6個不同的數字
      numbers.sort() # 排序數字,由小到大
      return numbers

lottery_numbers = generate_lottery_numbers( )
print("大樂透電腦選號:", lottery_numbers)

使用random.sample()函數來隨機從1到49中選取6個不同的數字,並將選取的數字儲存在一個列表中
然後使用sort()方法對列表中的數字進行排序,以確保輸出結果是由小到大排列的
最後將選號結果印出,每次執行程式都會生成一組新的隨機選號

hchungw
hchungw
詳解 #6051866
2024/03/23
python

import random
# Function to simulate lottery drawing
def draw_lottery():
    numbers = random.sample(range(1, 50), 6)  # Get 6 unique random numbers from 1 to 49
    numbers.sort()  # Sort the numbers in ascending order
    return numbers
# Draw the lottery numbers
lottery_numbers = draw_lottery()
lottery_numbers
Result
[3, 6, 13, 21, 29, 33]
114年高考上榜
114年高考上榜
詳解 #5792296
2023/04/28

Python 語法
a = []

for i in range(6):
    x = random.sample(range(1, 50), 6)
    a.append(x)
a.sort()
print(a)
xy
xy
詳解 #6120054
2024/06/04
#include <stdio.h...
(共 757 字,隱藏中)
前往觀看
皮卡112年高普考雙榜感謝阿摩
皮卡112年高普考雙榜感謝阿摩
詳解 #5792403
2023/04/28

Java版本:
public class Main{
  public static void main (String[]args){
    int[] lottery = new int[6];
    for (int i = 0; i < 6; i++){
    lottery[i] = (int) (Math.random () * 49) + 1;
 }
    for (int i = 0; i < 6; i++) { //排序
        for (int j = i + 1; j < 6; j++){
        if (lottery[j] < lottery[i]) {
        int temp = lottery[i];
        lottery[i] = lottery[j];
        lottery[j] = temp;
       }
   }
 }
    for (int i = 0; i < 6; i++) {
    System.out.print (lottery[i] + " ");
   }
 }
}

私人筆記 (共 1 筆)

無法顯示
無法顯示
私人筆記 #5290208
2023/07/06


(共 0 字,隱藏中)
前往觀看