转为float的吧!int类型还真的没小数点的
#include<stdio.h>
int main() {
int a,b,c,d;
float ave; //平均值
scanf_s("%d,%d,%d,%d", &a, &b, &c, &d);//输入四门成绩
ave = 1.0 * (float)(a + b + c + d)/4;
printf("%.1f", ave); //输出平均分
return 0;
}
我先把代码写出来,里面注释,看着会跟方便
#include <stdio.h>
int main()
{
int score = 0;
float sum = 0.0f; // f 表示 时 float 型
// 下面存放 4 的 成绩
for (int i = 0; i < 4; i++)
{
scanf("%d", &score);
sum = sum + score; // 每一门成绩都加起来
}
printf("%.1f\n", sum / 4); // .1f 表示 小数点后一位的格式打印
return 0;
}
温馨提示:注意审题,题目是要求输入分数值字符串,并用逗号隔开
1、运行效果
2、代码
#include <stdio.h>
int main(void) {
//60,75,80,90
printf("请你分别输入四门功课成绩(整数):\n");
//定义字符数组,保存字符串值
char scoreString[20];
memset(scoreString,0,sizeof(scoreString));
scanf("%s", scoreString);
//定义整型数组,保存整数分值
int scoreList[4];
//字符串分组
const char * split = ",";
char * c;
c = strtok(scoreString,split);
int i=0;
while(c!=NULL) {
//printf ("%s\n",c);
//printf ("%d\n",atoi(c));
scoreList[i]=atoi(c);
i++;
c = strtok(NULL,split);
}
//计算平均分
int scoreTotal=0;
for(int j=0;j<4;j++){
//printf ("%d\n",scoreList[j]);
scoreTotal+=scoreList[j];
}
printf ("4门课的总分数:%d\n",scoreTotal);
printf ("4门课的平均分:%.1f\n",(scoreTotal/4.0));
return 0;
}
3、知识点
数据类型:整型int、字符char、字符数组char[](相当于字符串)、浮点值
控制语句:while、for循环
内置函数:scanf、memset、sizeof、strtok、atoi