翻转数值

输入1234  输出4321

输入-1234 输出-4321

要求实现判断:输入数为-2^(32-1)~2^(32-1)-1,翻转数-2^(32-1)~2^(32-1)-1,在此范围输出翻转数,否则提示溢出.

实现:法一取余放入队列,取出队首乘倍数实现翻转

#include<iostream>
#include<queue>
using namespace std;
int dealFuntion(long long input);
long long mypow(int n);
int main(int argc, char* argv[])
{
    
    long long a=0;
    cout<<"输入需要翻转的数值:"<<endl;
    cin>>a;
    int b=dealFuntion(a);
    cout<<"翻转的数值为:"<<endl;
    cout<<b<<endl;
    return 0;
}
int dealFuntion(long long input)
{
if(input>2147483647||input<-2147483648)
{
cout<<"输入数溢出"<<endl;
return 0;
}
    queue<int> s1;
    long long i=0;
    int j=0;
    int symbol=1;
    int count=0;
    long long res;
    long long tmp;
    if(input ==0)
    {
        return 0;
    }
tmp=input;
    if(input<0)
    {
        symbol=-1;
tmp=-input;
    }
    if(tmp%10!=0)
    {
        for(i=1;(j=(tmp/i)%10)!=0;i=i*10)
        {
            s1.push(j);
            count++;
        }
    }
    else
    {   
        s1.push(0);
        count++;
        for(i=10;(j=(tmp/i)%10)!=0;i=i*10)
        {
            s1.push(j);
            count++;
        }
    }
    for(;(!s1.empty());s1.pop())
    {
        int tmp=s1.front();
        res=res+tmp*mypow(count-1);
if(res>2147483647)
{
            cout<<"翻转数溢出"<<endl;
return 0;
}
        count--;
    }
return (int)res*symbol;
}
long long mypow(int n)
{
int i=0;
long long res=1;
for(i=1;i<=n;i++)
{
res=10*res;
}
return res;
}
 
 
法二:数值转为字符串,翻转字符串,字符串转为数值实现翻转
#include<iostream>
#include<sstream>
#include<algorithm>
#include<string>
#include<stdlib.h>
using namespace std;
string lltostring(long long input);
long long rev_stringtoll(string my_string);
void rev_result(long long input);
int main(int argc, char* argv[])
{
long long input;
cout<<"输入翻转数值"<<endl;
cin>>input;
rev_result(input);
return 0;
}
void rev_result(long long input)
{
long long my_ll;
int symbol=1;
if(input>2147483647||input<-2147483648)
{
cout<<"输入数溢出"<<endl;
return;
}
if(input==0)
{
cout<<"翻转结果为:"<<endl<<"0"<<endl;
return;
}
if(input<0)
{
symbol=-1;
input=-input;
}
my_ll=rev_stringtoll(lltostring(input))*symbol;
if(my_ll>2147483647||my_ll<-2147483648)
{
cout<<"翻转数溢出"<<endl;
}
else
{
cout<<"翻转结果为:"<<endl<<my_ll<<endl;
}
}
long long rev_stringtoll(string my_string)
{
reverse(my_string.begin(),my_string.end());
return strtoll(my_string.c_str(),NULL,10);
}
string lltostring(long long input)
{
stringstream my_stream;
string res_string;
my_stream<<input;
my_stream>>res_string;
return res_string;
}

/*
//实现字符串翻转
char *reverse_str(char *str) {
if(NULL == str) { //字符串为空直接返回
return str;
}
char *begin;
char *end;
begin = end = str;

while(*end != '\0') { //end指向字符串的末尾
end++;
}
--end;

char temp;
while(begin < end) { //交换两个字符
temp = *begin;
*begin = *end;
*end = temp;
begin++;
end--;
}

return str; //返回结果
}
void main() {
char str[] = "123456";
printf(reverse_str(str));
}
*/

转载于:https://www.cnblogs.com/jest549/p/11443710.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值