您的位置:首页 > 其它

Coderforces 204A

2012-08-10 10:58 141 查看
此题比赛时没有好的方法不会做,最后从别人那里学到了一个好的方法,就是从小到大慢慢数,从11到99开始数数,直到l < 100...1 < r < 9000..9

当加到这一步时,输出count就是所求结果,比如 47 1024 首先是两位数的 11 到99 ,47在里面 ,此时数目是5,再到三位数,101 到 999,此时1024大于他们

数目count直接加上9*10(101 到191共10个),接着是四位数,1001 到 9999 ,1024在里面,此事要计算1001到1024首尾相同的数的个数,总共有3个,所以

总数就是98个。

View Code

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>

using namespace std;

int main()
{
int i, j;
long long a, b;
long long x, l, r, count, num;

count = 0LL;
x = 10;
cin>>l>>r;

for(i=1; i<=9; i++)
{
if(l<=i && i<=r)
count++;
}
for(int len=2; len<=18; len++)//长度从小到大递增
{
for(int d=1; d<=9; d++)//每个长度的数,都要计算count个数
{
a = d*x + d;
b = (d + 1)*x - 10 + d;
long long ln = max(a,l);
long long rn = min(b,r);

if(ln <= rn)
{
long long l0 = ln/10;
if(ln%10 > d)//比如上面的r == 47时
l0++;
long long r0 = rn/10;
if(rn%10 < d)
r0--;

count += r0 - l0 + 1;
}
}
x = x*10;
}

printf("%I64d\n",count);
//system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: