// (穷举大法好) #include<iostream> usingnamespace std; intmain(){ int a, b, c; cout << "请输入A、B、C三只小猪的体重: "; cin >> a >> b >> c; // 如果a=b=c,那么返回全部相等 if (a == b && b == c) { cout << "三只小猪体重完全相等" << endl; } // 如果不,就看是不是a>b=c elseif (a > b && b == c) { cout << "A最重" << endl; } // 不是就看a<b=c,是就返回b、c并列最重 elseif (a < b && b == c) { cout << "B和C并列最重" << endl; } // 不是就看a=b>c,ab最重 elseif (a == b && b > c) { cout << "A和B并列最重" << endl; } // a<b>c,b最重 elseif (a < b && b > c) { cout << "B最重" << endl; } // a>b>c,a最重 elseif (a > b && b > c) { cout << "A最重" << endl; } // a<b<c,c最重 elseif (a < b && b < c) { cout << "C最重" << endl; } // a=b<c,c最重 elseif (a == b && b < c) { cout << "C最重" << endl; } // a>b<c,此时判断a是否大于c elseif (a > b && b < c) { if (a > c) { cout << "A最重" << endl; } elseif (a == c) { cout << "A和C并列最重" << endl; } else { cout << "C最重" << endl; } } system("pause"); return0;
// —————————————————————————————————————————— // —————————————————————————————————————————— //运用三目运算符,不考虑相同重量: // int main() { // cout << "分别输入三只小猪的体重:\n"; // cin >> A >> B >> C; // result = (A > B) ? ((A > C) ? 'A' : 'C') : ((B < C) ? 'C' : 'B'); // cout << result << "小猪最重!" << endl; // system("pause"); // return 0; // } }
#include<iostream> usingnamespace std; //黑马答案 intmain(){ int arr[] = {2,4,6,5,9}; int start = 0; int temp = 0; int end = sizeof(arr) / sizeof(arr[0]) - 1;
while (start < end) //当start < end时才需要逆置 { temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; //每逆置一组元素,起始下标往前进一位 end--; //结束下标往后退一位 } for (int i = 0; i < 5; i++) { cout << arr[i] << endl; } }
// 我的 // int main() {
// int arr[] = {6,1,9,7,2}; // int start = 0; //起始下标 // int temp = 0; // int end = sizeof(arr) / sizeof(arr[0]) - 1; //结束下标 // int y = sizeof(arr) / sizeof(arr[0]) / 2;