It means that an item has a price, which can be reduced at most by d. If the price of the item is not more than d, the maximum price is 9 and the maximum price is reached. for example,
1029 102
The original price is 1029, which can be reduced by 102 yuan at most. Within the range that meets the conditions, the maximum 9 can be obtained and the maximum price is 999. If no conditions are met, the original price will be output.
Idea: First, find the number of digits of the original price, then calculate the maximum number of 9, compare with the original price, and determine whether the conditions are met. After that, it is a simulated process, which is added at until a loan meeting the conditions is found. Some details need to be noted in the middle, and the intermediate operation may exceed _ int64, all of which need to be processed.
Code:
[Cpp]
# Include <iostream>
# Include <string. h>
# Include <cstdio>
Using namespace std;
Typedef unsigned _ int64 LL;
Int fun (LL x ){
Int sum = 0;
While (x ){
Sum ++;
X/= 10;
}
Return sum;
}
LL cal (int x ){
LL sum = 0;
For (int I = 1; I <= x; ++ I ){
Sum = sum * 10 + 9;
}
Return sum;
}
LL mi (int cnt ){
LL s = 1;
For (int I = 1; I <= cnt; ++ I)
S * = 10;
Return s;
}
Int main (){
LL p, d;
While (scanf ("% I64d % I64d", & p, & d )! = EOF ){
LL value = p-d;
Int cnt = fun (p );
LL ans = cal (cnt );
If (ans> = value & ans <= p ){
Printf ("% I64d \ n", ans );
}
Else {
LL x = value % 10;
LL y = value/10;
LL z = y * 10 + 9;
If (z> p) {www.2cto.com
Z = z-9;
Printf ("% I64d \ n", p );
Continue;
}
LL num = 0, xx = 0, yy = 0;
LL cnt = 1;
While (z <= p ){
Cnt ++;
Xx = z/mi (cnt );
Yy = z % mi (cnt );
Z = xx * mi (cnt) + cal (cnt );
Num = z;
}
Num = xx * mi (cnt) + yy;
Cnt --;
While (num <= p ){
Num + = mi (cnt );
}
Printf ("% I64d \ n", num-mi (cnt ));
}
}
Return 0;
}