题目描述
输入一个不小于 100 且小于 1000,同时包括小数点后一位的一个浮点数,例如123.4,要求把这个数字翻转过来,变成4.321并输出。
输入输出样例
输入
123.4
输出
4.321
分析:
关键要抓住各位数字的分离,浮点数x取个位数,若用x%10则会附带小数,故一开始要将整数部分与小数部分分离,用floor。
代码
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
float x,d,m;
int a,b,c,n;
scanf("%f",&x);
if(x>=100&&x<1000)
{
n=floor(x);//向下取整
a=n/100;//取百位数
b=n/10%10;//取十位数
c=n%10;//取个位数
d=(x-n)*10;//取小数点后的数字
m=d+c*0.1+b*0.01+a*0.001;
printf("%.3f",m);//输出结果
}
}