java - 将两个数组相加,求出 10 的可能组合数
问题描述
如果你有 2 个数组 1 tom
和 1 ton
其中m
和n
都是>= 1
and <= 1000
,找出有多少种方法可以用这两个数组列表得到 10 的总和
前任:
Output: "What is m"
User Input: 7
Output: "What is n"
User Input: 5
Output: "There are 3 ways to get 10"
解决方案
我可能会因为提供答案而被否决,因为您没有提供明确的问题,而且在社区帮助您之前,您没有表现出任何努力自己解决问题。
我能够从您的示例结果中破译的是,ArrayList
如果您有两个输入m
和n
.
从您的输入中,取最大的数字并运行从 1 到该数字的循环。从总和中减去该数字,并检查该差是否大于 0 且小于另一个数字。如果这是真的,你有办法计算 10 的总和。
就像是:
public class StackOverflow {
public static void main(String args[]) {
int m = 7;
int n = 5;
int sum = 10;
// Take the larger of the two inputs
int limit = m > n ? m : n;
int count = 0;
for (int i = 1; i <= limit; i++) {
int possibleN = sum - i;
// Check that the possibleN is greater than 0 and less than n
if (0 < possibleN && possibleN <= n) {
System.out.printf("%d + %d = %d\r\n", i, possibleN, sum);
count++;
}
}
System.out.printf("There are %d ways to get %d\r\n", count, sum);
}
}
结果:
5 + 5 = 10
6 + 4 = 10
7 + 3 = 10
There are 3 ways to get 10
推荐阅读
- swift - UITextView sizeThatFits 刷新后返回不同的值
- c++ - 在与应用程序相同的窗口中显示 Qt 虚拟键盘
- java - 中国Unix时间
- apache - WHERE 子句与 Geode 中的值不匹配
- networkx - 了解 Networkx find_cliques() 函数
- python - Python RotatingFileHandler 没有写入 FileHandler 的所有结果
- ios - 如何修复 Swift 中的“无法使单元格出列”错误
- javascript - Vue 在 created() 过程中什么时候存储数据?
- visual-studio - 嵌套逻辑应用 - 在 Visual Studio 中参数化工作流 ID 时出错
- python - 为什么 Django 应用程序需要 `manage.py` 而不是使用 `__init__py`?