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、status和ans作为参数。
5.返回ans的长度,即为可能的情况数。
6.在process函数中,判断是否已经遍历完了times数组,如果是,则进行下面的操作:
6.1.创建三个变量a、b、c,用于存储可能的延时情况。
6.2.调用counts函数,获取当前status数组中非零元素的个数,存储在变量count中。
6.3.根据count的值,进行不同的情况判断:
6.3.1.如果count为0,说明三个服务都没有使用过,根据给定的关系式计算出a、b、c的值。
6.3.2.如果count为1,说明只有一个服务被使用过,根据给定的关系式计算出a、b、c的值。
6.3.3.如果count为2,说明有两个服务被使用过,根据给定的关系式计算出a、b、c的值。
6.3.4.如果count为3,说明三个服务都被使用过,直接将status数组中的值赋给a、b、c。
6.4.调用verify函数,判断当前的a、b、c是否满足条件,如果满足,则将其作为键存入ans中。
7.如果没有遍历完times数组,则进行递归操作:
7.1.遍历status数组,找到第一个为0的位置。
7.2.将当前遍历到的times元素赋值给status数组的该位置。
7.3.递归调用process函数,传入更新后的status数组,i+1,ans作为参数。
7.4.将status数组的该位置重新置为0,进行下一次遍历。
8.在counts函数中,遍历status数组,统计非零元素的个数,并返回该个数。
9.在verify函数中,根据给定的条件,判断a、b、c是否满足条件,如果满足则返回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))
}
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
}
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;
}








