算法代码如下:
#include <iostream>
typedef int DataType;
void Reverse(DataType a[], int n, DataType b[] ){
for(int i = 0; i < n; i++) {
b[i] = a[n - 1 - i];
}
}
int main() {
const int n = 5;
DataType a[n] = {1, 2, 3, 4, 5};
DataType b[n];
Reverse(a, n, b);
for(int i = 0; i < n; i++) {
std::cout << b[i] << " ";
}
return 0;
}
算法代码如下:
#include <iostream>
typedef int DataType;
void Reverse(DataType a[], int n) {
int m = n / 2;
DataType temp;
for(int i = 0; i < m; i++) {
temp = a[i];
a[i] = a[n - 1 - i];
a[n - 1 - i] = temp;
}
}
int main() {
const int n = 5;
DataType a[n] = {1, 2, 3, 4, 5};
Reverse(a, n);
for(int i = 0; i < n; i++) {
std::cout << a[i] << " ";
}
return 0;
}
算法代码如下:
#include <iostream>
#include <iomanip>
using namespace std;
const int MAX_SIZE = 100;
void MatrixMultiply(int a[][MAX_SIZE], int b[][MAX_SIZE], int c[][MAX_SIZE], int n) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
c[i][j] = 0;
for(int k = 0; k < n; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
}
int main() {
const int n = 3;
int a[n][MAX_SIZE] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int b[n][MAX_SIZE] = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
int c[n][MAX_SIZE];
MatrixMultiply(a, b, c, n);
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cout << setw(5) << c[i][j];
}
cout << endl;
}
return 0;
}
算法代码如下:
#include <iostream>
void BubbleSort(int a[], int n) {
int i, j, flag = 1;
int temp;
for(i = 1; i < n && flag == 1; i++) {
flag = 0;
for(j = 0; j < n - i; j++) {
if(a[j] > a[j + 1]) {
flag = 1;
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
int main() {
const int n = 5;
int a[n] = {5, 2, 8, 1, 6};
BubbleSort(a, n);
for(int i = 0; i < n; i++) {
std::cout << a[i] << " ";
}
std::cout << std::endl;
return 0;
}