lightoj(1136 - Division by 3 )规律

本文介绍了一种算法,用于找出特定整数序列中从A到B范围内可以被3整除的整数数量。通过观察序列中每个数字除以3的余数规律,实现了快速计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

There is sequence 1, 12, 123, 1234, ..., 12345678910, ... . Nowyou are given two integers A and B, you have to find the numberof integers from Ath number to Bth (inclusive)number, which are divisible by 3.

For example, let A = 3. B = 5. So, the numbers in thesequence are, 123, 1234, 12345. And 123, 12345 are divisible by 3. So, theresult is 2.

Input

Input starts with an integer T (≤ 10000),denoting the number of test cases.

Each case contains two integers A and B (1≤ A ≤ B < 231) in a line.

Output

For each case, print the case number and the total numbersin the sequence between Ath and Bth whichare divisible by 3.

Sample Input

Output for Sample Input

2

3 5

10 110

Case 1: 2

Case 2: 67

 题意

有序列1,12,123,1234,…,12345678910,…现在你是给定两个整数a和b,你必须从第A个找到第B个整数(含),且能被3整除。

例如,让一个= 3。B = 5。因此,序列中的数字是,123,1234,12345。123、12345能被3整除。所以,结果是2。

思路

找规律:1,0,0,1,0,0,1,0,0,1,0,0,1,0,0......

1是不可整除,0是可整除。

代码

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#define in(a,b) scanf("%d%d",&a,&b)
using namespace std;
int main(){
	int i,j,k,m,n,T,ans;
	scanf("%d",&T);k=T;
	while(T--){
		ans=0;
		in(m,n);
		while(m%3!=1){
			m++;
			ans++;
		}
		while(n%3!=1){
			n--;
			ans++;
		}
		ans=ans+(n-m)/3*2;
			printf("Case %d: %d\n",k-T,ans);
}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值