题目链接
大数模板题
Description:
There is a positive integer X, X’s reversion count is Y. For example, X=123, Y=321; X=1234, Y=4321. Z=(X-Y)/9, Judge if Z is made up of only one number(0,1,2…9), like Z=11,Z=111,Z=222,don’t consider '+'and ‘-’.
Input:
Input contains of several test cases. Each test case only contains of a number X, L is the length of X. ( 2 <= L < 100)
Output:
Output “YES”or “NO”.
样例输入:
10
13
样例输出:
YES
YES
题意:
就是问你一个整数x,y是x的逆序,例如X = 123,Y = 321;Z = (X - Y) / 9;现在问你Z是否只有(0-9)其中一个数字组成。X的长度2到100.
思路:
其实一眼看到这么长的数,就知道是大数模板,以为之前没咋地写过大数,总以为很难的样子,其实是非常简单的。(lll¬ω¬),Z是一定可以被9整出的,所以没必要考虑什么浮点数。这是我队友想到的。让我交的更有自信了。用set去维护,是否只有(0-9)其中一个数字组成。那这题也就是简单题了
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<set>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 1e3+5;
string a,b,c;
//大整数减法的板子,真好用 哈哈。
string sub(string a, string b) {
string c;
bool ok = 0;
int len1 = a.length();
int len2 = b.length();
int len = max(len1, len2);
for(int i = len1; i < len; i++)
a = "0" + a;
for(int i = len2; i < len; i++)
b = "0" + b;
if(a < b) {
string temp = a;
a = b;
b = temp;
ok = 1;
}
for(int i = len - 1; i >= 0; i--) {
if(a[i] < b[i]) {
a[i - 1] -= 1;
a[i] += 10;
}
char temp = a[i] - b[i] + '0';
c = temp + c;
}
int pos = 0;
while(c[pos] == '0' && pos < len) pos++;
if(pos == len) return "0";
if(ok) return "-" + c.substr(pos);
return c.substr(pos);
}
//大数除以整形数
string Except(string s, int x) {
int cmp = 0, ok = 0;
string ans = "";
for(int i = 0; i < s.size(); i++) {
cmp = (cmp * 10 + s[i] - '0');
if(cmp >= x) {
ok = 1;
ans += (cmp / x + '0');
cmp %= x;
} else {
if(ok == 1)
ans += '0'; //注意这里
}
}
return ans;
}
int main(){
while(cin>>a){
set<char> st;
st.clear();
b = a;
reverse(a.begin(),a.end());
c = sub(b,a);
if(c[0] == '-'){
c.erase(0,1);
}
if(c.size() == 1){
cout<<"YES"<<endl;
continue;
}
//这个大整数除法 c不能为小于等于0的数
string ans = Except(c,9);
for(int i=0;i<ans.size();i++){
st.insert(ans[i]);
}
if(st.size() == 1){
printf("YES\n");
}else{
printf("NO\n");
}
}
return 0;
}