C++80分求解

P5705 【深基2.例7】数字反转

HITvici @ 2022-12-02 12:43:17

#include <iostream>

using namespace std;

int main()
{
    int a1,a2,a3,a4;
    float b;
    cin>>b;
    a1=(int)b/100;
    a2=(int)(b-a1*100)/10;
    a3=(int)(b-a1*100-a2*10)/1;
    a4=(b-a1*100-a2*10-a3)*10;
    cout<<a1*0.001+a2*0.01+a3*0.1+a4;
    return 0;
}

第三个测试点输入888.8,输出的是7.888,不知道为啥,求大佬支招(萌新刚学c++捏)


by ud2_ @ 2022-12-02 13:08:05

@xu1341060890 float 没法精确表示 888.8,实际上存下的值是 888.79998779296875。这本质上是个反转字符串题,提到“浮点数”完全是误导。


by lucky_loser @ 2022-12-02 13:11:29

我直接用scanf和printf

#include <bits/stdc++.h>
using namespace std;
int main()
{
   har a, b, c, d;
    scanf("%c%c%c.%c", &a, &b, &c, &d);
    printf("%c.%c%c%c", d, c, b, a);
    return 0;
}

by HLJoier @ 2022-12-02 13:12:14

#include<bits/stdc++.h>
using namespace std;
int main() {
    string s;
    cin>>s;
    for(int i=s.size()-1;i>=0;--i)
        cout<<s[i];
    cout<<endl;
    return 0;
}

@xu1341060890


by HLJoier @ 2022-12-02 13:13:17

@yty2011 ???char打错了


by HITvici @ 2022-12-02 13:15:00

@yty2011 哈哈哈哈哈哈我主要是刚学了C++想试一试,不过谢谢


by HITvici @ 2022-12-02 13:15:15

@ud2_ 明白了,谢谢大佬!!


by HITvici @ 2022-12-02 13:15:34

@HY36_2022qujianxiu 明白了谢谢!!


by LitwoO @ 2022-12-30 22:08:28

@xu1341060890 why not try like this:

#include<iostream>;
int main(){
    char a,b,c,d,f;
    cin>>a>>b>>c>>d>>f;
    cout<<f<<d<<c<<b<<a;
    return 0;
}

by HITvici @ 2022-12-31 11:01:15

@zhuyihaoprogram 很简洁,比我的方法好= =


by RayLiuLiu @ 2023-02-04 11:33:36

直接用

string
头文件:#include<string>

反转用reverse:

头文件:#include<algorithm>
reverse(字符串名.begin(),字符串名.end());

代码:

#include<iostream>
#include<algorithm>//反转头文件
#icnlude<string>//字符串头文件
#include<cmath>
using namespase std;
int main(){
  string a;
  cin>>a;
  reverse(a.begin(),a.end());
  cout<<a;
}

|