基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了。所求为最接近l的值。
#include <cstdio> int f(__int64 x) {
int i, sum; i = sum = ;
while (x) {
if (i & )
sum -= x%;
else
sum += x%;
++i;
x/=;
}
return sum;
} int main() {
__int64 l, r, x;
bool flg;
int t; scanf("%d", &t);
while (t--) {
scanf("%I64d%I64d", &l, &r);
for (x=l;;++x)
if (x% == )
break;
flg = false;
while (x <= r) {
if (f(x) != ) {
flg = true;
break;
}
x += ;
}
if (flg)
printf("%I64d\n", x);
else
printf("-1\n");
} return ;
}
好歹写出来大数减了,贴上吧。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std; string SRCB="";
bool stop; int check(string s) {
int sum=;
int len = s.length();
int i = , j; while (s[i]) {
--len;
j = s[i] - '';
if (len & )
sum -= j;
else
sum += j;
++i;
}
return sum;
} string Minus(string a,string b) {
string c="",ans="",t;
int flag=, k=; if(a.length()<b.length()||(a.length()==b.length()&& a.compare(b)<)) {
t=a;
a=b;
b=t;
stop=true;
return "";
}
int i=a.length()-,j=b.length()-;
while(i>=&&j>=) {
if(a[i]+flag>b[j]) { c+=a[i]+flag-b[j]+'';
flag=;
} else if (a[i]+flag==b[j]) {
c+='';
flag=;
} else {
c+=(a[i]-'')+flag+-(b[j]-'')+'';
flag=-;
}
i--;
j--;
k++;
}
while(i>=) {
if(a[i]+flag<'') {
c+=a[i]+flag+;
flag=-;
} else {
c+=a[i]+flag;
flag=;
}
i--,k++;
}
int len=k-;
while(c[len]=='' && len>)
len--;
for(j=;j<=len;j++)
ans+=c[j];
char tt;
for(i=,j=ans.length()-;i<j;i++,j--) {
tt = ans[i];
ans[i] = ans[j];
ans[j]=tt;
}
return ans;
} int main() {
int t, tmp, ttmp;
string l, r, ans;
bool flag;
char x[];
//FILE *fout = fopen("data2", "w"); scanf("%d", &t);
while (t--) {
cin >>l>>r;
flag = stop = false;
tmp = check(r);
ttmp = tmp%;
if (ttmp < )
ttmp += ;
if (ttmp > ) {
x[] = ttmp-+'';
x[] = '\0';
ans = Minus(r, string(x));
} else if (ttmp < ) {
x[] = +ttmp+'';
x[] = '\0';
ans = Minus(r, string(x));
} else {
ans = r;
}
if (stop) {
printf("-1\n");
continue;
}
while (l.length()<ans.length() || (l.length()==ans.length()&&l.compare(ans)<=)) {
if (check(ans) != ) {
flag = true;
break;
}
//cout <<ans<<endl;
ans = Minus(ans, SRCB);
if (stop)
break;
}
if (flag) {
cout <<ans<<endl;
//fprintf(fout, "%s\n", ans.data());
} else {
printf("-1\n");
//fprintf(fout, "-1\n");
}
//fprintf(fout, "%d\n", ans);
}
//fclose(fout);
return ;
}