今天练习了数组求最值,数组求和。
求最值
#include <stdio.h>
int main()
{
int arr[] = { 33,5,22,44,55 };
int max = arr[0];
int len = sizeof(arr) / sizeof(int);
for (int i = 0; i < len; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
}
printf("%d\n", max);
return 0;
}
数组求和
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int contains(int arr[], int len, int num);
int main()
{
int arr[10] = { 0 };
srand(time(NULL));
int len = sizeof(arr) / sizeof(int);
for (int i = 0; i < len; )
{
int num = rand() % 100 + 1;
int res = contains(arr, len, num);
if (res == 0)
{
arr[i] = num;
i++;
}
}
int sum = 0;
for (int i = 0; i < len; i++)
{
sum = sum + arr[i];
}
printf("%d\n", sum);//数列和
printf("%d\n", sum / len);//数列的平均数
//统计有多少个数比平均数小
int count = 0;
for (int i = 0; i < len; i++)
{
if (arr[i] < sum / len)
{
count++;
}
}
printf("%d\n", count);
return 0;
}
int contains(int arr[], int len, int num)//有重复,则输出1
{
for (int i = 0; i < len; i++)
{
if (arr[i] == num)
{
return 1;
}
}
return 0;
}