码迷,mamicode.com
首页 > 其他好文 > 详细

翻转数值

时间:2019-09-01 21:46:35      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:for   include   null   argv   turn   clu   pac   lib   out   

输入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));
}
*/

翻转数值

标签:for   include   null   argv   turn   clu   pac   lib   out   

原文地址:https://www.cnblogs.com/jest549/p/11443710.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!