首页 > 试题广场 >

行星观测

[编程题]行星观测
  • 热度指数:3313 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解

科学家正在计划利用 行星上的一个研究模块进行一项重要的测量实验,测量共分为两次进行。

因为宇宙中有多种不确定因素,科学家们已经确定了最佳测量的时间在 l 到 r 范围内。

测量的要求是两次测量的间隔时间必须是 a 的倍数,现在请你帮助科学家计算测量方式的数量。

即有多少对测量时间整数 i 和 j 满足 l <= i < j <= r ,并且 j-i 是 a 的倍数。

进阶:空间复杂度 ,时间复杂度

输入描述:
输入共三行,从上到下三个数字分别代表 


输出描述:
在一行中输出测量方式的数量
示例1

输入

1
5
2

输出

4

说明

(1,3)、(1,5)、(2,4)和(3,5)共4个    
示例2

输入

4
9
6

输出

0

说明

一个能满足的都没有    
#include<iostream>
using namespace std;
int main(){
    long r,l,a;
    cin>>l>>r>>a;
    long s;
    s=(r-l)/a;
    long m,n;
    m=r-l-s*a+1;
    n=r-l-a+1;
    long count;
    count=(m+n)*s/2;
    cout<<count;
    return 0;
    
    
}

发表于 2021-11-16 11:12:20 回复(0)

先看code:

这题就是个纯粹的计算题,下面分析解题思路。

首先,记监测区间的长度为dist

那么在这个dist区间中,最多可以容纳 dist / aa,即j - i 的最大值是 dist - a,记为k

如上图所示的例子,k = 4.

那么当j - i = a时,很显然,可以有 dist - a + 1{i, j}

同理,当 j - i = 2a时,可以有 dist - 2a + 1{i, j}

所以,一共可能的{i, j}的对数为:

发表于 2021-09-18 11:14:38 回复(3)
l = int(input())
r = int(input())
a = int(input())

n = (r-l)//a
s = a*n*(n-1)//2+n*(r-l-n*a+1)
print(s)


发表于 2021-09-21 22:10:26 回复(0)
其实就是计算步长为1时,能够滑出多少个长度为i*a的窗口?其中
同样使用数学方法来做,但没能达成时间复杂度O(1)的成就。根据卷积过程可以知道,对于长度为n的区间,如果步长为1,窗口长度为a进行滑窗,可以产生n-a+1个窗口。因此只需要尝试所有可能的窗口大小就可以统计出窗口总数。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int l = Integer.parseInt(br.readLine());
        int r = Integer.parseInt(br.readLine());
        int a = Integer.parseInt(br.readLine());
        int n = r - l;
        long count = 0;
        for(int i = 1; i <= n / a; i++){
            count += n - i*a + 1;      // 窗口大小为i*a时,能够滑出的窗口数
        }
        System.out.println(count);
    }
}
后来发现评论区大佬们把这个求和过程写成一个统一的公式了,妙哉妙哉!
发表于 2022-01-16 22:50:11 回复(0)
l=int(input())
r=int(input())
a=int(input())
counts=0
for i in range(l,r+1):
    count_=(r-i)//a
    counts=counts+count_
print(counts)
发表于 2023-11-23 21:57:43 回复(0)
这样·数据小的可以,大数循环就不行
Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int temp;
        int count = 0;
        for (int i = a; i < b ;i++) {
            for (int j = b;j > i;j--){
                if (j==i) continue;
                temp = (j-i)%c;
                if (temp == 0){
                    count++;
                }
            }
        }
        System.out.println(count);


发表于 2023-05-10 11:08:25 回复(0)
间隔一个a,是(r-l)/a(向下取整) 对,间隔2个a是(r-l)/a向下取整 -1,3个a 是(r-l)/a向下取整 -2,一直到最大的间隔只有一对,因此是从1加到int((r-l)/a)对。不知道我说的对不对
l=int(input())
r=int(input())
a=int(input())
d=int((r-l)/a)
s=0
for i in range(1,d):
    s+=i
print(s)
发表于 2022-07-13 21:38:59 回复(1)
#include <vector>
#include <iostream>

long result = 0;

void calcuate(long l, long r, long a, long multiplyNum){
    if(r - l >= a * multiplyNum) {
        result += (r - l - a* multiplyNum) + 1;
    }
}

int main(){
    long l;
    std::cin >> l;
    long r;
    std::cin >> r;
    long a;
    std::cin >> a;
    long multiply = 1;
    while(a * multiply <= (r - l)){
        calcuate(l ,r ,a, multiply);
        multiply++;
    }
    std::cout << result << std::endl;
    return 0;
}

发表于 2022-03-23 10:33:09 回复(0)
import java.util.*;

public class Main{
    
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int l = scanner.nextInt();
        scanner.nextLine();
        int r = scanner.nextInt();
        scanner.nextLine();
        int a = scanner.nextInt();
        scanner.nextLine();
        long count = 0;
        int wuqg = 0;
        int wu = 0;
        //特殊值判断,一个也没有
        if(l+a>r){
            System.out.print(0);
            return;
        }
        //从[l,r]中找出第一个可以整除a的数wu
        for(wu = l;wu<=r;wu++){
            if((r-wu)%a==0){
                break;
            }
        }
        int c1 = (wu-l+1);
        long c2 = (r-wu)/a;//注意,中间结果可能会溢出,使用长整型long
        count += c1*c2;//从[l,wu]之间的实验测量个数
        //遍历,从[wu,r]中找出实验测量个数
        for(int i=wu+1;i<=r-a;i+=a){
            int temp = (r-i)/a;
            count += (long)(temp*a);
        }
        System.out.print(count);
    }
}
发表于 2021-11-04 15:34:09 回复(0)
l = int(input())
r = int(input())
min_p = int(input())
num_a = (r-l)// min_p
this_p = 0
this_res = 0
result = 0
if r-l < min_p:
    print(result)
else:
    for i in range(1, num_a+1):
        this_p = i*min_p
        this_res = r-l-this_p+1
        result += this_res
    print(result)
发表于 2021-10-08 20:37:29 回复(0)
l = int(input())
r = int(input())
a = int(input())
long = r-l
if long >= a:
    num = long // a
    temp = long % a + 1
    m = long - a + 1
    count = num*(temp+m)//2
    print(count)
else:
    print(0)
发表于 2021-09-21 15:11:29 回复(0)
import java.io.*;

public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        long l = Integer.parseInt(bf.readLine());
        long r = Integer.parseInt(bf.readLine());
        long a = Integer.parseInt(bf.readLine());
        long count = 0;
                
        long x = (r - l) / a;
        long y = (r - l) % a;
        count = x*(1 + y);
                //其余的等差数列求和
        if(x > 1){
            long n = x - 1;
            count += a*(n + 1)*n/2;
        }

        System.out.println(count);
    }
}

发表于 2021-09-20 13:06:14 回复(2)
OC 答案 输入 l,r,a,项目中可以运行,在这里运行报错
-(NSInteger)start:(int)l end:(NSInteger)r a:(NSInteger)a{
    NSInteger count = 0;
    for (int i = l; i<r+1; i++) {
        for (int j = i+1; j<r+1; j++) {
            if ((j-i)%a == 0) {
                count += 1;
            }
        }
    }
    return  count;
}


}

发表于 2021-09-17 10:56:07 回复(0)
// 通过了,不能使用for和int,解析写在题解了
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        Long begin = scanner.nextLong();
        Long end = scanner.nextLong();
        Long interval = scanner.nextLong();
        end = end - begin;
        begin = 0L;
        Long count = end/interval;
        Long sumStart = end - count*interval + 1;
        Long sumStop = end - interval + 1;
        // 等差数列前n项和
        Long result = (sumStart+sumStop)*count/2;
        System.out.println(result);
    }
}

编辑于 2021-09-16 10:36:55 回复(0)
我在用JS的时候发现总有预期很大时,有三组数不满足,比如249522833405508630,我的输出是249522833405508540。网上说JS number类型超过2的53次方9007199254740992,js会出现计算不精确的问题。不知道有没有大佬用JS解决一下这个问题
发表于 2021-09-11 19:12:04 回复(2)
题目描述***,间隔应该是 两者间有几个数 而不是 j-i=a
发表于 2021-09-11 10:34:57 回复(0)
import java.io.*;
public class Main{
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        int l = Integer.parseInt(reader.readLine());
        int r = Integer.parseInt(reader.readLine());
        int a = Integer.parseInt(reader.readLine());
        if ((r - l) < a) {
            writer.write(Integer.toString(0));
        } else {
            int res = 0;
            int temp = a;
            while ((r - l) >= a) {
                res = res + (r - a - l + 1);
                a = a + temp;
            }
            writer.write(Integer.toString(res));
        }
        writer.flush();
    }
}

注意使用long防止答案溢出
发表于 2021-09-10 23:33:33 回复(0)
其实是计算题,遍历哪怕一次也会超时
l = int(input())
r = int(input())
a = int(input())
 
d = int((r-l)/a)
g = (r-l)%a
c = d*(g+1)+d*(d-1)*a//2
print(c)

一开始用的
c = d*(g+1)+d*(d-1)*a/2
c = int(c)
会有3组数值很大的例子不对,这是//和先/再int的差异,还没注意到这个
发表于 2021-09-09 16:44:15 回复(2)
使用for循环遍历太浪费时间了,后面的测试用例不通过!蓝瘦
编辑于 2021-09-07 22:41:54 回复(0)