题目
题目描述
输入一个不小于 100 且小于 1000,同时包括小数点后一位的一个浮点数,例如123.4,要求把这个数字翻转过来,变成4.321并输出。
输入格式
无
输出格式
无
输入输出样例
输入 #1复制
123.4
输出 #1复制
4.321
思路与代码
1.先把带一位小数的数转化为整数,为什么转化,如果不转化的话就无法取余。总之就是可以把每一位都得到获取到。
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <string>
#include <stack>
#include <set>
#define IOS ios::sync_with_stdio(false), cin.tie(0)
using namespace std;
int main(){
float a;
int c;
int b;
cin>>a;
c=a*10;//转化为整数
b=c%10;
cout<<b;
cout<<".";//不要忘记输出小数点
c=c/10;
b=c%10;
cout<<b;
c=c/10;
b=c%10;
cout<<b;
c=c/10;
b=c%10;
cout<<b;
}
2.直接把读进去的数当成字符串,反转一下输出,这个会用到string和reverse,(字符串反转)
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <string>
#include <stack>
#include <set>
#define IOS ios::sync_with_stdio(false), cin.tie(0)
using namespace std;
int main(){
string s;
cin>>s;
reverse(s.begin(),s.end());
cout<<s<<endl;
}