gpt4 book ai didi

c - 六边形的表面积

转载 作者:行者123 更新时间:2023-11-30 18:53:00 25 4
gpt4 key购买 nike

我目前正在尝试学习C,并编写了这个程序来计算正六边形的面积:

#include <stdio.h>
#include <math.h>

void main(){
int a;
float ans;
scanf("%d", &a); // get length of side

ans = ((pow(a, (1/3)))/2)*(a*a);

printf("%f", ans);
}

但是,它输出看似随机的数字。

最佳答案

首先,您的代码无法编译( Missing semicolon ),并且您应该使用 int main()而不是void main() .

其次你的公式也错了,边长为正六边形的面积a计算为((3√3)/2)*a² .

第三个表达式如1/3总是产生零,因为两者都是整数,为了获得预期的行为,使其中之一为浮点/ double 。喜欢 1.0/3(float)1/3等等

#include <stdio.h>
#include <math.h>

int main()
{
int a;
float ans;
scanf("%d", &a); // get length of side
ans = (3*sqrt(3)/2.0)*a*a;
printf("%f", ans);
}

关于c - 六边形的表面积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33722692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com