C语言输入:工资数,小时数(整数,空格分隔), 输出:工资/小时数(精确到小数点后2位),并根据四舍五入取整,然后将取整的数平方后计算一共有几位,后三位分别是什么。***输入数据格式***:"%d %d" ***输出数据格式***:"%.2f\n" "%d\n" "%d\n" "%d %d %d\n"
时间: 2024-01-17 14:22:15 浏览: 117
以下是实现该功能的C语言代码:
```c
#include <stdio.h>
#include <math.h>
int main()
{
int salary, hours;
scanf("%d %d", &salary, &hours);
float wage_per_hour = (float)salary / hours;
printf("%.2f\n", wage_per_hour);
int rounded_wage = (int)(wage_per_hour + 0.5);
int squared_wage = rounded_wage * rounded_wage;
int num_of_digits = 0;
int copy_of_squared_wage = squared_wage;
while (copy_of_squared_wage != 0) {
num_of_digits++;
copy_of_squared_wage /= 10;
}
int last_three_digits = squared_wage % 1000;
int second_last_digit = (squared_wage / 10) % 10;
int last_digit = squared_wage % 10;
printf("%d\n", rounded_wage);
printf("%d\n", num_of_digits);
printf("%d %d %d\n", num_of_digits, second_last_digit, last_digit);
return 0;
}
```
解释:首先读入工资数和小时数,计算出每小时工资数,并输出到小数点后两位。然后将每小时工资数四舍五入取整,并计算取整后的数的平方。接着,计算平方数的位数和最后三个数字,并输出这些结果。最后返回0,表示程序正常结束。
阅读全文