您的位置:首页 > 其它

Exercise2

2018-03-30 15:44 141 查看
1.将数组A中的内容和数组B中的内容进行交换。(数组一样大)2. 计算1/1-1/2+1/3-1/4+1/5 ⋯⋯ + 1/99 - 1/100 的值。3. 编写程序数一下 1到 100 的所有整数中出现多少次数字9
//1将数组A中的内容和数组B中的内容进行交换。(数组一样大)
#include<stdio.h>
#include<math.h>
#include<windows.h>

#define len 10

void swap_array()
{
int j = 0;
int i = 0;
int arr1[len] = { 1, 3, 8, 6, 5,56,56,56,5634,345};
int arr2[len] = { 5, 3, 6, 9, 3 ,56,2,134,6,54};
for (j = 0; j < len; j++)
{
arr1[j] ^= arr2[j];
arr2[j] ^= arr1[j];
arr1[j] ^= arr2[j];
}
for (i = 0; i < len; i++)
{
printf("%d ", arr1[i]);
}
printf("\n");
for (i = 0; i < len; i++)
{
printf("%d ", arr2[i]);
}
printf("\n");
}

int main()
{
swap_array();
system("pause");
return 0;
}
//2计算1 / 1 - 1 / 2 + 1 / 3 - 1 / 4 + 1 / 5 ⋯⋯ + 1 / 99 - 1 / 100 的值。
#include<stdio.h>
#include<Windows.h>

double regular_calculation()
{
int i = 1;
int flag = 1;
double sum =0;//注意 sum 的类型 double
for (; i <= 100; i++)
{
sum += (flag*1.0) / i;
flag = -flag;
}
printf("sum=%f\n", sum);
}
int main()
{
regular_calculation();
system("pause");
return 0;
}
//3. 编写程序数一下 1到 100 的所有整数中出现多少次数字9。
#include<stdio.h>
#include<Windows.h>

void count_number()
{
int i = 1;
int count = 0;
for (; i <= 100; i++)
{
if (i % 10 == 9)//99计算两次
{
count++;
}
if (i / 10 == 9)
{
count++;
}
}
printf("%d\n",count);
}
int main()
{
count_number();
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: