Reversion Count

时间:2023-03-08 20:23:06

字符串基础用法题,包含有大数减法大数除法模板,不难理解,代码如下:

#include<stdio.h>
#include<string.h>
#include<string>
#include<math.h>
#include<time.h>
#include<iostream>
#include<ctype.h>
#include<map>
#include<set>
#include<algorithm>
#include<stdlib.h>
#include<queue>
#include<stack>
using namespace std;
string a,b,c;
string sub(string a, string b)
{
string c;
bool ok = ;
int len1 = a.length();
int len2 = b.length();
int len = max(len1, len2);
for(int i = len1; i < len; i++)//短的串前边加0方便后边计算
a = "" + a;
for(int i = len2; i < len; i++)
b = "" + b;
if(a < b)//判断正负
{
string temp = a;
a = b;
b = temp;
ok = ;
}
for(int i = len - ; i >= ; i--)
{
if(a[i] < b[i])
{
a[i - ] -= ;
a[i] += ;
}
char temp = a[i] - b[i] + '';
c = temp + c;//此处不能写为c+temp因为是倒着算,需要正着存,顺序写错就不对了,自己可以试几组样例
}
int pos = ;
while(c[pos] == '' && pos < len) pos++;//去除前导0
if(pos == len) return "";
if(ok) return "-" + c.substr(pos);
return c.substr(pos);
} string chufa(string s,int x)
{
int jj=,fla=;
string ans="";
for(int i=; i<s.size(); i++)
{
jj=jj*+s[i]-'';
if(jj>=x)
{
fla=;
ans+=jj/x+'';
jj%=x;
}
else
{
if(fla==)//在运算过程中不够除商0;
ans+='';
}
}
return ans;
}
int main()
{
int n,i,j;
while(cin>>a)
{
set<char>qq;
b=a;
reverse(a.begin(),a.end());
c=sub(b,a);
if(c[]=='-')//题目不要求正负,可以删除再计算
c.erase(,);
if(c.size()==)
{
printf("YES\n");
continue;
}
string ans=chufa(c,); for(int i=; i<ans.size(); i++)
{
qq.insert(ans[i]);
}
if(qq.size()==)
printf("YES\n");
else
printf("NO\n");
}
}