roberthsu2003 / cAndC-

48 stars 18 forks source link

使用lesson1_6,求出亂數值的最大值和最小值 #29

Closed roberthsu2003 closed 5 months ago

roberthsu2003 commented 7 months ago

輸出

請輸入元素數量:10
第1個元素:268
第2個元素:287
第3個元素:332
第4個元素:460
第5個元素:251
第6個元素:275
第7個元素:221
第8個元素:295
第9個元素:181
第10個元素:308
元素最大值是:460
元素最小值是:181
roberthsu2003 commented 7 months ago
#include <iostream>
#include <time.h>
using namespace std;

int main() {
        srandom(time(NULL));
        int min = 100;
        int max = 500;
        //配置記憶體給陣列變數n
        int nums;
        cout << "請輸入元素數量:";
        cin >> nums;

        int n[nums];
        //輸入
        for (int i = 0; i < nums; i++) {
            n[i] = random() % (max - min + 1) + min;
        }

        //輸出
        int total = 0;
        for (int i = 0; i < nums; i++) {
            cout << "第" << i + 1 << "個元素:";
            cout << n[i] << endl;
            total += n[i];
        }

        cout << "元素值總合為:" << total << endl;
}
lihua1018 commented 7 months ago
#include <iostream>
#include <time.h>
using namespace std;

int main() {
    srandom(time(NULL));
    int min = 100;
    int max = 500;
    //配置記憶體給陣列變數n
    int nums;
    cout << "請輸入元素數量:";
    cin >> nums;

    int n[nums];
    //輸入
    for (int i = 0; i < nums; i++) {
      n[i] = random() % (max - min + 1) + min;
    }

    //輸出
    int total = 0;
    int MAX = n[0];
    int MIN = n[0];

    for (int i = 0; i < nums; i++) {
      cout << "第" << i + 1 << "個元素:";
      cout << n[i] << endl;
      total += n[i]; 
      if (n[i] > MAX) {
        MAX = n[i];
      } else if (n[i] < MIN){
        MIN = n[i];
      }

    }

    cout << "元素值總合為:" << total << endl;
    cout << "最大元素值:" << MAX << endl;
    cout << "最小元素值:" << MIN << endl;
}

https://replit.com/join/kpmshttttr-chung03216

a86685566 commented 7 months ago
#include <iostream>
#include <time.h>
using namespace std;

int main() {
  srandom(time(NULL));
  int max = 500;
  int min = 100;

  int nums;
  cout << "請輸入元素量:";
  cin >> nums;
  int n[nums];

  // 亂數設定數值
  int maxN = min; //紀錄最大的元素值
  int minN = max; //紀錄最小的元素值
  for(int i=0; i<nums; i++){
    n[i] = random() % (max-min+1)+min;
    if(n[i] > maxN){
      maxN = n[i];
    }
    if(n[i] < minN){
      minN = n[i];
    }
  }

  int total = 0; //紀錄總數用
  for(int i=0; i<nums; i++){
    cout << "第" << i+1 << "個元素值為:" << n[i] << endl;
    total += n[i];
  }
  cout << "出現最小的元素值為:【" << minN << "】" << endl;
  cout << "出現最大的元素值為:【" << maxN << "】" << endl;
  cout << "元素值總合為:【" << total << "】" << endl;
}

其中,不論


    if(n[i] > maxN){
      maxN = n[i];
    }
    if(n[i] < minN){
      minN = n[i];
    }

出現在上面或是下面的 for(int i=0; i<nums; i++){ 之中都可以正常運行 其實兩個 for 是可以合併的

cft67ujm commented 7 months ago

include

include

using namespace std;

int main() { srandom(time(NULL));

int min = 100; int max = 500;

cout << "請輸入元素數量:"; int nums; cin >> nums;

int n[nums + 2]; n[nums + 1] = min; n[nums + 2] = max; for (int i = 0; i < nums; i++) { n[i] = rand() % (max - min + 1) + min; } int total = 0; for (int i = 0; i < nums; i++) { cout << "第" << i + 1 << "個元素:"; cout << n[i] << endl; total += n[i]; if (n[i] > n[nums + 1]) { n[nums + 1] = n[i]; } if (n[i] < n[nums + 2]) { n[nums + 2] = n[i]; } } cout << "元素總和為:" << total << endl; cout << "最大值為:" << n[nums + 1] << endl; cout << "最小值為:" << n[nums + 2] << endl; }

jackak1003 commented 7 months ago
#include <iostream>
#include <time.h>  //建立範圍亂數
using namespace std;
int main() {
  srandom(time(NULL));
  int min=100;
  int max=999;
  int nums;
  cout << "請輸入元素數量:";
  cin >> nums;
  int n[nums];

  for(int i=0;i<nums;i++){
    n[i]=random()%(max-min+1)+min;
  }

  for(int i=0;i<nums;i++){
    cout<<"第"<<i+1<<"個元素是:"<<n[i]<<endl;  
  }
  int minss=n[0];
  for(int i=0;i<nums;i++){
    if(n[i]<minss)
      minss=n[i];
  }
  cout<<"最小值:"<<minss<<endl;
  int maxx=n[0];
  for(int j=0;j<nums;j++){
    if(n[j]>maxx)
      maxx=n[j];
  }
  cout<<"最大值:"<<maxx<<endl;

}
estelaOAO commented 7 months ago

include

include

using namespace std;

int main() {
srandom(time(NULL)); int min =1; int max =99; int nums; cout << "請輸入元素數量:"; cin >> nums; int n[nums]; //輸入 for (int i = 0; i < nums; i++) { n[i] = random() % (max - min + 1) + min; }

//輸出

int max_value = n[0]; int min_value = n[0]; for (int i = 0; i < nums; i++) { cout << "第" << i + 1 << "個元素:"; cout << n[i] << endl; if (n[i] > max_value) { max_value = n[i]; } if (n[i] < min_value) { min_value = n[i]; } }

cout << "最大值為:" << max_value << endl;
cout << "最小值為:" << min_value << endl;

}