俩个闹钟响铃

题目描述
有两个闹钟。

第一个闹钟会在 b,b+a,b+2a,b+3a,… 时刻响铃。

第二个闹钟会在 d,d+c,d+2c,d+3c,… 时刻响铃。

请计算两个闹钟第一次同时响铃的具体时刻。输入格式
第一行包含两个整数 a,b。

第二行包含两个整数 c,d。

输出格式
一个整数,表示第一次同时响铃的具体时刻。

如果永远都不可能同时响铃,则输出 −1。

数据范围
所有测试点满足 1≤a,b,c,d≤100。

样例
输入样例1:
20 2
9 19
输出样例1:
82
输入样例2:
2 1
16 12
输出样例2:
-1

思路一:
刚开始想的是哈希表
但是最后一个点TLE了,于是又去想想,发现是个数学问题;
之后敲代码hhh
转码,有的累,需要的私聊

时间复杂度 O(n^2)
参考文献
C++ 代码

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int a,b,c,d;
int main()
{
    cin>>a>>b>>c>>d;
    for (int i = 0; i < 100; i ++ )
    {
        for (int j = 0; j < 100; j ++ )
        {
            if(i*a-j*c==d-b)
            {
                cout<<i*a+b<<'\n';
                return 0;
            }
        }
    }
   cout<<"-1"<<'\n';
    return 0;
}
//这样也行
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    int a, b, c, d;
    cin >> a >> b >> c >> d;

    for (int i = max(b, d); i <= 10100; i ++ )
        if ((i - b) % a == 0 && (i - d) % c == 0)
        {
            cout << i << endl;
            return 0;
        }

    puts("-1");
    return 0;
}

Java代码

import java.util.*;
import java.io.*;
public class Main{
    static  int  a,b,c,d;
    public static void main(String[] args) {
        Scanner cin =new Scanner(System.in);
        a=cin.nextInt();
        b=cin.nextInt();
        c=cin.nextInt();
        d=cin.nextInt();
        for (int i = 0; i < 100; i ++ )
        {
         for (int j = 0; j < 100; j ++ )
         {
            if(i*a-j*c==d-b)
            {
                System.out.println(i*a+b);
                return ;
            }
          }
        }
        System.out.println("-1");
        return ;
    }
}

Python3代码

a, b = map(int, input().split())
c, d = map(int, input().split())
f = False
ans = 10000000000
for i in range(200):
    for j in range(200):
        if b + i * a == d + j * c:
            if ans > d + j * c: ans = d + j * c
if ans == 10000000000 : print("-1")
else : print(ans)

GO 代码

package main

import (
    "bufio"
    . "fmt"
    "os"
)

func main() {
    in := bufio.NewReader(os.Stdin)
    out := bufio.NewWriter(os.Stdout)
    defer out.Flush()
    var a, b, c, d int
    Fscan(in, &a, &b, &c, &d)
    for i := 0; i <= 1000; i++ {
        for j := 0; j <= 1000; j++ {
            if i*a - j*c == d - b {
                Fprintln(out, b+i*a)
                return
            }
        }
    }
    Fprintln(out, -1)
}

欢迎留言点赞

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,以下是我提供的代码: using System; using System.Windows.Forms; namespace AlarmClock { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnSetAlarm_Click(object sender, EventArgs e) { // 获取设定的时间 int hour = (int)numHour.Value; int minute = (int)numMinute.Value; int second = (int)numSecond.Value; // 设置闹钟 AlarmClock.SetAlarm(hour, minute, second); } private void tmrTick_Tick(object sender, EventArgs e) { // 处理闹钟事件 AlarmClock.HandleEvents(DateTime.Now); } } public static class AlarmClock { // 定义闹钟事件 public enum EventType { Tink, // 滴答 Alarm // 响铃 } // 存储闹钟事件 public class AlarmEvent { public DateTime Time { get; set; } public EventType Type { get; set; } public AlarmEvent(DateTime time, EventType type) { Time = time; Type = type; } } // 存储所有闹钟事件 private static List<AlarmEvent> events = new List<AlarmEvent>(); // 添加一个闹钟事件 public static void SetAlarm(int hour, int minute, int second) { DateTime now = DateTime.Now; DateTime time = new DateTime(now.Year, now.Month, now.Day, hour, minute, second); if (time < now) { time = time.AddDays(1); } events.Add(new AlarmEvent(time, EventType.Alarm)); } // 处理所有闹钟事件 public static void HandleEvents(DateTime now) { foreach (AlarmEvent evt in events) { if (now >= evt.Time) { if (evt.Type == EventType.Tink) { MessageBox.Show("Tink"); } else if (evt.Type == EventType.Alarm) { MessageBox.Show("Alarm"); } events.Remove(evt); break; } } } } } 请注意,这只是基本的模拟实现,可能存在一些问题。如果需要更加完整的闹钟功能,可以参考其他闹钟应用程序的实现方式,并进行相应的修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值