1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 //方法一 翻轉字符串 6 void method1() 7 { 8 string s = "i love acm"; 9 reverse(s.begin(), s.end()); 10 cout << s << endl; 11 } 12 13 //方法二 翻轉整形數組 14 void method2() 15 { 16 int n, a[100], l, r; //需要翻轉的左邊界 右邊界 17 18 cin >> n; 19 for(int i = 1; i <= n; ++i) cin >> a[i]; 20 cin >> l >> r; 21 22 reverse(a + l, a + r + 1); //這里注意 結束的指針是右邊界下標加 1 23 24 for(int i = 1; i <= n; ++i) cout << a[i] << " "; 25 cout<<endl; 26 } 27 28 signed main() 29 { 30 method1(); 31 method2(); 32 return 0; 33 }