1~a之和, 1~b的平方,以及 1~c的倒数和2020.11.10

// 1~a之和, 1~b的平方,以及 1~c的倒数和
#include<stdio.h>
#include<math.h>
double a,b,c,sum,n,y,x,k,l,v;
double t,m;
int main()
{
n=0;
x=1;
m=0;
l=0;
scanf("%lf %lf %lf",&a,&b,&c);
while(n<a)
{
n++;
k+=n;
}
for(x=1;x<=b;x++)
{
y=pow(x,2);
m+=y;
}
for(t=1;t<=c;t++)
{
v=1/t;
l+=v;
}
sum=k+m+l;
printf("%.2lf\n",sum);
return 0;
}

要计算给定三个数a、bc之间的特定数学运算,你可以使用C语言编写以下函数。这里我们分别计算1到a的(sum_a)、1到b的平方(sum_squares_b)以及1到c的倒数(sum_reciprocals_c),然后将结果四舍五入保留两位小数。 ```c #include <stdio.h> #include <math.h> // 包含sqrt()函数 double sum_to_n(int n) { double total = 0; for (int i = 1; i <= n; i++) { total += i; } return total; } double sum_of_squares(int n) { double total = 0; for (int i = 1; i <= n; i++) { total += pow(i, 2); } return total; } double sum_reciprocals_to_n(double n) { double total = 0; if (n > 0) { // 避免除以零的情况 for (int i = 1; i <= n; i++) { total += 1 / i; } } return round(total * 100) / 100; // 四舍五入并保留两位小数 } int main() { int a, b, c; printf("Enter the values of a, b, and c: "); scanf("%d %d %d", &a, &b, &c); double sum_a = sum_to_n(a); double sum_squares_b = sum_of_squares(b); double sum_reciprocals_c = sum_reciprocals_to_n(c); printf("Sum from 1 to a (rounded): %.2f\n", sum_a); printf("Sum of squares from 1 to b (rounded): %.2f\n", sum_squares_b); printf("Sum of reciprocals from 1 to c (rounded): %.2f\n", sum_reciprocals_c); return 0; } ``` 在这个程序中,`sum_to_n()`、`sum_of_squares()` `sum_reciprocals_to_n()` 分别计算所需的平方倒数,然后在main函数中获取用户输入的a、bc值,并打印出结果。 运行这个程序时,请确保不要输入负数或零,因为它们可能导致除以零的错误。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值