将数组A中的内容和数组B中的内容进行交换(数组一样大)
思路分析:
数组遍历,创建临时变量,依次交换数组中的元素
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void swap(int arr1[], int arr2[],int n)
{
int tmp;
for (int i = 0; i < n; ++i)
{
tmp = arr1[i];
arr1[i] = arr2[i];
arr2[i] = tmp;
}
}
void dayin(int arr[], int n)
{
int i = 0;
for (i; i < n; ++i)
{
printf("%d ", arr[i]);
}
printf("\n");
}
int main()
{
int arr1[] = { 9, 5, 2, 7, 6 };
int arr2[] = { 1, 2, 3, 4, 5, 66 };
swap(arr1, arr2,6);
dayin(arr1, 6);
system("pause");
return 0;
}
计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值
思路分析:
首先确定计算的浮点数类型,然后注意奇数项为正,偶数项为负
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
int i = 0;
double sum = 0;//计算的结果为小数,用浮点型
for (i = 1; i < 101; ++i)
{
sum = sum+ pow(-1, i + 1)/i;//奇数项为正,偶数项为负
}
printf("%f\n", sum);
system("pause");
return 0;
}
编写程序数一下 1到 100 的所有整数中出现多少次数字9
思路分析:
十位和个位只要遇到9,b就自增,最后返回的值就是出现9的次数
#include <stdio.h>
int main()
{
int a = 0;
int b = 0;
for (a = 1; a < 101; a++)
{
if (a % 10 == 9)
{
b++;
}
if (a / 10 == 9)
{
b++;
}
}
printf("%d\n", b);
system("pause");
return 0;
}