思路
每次从 到 枚举,每次将数字的最后一位移到前面,判断是否满足条件,如果满足将数对数量加一,不断重复直到发现与前面数字相同,然后换下一个数。
代码
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
int count = 1; //记录测试用例编号。
while (t--)
{
int a, b;
cin >> a >> b;
int cnt = 1, a1 = a;
while (a1 >= 10) //计算数位。
{
cnt *= 10;
a1 /= 10;
}
int ans = 0;
for (int n = a; n <= b; n++) //枚举从a到b的所有数字。
{
int tmp = n;
while (1)
{
tmp = (tmp / 10) + ((tmp % 10) * cnt); //每次将数字最末尾的一个数字,移到最前面。
if (tmp == n) //如果重复就退出循环,换下一个数。
{
break;
}
if (tmp > n && tmp <= b) //如果移动后的数字满足条件。
{
ans++; //数对数量加一。
}
}
}
cout << "Case #" << count++ << ": " << ans << endl; //输出格式要与题目保持一致。
}
return 0;
}update:修改了错误的代码。