回文数-高精度-模拟

练习下类

https://www.luogu.org/problemnew/show/P1015


//#include "stdafx.h"
#include 
#include 
using namespace std;


class LINT
{
private :
    char num[200];
    int n;//进制
public :
    LINT(){};
    LINT(int n,char m[]);	//n进制,数m	
    ~LINT();
    bool IsHuiWen();//是否回文,是1
    LINT Reverse();//颠倒
    LINT operator+(const LINT & b);//重载加法运算
    void Print();
    
};
/*
功能:构造函数
输入:进制n,实际数字字符串:m
输出:无
日期:
*/
LINT::LINT(int n, char m[])
{
    this->n=n;
    //strcpy_s(num,strlen(m)+1,m);
    strcpy(num,m);
}
/*
功能:析构函数
输入:无
输出:无
日期:
*/
LINT::~LINT()
{}
/*
功能:输出私有变量,方便测试
输入:
输出:进制n,实际数字字符串:m
日期:
*/
void LINT::Print()
{
    cout<<"进制:"<num);

    tmp.n = this->n;

    for(i=0;inum[i];
    }
    tmp.num[len]='\0';
    return tmp;
}
/*
功能:重载运算符+ 考虑16进制,且认为ab进制相同
输入:LINT a+b
返回: a+b
日期:
*/
LINT LINT::operator+(const LINT&bb)
{
    LINT c;
    LINT b;
    LINT a;
    b= bb;
    a=*this;
    int lena = strlen(a.num);//a的长度
    int lenb = strlen(b.num);
    int minLenAb = lena>lenb?lenb:lena;//两者的最小,从高位向低位求和
    int maxLenAb = lena>lenb?lena:lenb;//max between a and b
    //首先转换成数字,且完成16进制的字符转换
    if (b.n==16)
    {
        for (int i=0; i='0' && a.num[i]<='9') a.num[i]-='0';
            else if(a.num[i]=='a' || a.num[i]=='A') a.num[i]=10;
            else if(a.num[i]=='b' || a.num[i]=='B') a.num[i]=11;
            else if(a.num[i]=='c' || a.num[i]=='C') a.num[i]=12;
            else if(a.num[i]=='d' || a.num[i]=='D') a.num[i]=13;
            else if(a.num[i]=='e' || a.num[i]=='e') a.num[i]=14;
            else if(a.num[i]=='f' || a.num[i]=='f') a.num[i]=15;
        }
        for (int i=0; i='0' && b.num[i]<='9') b.num[i]-='0';
            else if(b.num[i]=='a' || b.num[i]=='A') b.num[i]=10;
            else if(b.num[i]=='b' || b.num[i]=='B') b.num[i]=11;
            else if(b.num[i]=='c' || b.num[i]=='C') b.num[i]=12;
            else if(b.num[i]=='d' || b.num[i]=='D') b.num[i]=13;
            else if(b.num[i]=='e' || b.num[i]=='e') b.num[i]=14;
            else if(b.num[i]=='f' || b.num[i]=='f') b.num[i]=15;
        }
    }
    else
    {
        for (int i=0; i=0 && c.num[i]<=9) c.num[i]+='0';
            else if (c.num[i]==10) c.num[i]='a';
            else if (c.num[i]==11) c.num[i]='b';
            else if (c.num[i]==12) c.num[i]='c';
            else if (c.num[i]==13) c.num[i]='d';
            else if (c.num[i]==14) c.num[i]='e';
            else if (c.num[i]==15) c.num[i]='f';
        }
    }
    //调到
    
    return c.Reverse();
}
/*
功能:重载运算符+ 没有考虑16进制 只对了3个点 还有1个点
输入:LINT a
返回:LINT a+b后的值
日期:
*/
/*
LINT LINT::operator+(const LINT & b)
{
    LINT c;
    int lena = strlen(this->num);
    int lenb = strlen(b.num);
    int lenc = lena>lenb ? lenb : lena;
    for (int i=0; inum[lena-1-i]-'0';
    }

    for (int i=lenc; inum[lena-1-i];
    }
    for (int i=lenc; ilenb) c.num[lena] = '\0';
    else c.num[lenb] = '\0';

    c.n = b.n;
    //处理针对n进制,这里因为a,b都是n进制,不做进制转换了
    lenc = strlen(c.num);
    c.num[lenc]='0';
    for (int i=0;i>n;
    cin>>num;
    
    LINT a(n,num);

    if (a.IsHuiWen()) {cout<<"STEP=0";return 0;}

    for (k=1;k<=30; k++)
    {
        LINT b = a.Reverse();
        a = a+b;
    //	a.Print();
        if (a.IsHuiWen())break;

    }

    if (k<=30) {cout<<"STEP="<

你可能感兴趣的:(算法)