/// 1.交换两个数组的元素,两个数组元素个数相同
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int arr1[] = { 2, 3, 5, 4, 6, 7, 9, 12, 34, 45 };
int arr2[] = { 1, 9, 8, 0, 4, 6, 34, 56, 7, 23 };
int i = 0, temp = 0;
int size = sizeof(arr1) / sizeof(*arr1);//求数组元素的个数
printf("交换前:\n");
printf("arr1数组元素为:");
for (i = 0; i < size; i++)
{
printf("%4d", arr1[i]);
}
printf("\n");
printf("arr2数组元素为:");
for (i = 0; i < size; i++)
{
printf("%4d", arr2[i]);
}
printf("\n");
for (i = 0; i < size; i++)
{
temp = arr1[i];
arr1[i] = arr2[i];
arr2[i] = temp;
}
printf("交换后:\n");
printf("arr1数组元素为:");
for (i = 0; i < size; i++)
{
printf("%4d", arr1[i]);
}
printf("\n");
printf("arr2数组元素为:");
for (i = 0; i < size; i++)
{
printf("%4d", arr2[i]);
}
fflush(stdin);
getchar();
return 0;
}
//2.求1/1 - 1/2 + 1/3 - 1/4......-1/100
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int i = 0;
int flag = 1;
double sum = 0;
for (i = 1; i <= 100; i++)
{
sum = sum + (1.0 / i)*flag;//(1.0/i)表示强制转换为浮点型
flag = -flag;//实现加减交替
//sum=sum+pow((-1),(1+i))*(1.0/i);//pow(a,b)为a的b次方,需要调用math.h
}
printf("求得结果为%lf", sum);
getchar();
return 0;
}
//3.求1到100里面9出现的次数
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int i = 0, sum = 0;
for (i = 1; i <= 100; i++)
{
if (i / 10 == 9)//十位上的9
{
sum++;
}
if (i % 10 == 9)//个位上的9
{
sum++;
}
}
printf("1到100之间9总共出现了%d次", sum);
getchar();
return 0;
}