1.4 数组笔记
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n, k;
int a[100];
cin >> n >> k;
for (int i = 0; i < n; i ++ ) cin >> a[i];
reverse(a, a + k); //要用到<algorithm>库函数
reverse(a + k, a + n);
reverse(a, a + n); //翻转函数,以a[0]和a[n]为首尾
for (int i = 0; i < n; i ++ )
for (int j = i + 1; j < n; j ++ )
if (a[i] > a[j])
swap(a[i], a[j]); //要用到<algorithm>库函数,交换两个数组元素
for (int i = 0; i < n; i ++ ) cout << a[i] << ' ';
cout << endl;
return 0;
}
练习题5:计算 2 的N 次方。N≤10000
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a[10000], size = 1, n;
a[0] = 1;
cin >> n;
while (n -- )
{
int t = 0;
for (int i = 0; i < size; i ++ ) //高精度算法
{
t += a[i] * 2;
a[i] = t % 10;
t /= 10;
}
if (t) a[size ++ ] = t;
}
for (int i = size - 1; i >= 0; i -- ) cout << a[i];
cout << endl;
return 0;
}