C++练习题1:"输入三个数,输出最大值"
// stdycpp01.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//"输入三个数,输出最大值"
//两种方法:1.输入值,然后进行比较,然后输出
// 2.装入容器数组,利用api直接输出
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 5 using namespace std; 6 7 int main() 8 { 9 int a, b, c, m; 10 cout << "输入三个数,输出最大值" << endl; 11 cin >> a >> b >> c; 12 int a1[] = { a, b, c }; 13 int max_a1 = *max_element(a1, a1 + 3); 14 int max_a1_xb= max_element(a1, a1 + 3)-a1; 15 int* max_a1_xb_pt = &max_a1_xb; 16 vector<int>num = { a,b,c }; 17 int max_num = *max_element(num.begin(), num.end()); 18 int max_num_xb= max_element(num.begin(), num.end())-num.begin(); 19 cout << "最大值是:" << endl; 20 m = a; 21 if (b > m) 22 m = b; 23 if (c > m) 24 m = c; 25 cout << m << endl; 26 cout << "容器获取最大值:" << max_num << endl; 27 cout << "容器获取最大值下标:" << max_num_xb << endl; 28 cout << "数组获取最大值:" << max_a1 << endl; 29 cout << "数组获取最大值下标:" << max_num_xb << endl; 30 cout << "数组获取最大值下标指针:" << *max_a1_xb_pt << endl; 31 // std::cout << "Hello World!\n"; 32 }

浙公网安备 33010602011771号