Codeforces Round #617 (Div. 3)
传送门:Codeforce—1296—B
测试样例:
input
6
1
10
19
9876
12345
1000000000
output
1
11
21
10973
13716
1111111111
题目大意:Mishka有s(整数)元,每当他花x(整数)元买东西时,会获得⌊x/10⌋(向下取整)的回报。问Mishka以最优的方式买东西最多可以花多少钱。T个测试样例,每个测试样例给定一个整数s。
思路:我们每花费x元可获得⌊x/10⌋(向下取整)的回报,简单来说,每当我们花费10元就可获得1元的回报。
int ans=0;
while(n>=10)
{
n=n-10+1;
ans+=10;
}
ans+=n;
但是做减法对于题目给定的数量级会超时,所以将减法转化为除法。(实测test 3时TLE)
AC代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int ans=0;
while(n>=10)
{
ans+=n/10*10; //有多少个整10
n=n-n/10*10+n/10;
}
ans+=n;
printf("%d\n",ans);
}
return 0;
}
补一波朋友的代码,思路很好
AC代码:
By vitoy, contest: Codeforces Round #617 (Div. 3), problem: (B) Food Buying, Accepted
#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
if(n%9) cout<<n+n/9<<endl;
else cout<<n+n/9-1<<endl;
}
return 0;
}