萌新求助~~求n个数的平均数,运行是对的但提交上去错了
程序代码:
#include<stdio.h> int main() { float a,b,ave; int n,i; a=0; scanf("%d\n",&n); for(i=1;i<=n;i++) { scanf("%f ",&b); a+=b; } ave=a/n; printf("%.2f\n",ave); return 0; } [local]1[/local]
#include<stdio.h> int main() { float a=0,b,ave; // a 没有赋初值,输出结果存在不确定性 int n,i; a=0; scanf("%d",&n); // 多了一个换行符,有残留给第一个 b 值的可能 for(i=1;i<=n;i++) { scanf("%f",&b); a+=b; } ave=a/n; printf("%.2f\n",ave); return 0; }
[此贴子已经被作者于2020-10-24 20:08编辑过]
#include<stdio.h> int main() { int m,e,choice1; char choice2; double a,cost; printf("Enter how much gas a = "); scanf("%lf",&a); printf("90# gas seclet 90\n"); printf("93# gas select 93\n"); printf("97# gas select 97\n"); printf("Enter choice1:"); scanf("%d",&choice1); switch(choice1) { case 90#:cost=6.95 * a;break; case 93#:cost=7.44 * a;break; case 97#:cost=7.93 * a;break; default:cost=0.00;break; } printf("self-service select m\n"); printf("ancillary-service select e\n"); printf("Enter choice2:"); scanf("%c",&choice2); if(choice2 == 'm') { cost=cost * (1-0.05); } else if(choice2 == 'e') { cost=cost * (1-0.03); } else printf("\n"); printf("You need pay %.2f yuan\n",cost); return 0; }