首页 > 试题广场 >

被3整除

[编程题]被3整除
  • 热度指数:467 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

小Q得到一个神奇的数列: 1, 12, 123,...12345678910,1234567891011...。

并且小Q对于能否被3整除这个性质很感兴趣。

小Q现在希望你能帮他计算一下从数列的第l个到第r个(包含端点)有多少个数可以被3整除。


输入描述:
输入包括两个整数l和r(1 <= l <= r <= 1e9), 表示要求解的区间两端。


输出描述:
输出一个整数, 表示区间内能被3整除的数字个数。
示例1

输入

2 5

输出

3

说明

12, 123, 1234, 12345...
其中12, 123, 12345能被3整除。
根据序列找规律发现是否能被3整除的结果为:不能,能,能,不能,能,能。。。
可知,项的索引 % 3的余数为0和2时即可被整除
#include<iostream>
using namespace std;

int main()
{
	int left = 0, right = 0, count = 0;
	cin >> left >> right;
	
    for(int i = left; i <= right; i++)
    {
        if(i % 3 == 0)
            count++;
        else if(i % 3 == 2)
            count ++;
    }
    
	cout << count << endl;
	return 0;
}


发表于 2020-02-13 12:46:02 回复(0)
3的倍数有个特点就是各个位数相加的和仍为3的倍数
#include<stdio.h>
#include<iostream>
 using namespace std;
int main(){
    long start,end;
    long count=0;
    cin>>start>>end;
    for(long i=start;i<=end;i++){
        if((i*(i+1)/2)%3==0){
            count++;
        }
    }
    cout<<count;
    return 0;
}
发表于 2019-09-06 17:03:34 回复(0)
#include <iostream>
using namespace std;
 
int main()
{
    int l,r;
    int count = 0;
    while (cin >> l>> r) {
        count = (r / 3 - l / 3) * 2;
        if (l % 3 == 0) {
            count++;
        }
        if (r % 3 == 2) {
            count++;
        }
        cout << count;
    }
    return 0;
}


编辑于 2019-09-27 17:07:00 回复(0)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            string data = Console.ReadLine();
            string[] datas = data.Split(' ');
            int leftL = int.Parse(datas[0]);
            int rightL = int.Parse(datas[1]);
          
            List<double> listN = new List<double>();
            for (int i = leftL; i <= rightL; i++)
            {
                string str = "";
                
                for (int j = 1; j <=i; j++)
                {
                    str += j.ToString();
                }
                listN.Add(double.Parse(str));
            }
            int n = 0;
            for (int i = 0; i < listN.Count; i++)
            {
                if (listN[i] % 3 == 0)
                    n++;
            }
            Console.WriteLine(n);
            Console.ReadLine();
        }
    }
}
long都用上了  过的用例只有10%...
发表于 2019-05-29 17:05:13 回复(0)
//为什么这个通不过?
using System;
class Program
{
    static void Main(string[] args)
    {
        int begin = int.Parse(Console.ReadLine());
        int end = int.Parse(Console.ReadLine());
        int count = 0;
        for(int i = begin; i<=end; i++){
            int sum = 0;
            for(int j=1; j<=i; j++){
                sum +=j;
            }
            if(sum!=0&&sum%3==0){
                count++;
            }
        }
        Console.WriteLine(count);
    }
}
编辑于 2019-03-15 17:54:27 回复(0)