攻克lightoj基础题系列
给你两个数(在本题中5代表的数是12345为1到五的等差数列,其他数也如此),求两个数之间三的倍数的数的个数,明显的暴力超时,但规律易得,每三个数内都有两个数为三的倍数,把情况讨论一下就可以了。
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int t;
scanf("%d", &t);
for (int k = 1; k <= t; ++k)
{
int a, b;
scanf("%d%d", &a, &b);
int ans = 0;
if (a % 3 == 0)
{
ans++;
}
else if (a % 3 == 1)
{
ans += 2;
a += 2;
}
else
{
ans += 2;
a++;
}
if (b % 3 == 0)
{
}
else if (b % 3 == 1)
{
b--;
}
else
{
ans++;
b -= 2;
}
ans +=(b - a) / 3 * 2;
printf("Case %d: %d\n", k, ans);
}
return 0;
}