《程序设计基础(B)Ⅰ》实验1-顺序结构程序设计(中)

版权声明:版权问题请加微信:17165380098 备注 版权问题 https://blog.csdn.net/qq_30277453/article/details/82900826

C语言实验——买糖果  

注意n元与三毛钱的区别

因此可以设置10*n 用毛做单位

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n;
    scanf("%d",&n);
    int a,b;
    a = (10*n)/3;
    b = (10*n)%3;
    printf("%d %d\n",a,b);
    return 0;
}

C语言实验——三个整数和、积与平均值

注意:

整形变量 a,b,c的平均值为浮点型 

在coding时需要强制性格式转换

另外 printf内 保留n位小数 用 %.nf(double型变量要用 %lf, .n(点n) 代表小数点后保留n位 )

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a,b,c;
    scanf("%d %d %d",&a,&b,&c);
    int sum,mul;
    sum = a+b+c;
    mul = a*b*c;
    float ave;
    ave = (float)sum/3;
    printf("%d %d %.2f\n",sum,mul,ave);
    return 0;
}

C语言实验——圆柱体计算

double 双精度浮点型变量 

float 单精度浮点型变量

因为精度限制,有的程序要求小数点后位数不同 若超过7位 要用double

#include <stdio.h>
#include <stdlib.h>
//define p 3.1415926
int main()
{
    int r,h;
    scanf("%d %d",&r,&h);
    float l,s,w,v;
    double p = 3.1415926;//该部分可用第一段注释替代
    l = 2*p*r;
    s = p*r*r;
    w = l*h;
    v= s*h;
    printf("%.2f %.2f %.2f %.2f\n",l,s,w,v);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_30277453/article/details/82900826