1.将数组A中的内容和数组B中的内容进行交换。(数组一样大)
#include
using namespace std;
#include<stdlib.h>
void Print(int array[], int n) {
for (int i = 0; i < n; i++)
cout << array[i] << endl;
}
int main() {
int array1[10] = { 1,2,3,4,5,6,7,8,9,10 };
int array2[10] = { 11,12,13,14,15,16,17,18,19,20 };
Print(array1, 10);
Print(array2, 10);
for (int i = 0; i < 10; i++) {
int temp = array1[i];
array1[i] = array2[i];
array2[i] = temp;
}
Print(array1, 10);
Print(array2, 10);
system("pause");
return 0;
}
2.2. 计算1 / 1 - 1 / 2 + 1 / 3 - 1 / 4 + 1 / 5 …… + 1 / 99 - 1 / 100 的值
#define _crt_secure_no_warnings
#include
#include<stdlib.h>
using namespace std;
int main() {
double sum = 0, temp;
for (int i = 1; i <= 100; i++) {
temp = 1.0 / i * (i % 2 == 0 ? -1 : 1);
sum += temp;
}
cout << sum << endl;
system("pause");
return 0;
}
3.3. 编写程序数一下 1到 100 的所有整数中出现多少次数字9。
#define _crt_secure_no_warnings
#include
#include<stdlib.h>
using namespace std;
int main() {
int count = 0;
for (int i = 1; i <= 100; i++) {
if (i % 10 == 9) {
count++;
}
if (i / 10 % 10 == 9) {
count++;
}
}
cout << count << endl;
system(“pause”);
return 0;
}