c语言,随机输出十道有加减乘除并判断正误求平均数最大值最小值将正确答案按从小到大和从大到小排序
时间: 2024-03-13 10:47:08 浏览: 66
c代码-输入三个数实数a、b、c,按从大到小输出,并输出最大值和最小值。
好的,以下是我给出的代码,它可以随机输出十道包含加减乘除的数学题,并且判断正误,求平均数、最大值和最小值,最后按从小到大和从大到小排序正确答案:
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int i, a, b, c, d, ans, correct = 0, sum = 0, max = -1000000, min = 1000000;
char op;
srand(time(NULL));
for(i = 0; i < 10; i++)
{
a = rand() % 100 + 1;
b = rand() % 100 + 1;
c = rand() % 100 + 1;
d = rand() % 100 + 1;
switch(rand() % 4)
{
case 0:
op = '+';
ans = a + b + c + d;
break;
case 1:
op = '-';
ans = a - b - c - d;
break;
case 2:
op = '*';
ans = a * b * c * d;
break;
case 3:
op = '/';
ans = a / b / c / d;
break;
}
printf("%d %c %d %c %d %c %d = ", a, op, b, op, c, op, d);
int inputAns;
scanf("%d", &inputAns);
if(inputAns == ans)
{
printf("Correct!\n");
correct++;
}
else
{
printf("Wrong! The correct answer is %d.\n", ans);
}
sum += ans;
if(ans > max)
{
max = ans;
}
if(ans < min)
{
min = ans;
}
}
printf("The average is %d.\n", sum / 10);
printf("The maximum is %d.\n", max);
printf("The minimum is %d.\n", min);
int correctAnswers[10];
for(i = 0; i < 10; i++)
{
a = rand() % 100 + 1;
b = rand() % 100 + 1;
c = rand() % 100 + 1;
d = rand() % 100 + 1;
switch(rand() % 4)
{
case 0:
op = '+';
correctAnswers[i] = a + b + c + d;
break;
case 1:
op = '-';
correctAnswers[i] = a - b - c - d;
break;
case 2:
op = '*';
correctAnswers[i] = a * b * c * d;
break;
case 3:
op = '/';
correctAnswers[i] = a / b / c / d;
break;
}
}
printf("The correct answers in ascending order are: ");
for(i = 0; i < 10; i++)
{
int j;
int minIndex = i;
for(j = i + 1; j < 10; j++)
{
if(correctAnswers[j] < correctAnswers[minIndex])
{
minIndex = j;
}
}
int temp = correctAnswers[i];
correctAnswers[i] = correctAnswers[minIndex];
correctAnswers[minIndex] = temp;
printf("%d ", correctAnswers[i]);
}
printf("\nThe correct answers in descending order are: ");
for(i = 9; i >= 0; i--)
{
printf("%d ", correctAnswers[i]);
}
printf("\n");
return 0;
}
```
这个代码使用了随机数来生成题目和答案,然后通过scanf函数接收用户输入的答案,并且进行判断正误。在判断完正误之后,我们可以计算出平均数、最大值和最小值。接下来,我将正确答案存储到一个数组中,并且使用选择排序算法对其进行从小到大和从大到小的排序,最后输出结果。
阅读全文