首页 > 其他分享 >2023-10-21:用go语言,一共有三个服务A、B、C,网络延时分别为a、b、c 并且一定有:1 <= a <= b <= c <= 10^9 但是具体的延时数字丢失了,只有单次调用的时间 一次调

2023-10-21:用go语言,一共有三个服务A、B、C,网络延时分别为a、b、c 并且一定有:1 <= a <= b <= c <= 10^9 但是具体的延时数字丢失了,只有单次调用的时间 一次调

时间:2023-10-21 22:05:02浏览次数:31  
标签:status 10 网络延时 return int else && go times

2023-10-21:用go语言,一共有三个服务A、B、C,网络延时分别为a、b、c

并且一定有:1 <= a <= b <= c <= 10^9

但是具体的延时数字丢失了,只有单次调用的时间

一次调用不可能重复使用相同的服务,

一次调用可能使用了三个服务中的某1个、某2个或者全部3个服务

比如一个调用的时间,T = 100

100的延时可能来自以下7种情况:

a = 100,这次调用可能单独使用了A

b = 100,这次调用可能单独使用了B

c = 100,这次调用可能单独使用了C

a + b = 100,这次调用可能组合使用了A、B

a + c = 100,这次调用可能组合使用了A、C

b + c = 100,这次调用可能组合使用了B、C

a + b + c = 100,这次调用可能组合使用了A、B、C全部服务

那么可想而知,如果给的调用时间足够多,是可以猜测出a、b、c的

给定一个数组times,长度为n,并且一定有4 <= n <= 7

times[i] = s,表示i号调用用时s,而且times中一定都是正数且没有重复值。

请根据n次调用,猜测出a、b、c三元组可能的情况数。

如果任何a、b、c都无法匹配上给定的调用耗时,返回0,

测试的次数T <= 100,

也就是说,一共最多给定100个数组,每一次让你返回a、b、c三元组可能的情况数。

来自招商银行。

来自左程云

答案2023-10-21:

为什么用讯飞星火?

这次代码生成用的讯飞星火,生成完后,要略微修改下代码才能通过。另外c代码的生成,一直有问题,索性就不管了。

之前一直用的chatgpt,但那个地址不能用了,所以用讯飞星火试水。

文心一言以及其他产品,我也试了下,java代码太多,文心一言无法转换成其他代码,都有各自的问题,所以只能放弃。

大体过程如下:

1.首先,定义一个函数ways,接受一个整数数组times作为参数,并返回可能的情况数。

2.在ways函数中,创建一个长度为7的整数数组status,用于记录服务的延时情况。初始化为全0。

3.创建一个空的mapans,用于存储可能的三元组情况。

4.调用process函数,传入times、0、statusans作为参数。

5.返回ans的长度,即为可能的情况数。

6.在process函数中,判断是否已经遍历完了times数组,如果是,则进行下面的操作:

6.1.创建三个变量abc,用于存储可能的延时情况。

6.2.调用counts函数,获取当前status数组中非零元素的个数,存储在变量count中。

6.3.根据count的值,进行不同的情况判断:

6.3.1.如果count为0,说明三个服务都没有使用过,根据给定的关系式计算出abc的值。

6.3.2.如果count为1,说明只有一个服务被使用过,根据给定的关系式计算出abc的值。

6.3.3.如果count为2,说明有两个服务被使用过,根据给定的关系式计算出abc的值。

6.3.4.如果count为3,说明三个服务都被使用过,直接将status数组中的值赋给abc

6.4.调用verify函数,判断当前的abc是否满足条件,如果满足,则将其作为键存入ans中。

7.如果没有遍历完times数组,则进行递归操作:

7.1.遍历status数组,找到第一个为0的位置。

7.2.将当前遍历到的times元素赋值给status数组的该位置。

7.3.递归调用process函数,传入更新后的status数组,i+1ans作为参数。

7.4.将status数组的该位置重新置为0,进行下一次遍历。

8.在counts函数中,遍历status数组,统计非零元素的个数,并返回该个数。

9.在verify函数中,根据给定的条件,判断abc是否满足条件,如果满足则返回true,否则返回false。

go完整代码如下:

package main

import "fmt"

func ways(times []int) int {
	status := []int{0, 0, 0, 0, 0, 0, 0}
	ans := make(map[string]struct{})
	process(times, 0, status, ans)
	return len(ans)
}

func process(times []int, i int, status []int, ans map[string]struct{}) {
	if i == len(times) {
		a, b, c := 0, 0, 0
		count := counts(status)
		if count == 0 {
			a = (status[3] + status[4] - status[5]) / 2
			b = (status[3] + status[5] - status[4]) / 2
			c = (status[4] + status[5] - status[3]) / 2
		} else if count == 1 {
			if status[0] != 0 {
				a = status[0]
				if status[3] != 0 {
					b = status[3] - a
				}
				if status[4] != 0 {
					c = status[4] - a
				}
				if status[5] != 0 {
					if b != 0 && c == 0 {
						c = status[5] - b
					}
					if c != 0 && b == 0 {
						b = status[5] - c
					}
				}
			} else if status[1] != 0 {
				b = status[1]
				if status[3] != 0 {
					a = status[3] - b
				}
				if status[5] != 0 {
					c = status[5] - b
				}
				if status[4] != 0 {
					if a != 0 && c == 0 {
						c = status[4] - a
					}
					if c != 0 && a == 0 {
						a = status[4] - c
					}
				}
			} else {
				c = status[2]
				if status[4] != 0 {
					a = status[4] - c
				}
				if status[5] != 0 {
					b = status[5] - c
				}
				if status[3] != 0 {
					if a != 0 && b == 0 {
						b = status[3] - a
					}
					if b != 0 && a == 0 {
						a = status[3] - b
					}
				}
			}
		} else if count == 2 {
			if status[0] != 0 {
				a = status[0]
			}
			if status[1] != 0 {
				b = status[1]
			}
			if status[2] != 0 {
				c = status[2]
			}
			if a == 0 {
				if status[3] != 0 {
					a = status[3] - b
				} else if status[4] != 0 {
					a = status[4] - c
				} else {
					a = status[6] - b - c
				}
			} else if b == 0 {
				if status[3] != 0 {
					b = status[3] - a
				} else if status[5] != 0 {
					b = status[5] - c
				} else {
					b = status[6] - a - c
				}
			} else {
				if status[4] != 0 {
					c = status[4] - a
				} else if status[5] != 0 {
					c = status[5] - b
				} else {
					c = status[6] - a - b
				}
			}
		} else {
			a = status[0]
			b = status[1]
			c = status[2]
		}
		if verify(status, a, b, c) {
			ans[fmt.Sprint(a)+"_"+fmt.Sprint(b)+"_"+fmt.Sprint(c)] = struct{}{}
		}
	} else {
		for j := 0; j < 7; j++ {
			if status[j] == 0 {
				status[j] = times[i]
				process(times, i+1, status, ans)
				status[j] = 0
			}
		}
	}
}

func counts(status []int) int {
	count := 0
	for i := 0; i < 3; i++ {
		if status[i] != 0 {
			count++
		}
	}
	return count
}

func verify(status []int, a, b, c int) bool {
	if a <= 0 || b <= 0 || c <= 0 || a > b || b > c {
		return false
	}
	if status[0] != 0 && status[0] != a {
		return false
	}
	if status[1] != 0 && status[1] != b {
		return false
	}
	if status[2] != 0 && status[2] != c {
		return false
	}
	if status[3] != 0 && status[3] != a+b {
		return false
	}
	if status[4] != 0 && status[4] != a+c {
		return false
	}
	if status[5] != 0 && status[5] != b+c {
		return false
	}
	if status[6] != 0 && status[6] != a+b+c {
		return false
	}
	return true
}

func main() {
	times := []int{1, 2, 3, 4, 5, 6, 7}
	fmt.Println(ways(times))
}

2023-10-21:用go语言,一共有三个服务A、B、C,网络延时分别为a、b、c 并且一定有:1 <= a <= b <= c <= 10^9 但是具体的延时数字丢失了,只有单次调用的时间 一次调_三元组

rust完整代码如下:

use std::collections::HashSet;

fn main() {
    let status = [1, 2, 3, 4, 5, 6, 7];
    let ans = ways(&status);
    println!("Hello, World {}", ans);
}

fn ways(times: &[i32]) -> usize {
    let mut status = [0; 7];
    let mut ans = std::collections::HashSet::new();
    process(times, 0, &mut status, &mut ans);
    ans.len()
}

fn process(times: &[i32], i: usize, status: &mut [i32], ans: &mut HashSet<String>) {
    if i == times.len() {
        let a = if status[0] != 0 {
            status[0]
        } else {
            (status[3] + status[4] - status[5]) / 2
        };
        let b = if status[1] != 0 {
            status[1]
        } else {
            (status[3] + status[5] - status[4]) / 2
        };
        let c = if status[2] != 0 {
            status[2]
        } else {
            (status[4] + status[5] - status[3]) / 2
        };
        if verify(status, a, b, c) {
            let a_str = a.to_string();
            let b_str = b.to_string();
            let c_str = c.to_string();
            ans.insert(format!("{}-{}-{}", a_str, b_str, c_str));
        }
    } else {
        for j in 0..7 {
            if status[j] == 0 {
                status[j] = times[i];
                process(times, i + 1, status, ans);
                status[j] = 0;
            }
        }
    }
}

fn verify(s: &[i32], a: i32, b: i32, c: i32) -> bool {
    if a <= 0 || b <= 0 || c <= 0 || a > b || b > c {
        return false;
    }
    if s[0] != 0 && s[0] != a {
        return false;
    }
    if s[1] != 0 && s[1] != b {
        return false;
    }
    if s[2] != 0 && s[2] != c {
        return false;
    }
    if s[3] != 0 && s[3] != a + b {
        return false;
    }
    if s[4] != 0 && s[4] != a + c {
        return false;
    }
    if s[5] != 0 && s[5] != b + c {
        return false;
    }
    if s[6] != 0 && s[6] != a + b + c {
        return false;
    }
    true
}

2023-10-21:用go语言,一共有三个服务A、B、C,网络延时分别为a、b、c 并且一定有:1 <= a <= b <= c <= 10^9 但是具体的延时数字丢失了,只有单次调用的时间 一次调_三元组_02

c++完整代码如下:

#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>

using namespace std;

bool verify(std::vector<int> s, int a, int b, int c);

int counts(vector<int> status);

void process(vector<int> times, int i, vector<int>& status, unordered_set<string>& ans);

int ways(vector<int> times) {
    vector<int> status(7);
    unordered_set<string> ans;
    process(times, 0, status, ans);
    return ans.size();
}

void process(vector<int> times, int i, vector<int>& status, unordered_set<string>& ans) {
    if (i == times.size()) {
        int a = 0;
        int b = 0;
        int c = 0;
        int cnt = counts(status);
        if (cnt == 0) {
            a = (status[3] + status[4] - status[5]) / 2;
            b = (status[3] + status[5] - status[4]) / 2;
            c = (status[4] + status[5] - status[3]) / 2;
        }
        else if (cnt == 1) {
            if (status[0] != 0) {
                a = status[0];
                if (status[3] != 0) {
                    b = status[3] - a;
                }
                if (status[4] != 0) {
                    c = status[4] - a;
                }
                if (status[5] != 0) {
                    if (b != 0 && c == 0) {
                        c = status[5] - b;
                    }
                    if (c != 0 && b == 0) {
                        b = status[5] - c;
                    }
                }
            }
            else if (status[1] != 0) {
                b = status[1];
                if (status[3] != 0) {
                    a = status[3] - b;
                }
                if (status[5] != 0) {
                    c = status[5] - b;
                }
                if (status[4] != 0) {
                    if (a != 0 && c == 0) {
                        c = status[4] - a;
                    }
                    if (c != 0 && a == 0) {
                        a = status[4] - c;
                    }
                }
            }
            else {
                c = status[2];
                if (status[4] != 0) {
                    a = status[4] - c;
                }
                if (status[5] != 0) {
                    b = status[5] - c;
                }
                if (status[3] != 0) {
                    if (a != 0 && b == 0) {
                        b = status[3] - a;
                    }
                    if (b != 0 && a == 0) {
                        a = status[3] - b;
                    }
                }
            }
        }
        else if (cnt == 2) {
            if (status[0] != 0) {
                a = status[0];
            }
            if (status[1] != 0) {
                b = status[1];
            }
            if (status[2] != 0) {
                c = status[2];
            }
            if (a == 0) {
                if (status[3] != 0) {
                    a = status[3] - b;
                }
                else if (status[4] != 0) {
                    a = status[4] - c;
                }
                else {
                    a = status[6] - b - c;
                }
            }
            else if (b == 0) {
                if (status[3] != 0) {
                    b = status[3] - a;
                }
                else if (status[5] != 0) {
                    b = status[5] - c;
                }
                else {
                    b = status[6] - a - c;
                }
            }
            else {
                if (status[4] != 0) {
                    c = status[4] - a;
                }
                else if (status[5] != 0) {
                    c = status[5] - b;
                }
                else {
                    c = status[6] - a - b;
                }
            }
            if (verify(status, a, b, c)) {
                ans.insert(to_string(a) + "_" + to_string(b) + "_" + to_string(c));
            }
        }
        else {
            a = status[0];
            b = status[1];
            c = status[2];
        }
        if (verify(status, a, b, c)) {
            ans.insert(to_string(a) + "_" + to_string(b) + "_" + to_string(c));
        }
    }
    else {
        for (int j = 0; j < 7; j++) {
            if (status[j] == 0) {
                status[j] = times[i];
                process(times, i + 1, status, ans);
                status[j] = 0;
            }
        }
    }
}

int counts(vector<int> status) {
    int cnt = 0;
    for (int i = 0; i < 3; i++) {
        if (status[i] != 0) {
            cnt++;
        }
    }
    return cnt;
}

bool verify(std::vector<int> s, int a, int b, int c) {
    if (a <= 0 || b <= 0 || c <= 0 || a > b || b > c) {
        return false;
    }
    if (s[0] != 0 && s[0] != a) {
        return false;
    }
    if (s[1] != 0 && s[1] != b) {
        return false;
    }
    if (s[2] != 0 && s[2] != c) {
        return false;
    }
    if (s[3] != 0 && s[3] != a + b) {
        return false;
    }
    if (s[4] != 0 && s[4] != a + c) {
        return false;
    }
    if (s[5] != 0 && s[5] != b + c) {
        return false;
    }
    if (s[6] != 0 && s[6] != a + b + c) {
        return false;
    }
    return true;
}

int main() {
    std::vector<int> status = { 1, 2, 3, 4, 5, 6, 7 };
    int ans = ways(status);
    std::cout << "Hello, World " << ans << std::endl;
    return 0;
}

2023-10-21:用go语言,一共有三个服务A、B、C,网络延时分别为a、b、c 并且一定有:1 <= a <= b <= c <= 10^9 但是具体的延时数字丢失了,只有单次调用的时间 一次调_#include_03

标签:status,10,网络延时,return,int,else,&&,go,times
From: https://blog.51cto.com/moonfdd/7969659

相关文章

  • 阶段总结 10.21
    CSP总结考完了CSP。先做个结论:本场考试难度约等于考试前的质量较低的几场lxsround。我们机房应该不少于\(5\)个人有AK的能力。但是是什么阻止了我达到甚至接近这一目标呢?在这场考试中,暴露出了以下问题:某些方面,例如dp,存在较严重欠缺。似乎没有在这次考试显然地体现......
  • 2023-10-21 闲话
    同学你不觉得你的机械键盘有点吵吗?你是不是你的学习比我们几个的学习更重要?给我吓怕了,本来想喷来着。还准备了精彩的“你是不是觉得你们几个的学习比我的更重要?”。本来就是个等号,为什么要强行找一个大小于,又不是均值不等式。......
  • 2023.10.21——每日总结
    学习所花时间(包括上课):9h代码量(行):0行博客量(篇):1篇今天,上午学习,下午学习;我了解到的知识点:1.休息明日计划:学习......
  • 2023-10-21:用go语言,一共有三个服务A、B、C,网络延时分别为a、b、c 并且一定有:1 <= a <= b
    2023-10-21:用go语言,一共有三个服务A、B、C,网络延时分别为a、b、c并且一定有:1<=a<=b<=c<=10^9但是具体的延时数字丢失了,只有单次调用的时间一次调用不可能重复使用相同的服务,一次调用可能使用了三个服务中的某1个、某2个或者全部3个服务比如一个调用的时间,T=100100......
  • 呜呜呜我要拿Go赢他~ Go language MacOs build development environment Hello! Go !
    前言Go编程语言是一个开源项目,旨在提高程序员的工作效率。Go富有表现力、简洁、干净且高效。它的并发机制使编写能够充分利用多核和联网机器的程序变得容易,而其新颖的类型系统可以实现灵活和模块化的程序构建。Go可以快速编译为机器代码,同时还具有垃圾收集的便利性和运行时反射......
  • go 总结
    (4).总结:引用顺序是:main.go->add.go->b.go编译顺序是:main.go<-add.go<-b.go a.先执行b.go的全局变量初始化、init函数. b.再执行add.go的全局变量初始化、init函数. c.再执行main.go的全局变量初始化、init函数.上面add.go和b.go都有Age,会不会产生覆盖?......
  • 10.19
    上午上了统一建模语言,讲了类图,顺序图,并且讲解了我们的模型不足,以及哪里需要修改,然后上了体育课,体育课,练习了蓝球的过人技巧,最后进行了比赛,虽然没有胜利,但是收获了很多,并且获得了很多的蓝球知识,下午上了数据结构和离散数学,数据结构讲了树和森林的遍历方法,离散数学讲了相容关系,以及......
  • 小白学Python - 使用 Django 的天气应用程序
    使用Django的天气应用程序本文中我们将学习如何创建一个使用Django作为后端的天气应用程序。Django提供了一个基于PythonWeb框架的Web框架,允许快速开发和干净、务实的设计。基本设置cdweather启动服务器pythonmanage.pyrunserver要检查服务器是否正在运行,请转至Web......
  • 一文1000字彻底搞懂Web测试与App测试的区别
    总结分享一些项目需要结合Web测试和App测试的工作经验给大家:从功能测试区分,Web测试与App测试在测试用例设计和测试流程上没什么区别。而两者的主要区别体现在如下几个方面:1系统结构方面Web项目,B/S架构,基于浏览器的;Web测试过程中,客户端会随服务器端同步更新,所以只需更新服务器......
  • 在Postgresql中,为表中的所有点提供100m缓冲区,其中有50M个条目
    我有一个表,有50米的纬度和经度条目,geom是我从QGIS导入时创建的。我试图通过调用jupyter笔记本中的表来为表中的所有点创建一个100m的缓冲区。我还尝试使用SRID:25832转换坐标系,以米为单位表示缓冲区,但在postgresql视图中看不到投影到正确位置的点。SELECT*FROMpublic."opera......