题目
若运行时输入:4.4<回车>,则以下程序的运行结果是________。#include <stdio.h>void main(void)(float CostPrice,SellingPrice;printf(“Enter Cost Price :”);scanf(“%f”,CostPrice);if(CostPrice>=5){SellingPrice=CostPrice+CostPrice*0.25;printf(“Selling Price(0.25)%6.2f”,SellingPrice);)else(SellingPrice=CostPrice+CostPrice*0.30;printf(“Selling Price(0.30)%6.2f”,SellingPrice);)}
若运行时输入:4.4<回车>,则以下程序的运行结果是________。
#include <stdio.h>
void main(void)
{
float CostPrice,SellingPrice;
printf(“Enter Cost Price $:”);
scanf(“%f”,CostPrice);
if(CostPrice>=5)
{
SellingPrice=CostPrice+CostPrice*0.25;
printf(“Selling Price(0.25)$%6.2f”,SellingPrice);
}
else
{
SellingPrice=CostPrice+CostPrice*0.30;
printf(“Selling Price(0.30)$%6.2f”,SellingPrice);
}
}
题目解答
答案
Selling Price(0.30)$5.72
解析
本题主要考察C语言中变量输入、条件判断及输出格式的综合应用,具体解题步骤如下:
步骤1:分析程序功能
程序的核心逻辑是:
- 提示用户输入成本价(
CostPrice); - 根据成本价是否≥5,计算售价(
SellingPrice):- 若
CostPrice≥5,售价=成本价×1.225(加25%); - 若
CostPrice<5,售价=成本价×1.30(加30%);
- 若
- 按指定格式输出售价。
步骤2:检查输入与变量类型匹配
题目输入为4.4(float类型),程序中CostPrice定义为float,scanf使用%f格式符读取,输入输出类型匹配,无错误。
步骤3:条件判断
输入CostPrice=4.4,因4.4<5,执行else分支:
$SellingPrice = 4.4 + 4.4 \times 0.30 = 4.4 \times 1.30 = 5.72$
步骤4:输出格式验证
输出格式为“Selling Price(0.30)$%6.2f”:
%6.2f表示浮点数占6列、保留2位小数,5.2f确保5.72显示为两位小数,%6使输出右对齐(不影响影响结果可读性)。