Closed roberthsu2003 closed 5 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;
}
#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;
}
#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 是可以合併的
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; }
#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;
}
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;
}
輸出