CF 135 DIV2 B Special Offer! Super Price 999 Bourles!

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.