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();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值