#include<stdio.h> int main() { float q; float cost; scanf("%f",q); if (q<0) { printf("Invalid Value!"); } else { if (q<=50) { cost=q*0.53; } else { cost=50*0.53+(q-50)*0.58; } printf("cost = %.2l",cost); } return 0; }
时间: 2024-01-19 08:05:12 浏览: 27
你的代码有几处错误。首先,scanf函数的第二个参数应该传入变量的地址,而不是变量的值。所以应该改为scanf("%f", &q)。
其次,在printf函数中,你使用了%.2l格式符,但是应该使用%.2f格式符来输出浮点数的值,而不是使用%l。
下面是修改后的代码:
```
#include <stdio.h>
int main() {
float q;
float cost;
scanf("%f", &q);
if (q < 0) {
printf("Invalid Value!");
}
else {
if (q <= 50) {
cost = q * 0.53;
}
else {
cost = 50 * 0.53 + (q - 50) * 0.58;
}
printf("cost = %.2f", cost);
}
return 0;
}
```
注意:在输出结果时,cost变量应该保留两位小数,所以使用了%.2f格式符,并且在字符串末尾添加了换行符\n。
相关问题
#include <stdio.h> int main() { char level; float amount, cost; scanf("%c%f", &level, &amount); if(level=='A') cost = amount * 19.99; if(level=='B') cost = amount * 17.99; if(level=='C') cost = amount * 15.99; if(level=='D') cost = amount * 11.99; else{ printf("No available.\n"); return 0; } printf("%.2f\n", cost); return 0; }我的代码对上面的问题为什么通过率只有40%
根据您提供的代码和问题,可能的原因如下:
1. 输入格式不匹配:您使用`scanf("%c%f", &level, &amount)`来读取字符和浮点数,但是如果输入的字符后面没有空格或者回车,程序会把它当作浮点数的一部分,导致输入格式不匹配。建议使用`scanf(" %c %f", &level, &amount)`来读取输入,这样可以忽略前面的空格和回车。
2. 缺少else-if语句:在判断level的值时,您使用了多个独立的if语句,而应该使用else-if语句来避免重复判断。否则,当level的值为D时,也会执行最后一个if语句,导致输出错误结果。
修改后的代码如下:
```
#include <stdio.h>
int main() {
char level;
float amount, cost;
scanf(" %c %f", &level, &amount);
if(level=='A')
cost = amount * 19.99;
else if(level=='B')
cost = amount * 17.99;
else if(level=='C')
cost = amount * 15.99;
else if(level=='D')
cost = amount * 11.99;
else{
printf("No available.\n");
return 0;
}
printf("%.2f\n", cost);
return 0;
}
```
注意,修改后的代码只是解决了可能的原因之一,如果仍然无法通过测试,请检查其他可能的错误。
c语言完成:建筑钉子1.2元一个,第一天买100个,第二天开始,每天买前一天的3倍,直至当天购买的钉子个数达 到n个,n从键盘输入。编写程序求每天平均花多少钱? 【输入形式】 输入提示信息:"Input n:" 输入格式:"%d"#include <stdio.h> int main() int i, j, n; float sum = 0, aver; printf("input n :"); scanf("%d",&n); j=100; int zhong=0; for(i=1;i++){ zhong+=j; sum+=1.2*j; j*=3; if(zhong>=n){ break; }} aver=sum/i;
首先,我们需要从键盘输入n的值。然后我们可以使用一个循环来模拟每一天的购买。在循环中,我们可以维护一个变量来记录总花费,以及一个变量来记录当天购买的钉子个数。每一天,我们将当天购买的钉子个数设为前一天购买的3倍,并将总花费增加当天购买的钉子个数乘以钉子的单价。当当天购买的钉子个数达到n时,我们停止循环。最后,我们将总花费除以天数,就得到了每天的平均花费。
以下是代码的示例实现:
```
#include <stdio.h>
int main()
{
int n;
printf("Input n: ");
scanf("%d", &n);
int total_cost = 0; // 总花费
int num_nails = 100; // 当天购买的钉子个数
int day = 1; // 第几天
while (num_nails < n)
{
total_cost += num_nails * 1.2; // 当天购买的总价格
num_nails *= 3; // 当天购买的钉子个数
day++; // 下一天
}
total_cost += num_nails * 1.2; // 加上最后一天购买的总价格
printf("Average cost per day: %.2f", (double)total_cost / day); // 输出每天的平均花费
return 0;
}
```
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)