本文主要是介绍Codeforces Round #725 (Div. 3)-F. Interesting Function,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目链接
You are given two integers l and r, where l<r. We will add 1 to l until the result is equal to r. Thus, there will be exactly r−l additions performed. For each such addition, let’s look at the number of digits that will be changed after it.
For example:
if l=909, then adding one will result in 910 and 2 digits will be changed;
if you add one to l=9, the result will be 10 and 2 digits will also be changed;
if you add one to l=489999, the result will be 490000 and 5 digits will be changed.
Changed digits always form a suffix of the result written in the decimal system.
Output the total number of changed digits, if you want to get r from l, adding 1 each time.
Input
The first line contains an integer t (1≤t≤104). Then t test cases follow.
Each test case is characterized by two integers l and r (1≤l<r≤109).
Output
For each test case, calculate the total number of changed digits if you want to get r from l, adding one each time.
Example
input
4
1 9
9 10
10 20
1 1000000000
output
8
2
11
1111111110
题意:给定两个数L和R,问从L逐渐加1一直到R需要变动多少个位数,例如8到9需要变动一位,9到10则是变动两位
思路:只需要求从0到L变动多少位和0到R变动多少位,然后求差即可
#include<bits/stdc++.h>
using namespace std;
void slove()
{int l,r;cin>>l>>r;int f=r-l;long long ans1=0;long long ans2=0;long long a1=1;while(l){int g=l%10;ans1+=g*a1;a1=a1*10+1;l/=10;}long long a2=1;while(r){int g=r%10;ans2+=g*a2;a2=(a2*10+1);r/=10;}cout<<ans2-ans1<<endl;}
int main()
{int t;cin>>t;while(t--){slove();}
}
这篇关于Codeforces Round #725 (Div. 3)-F. Interesting Function的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!