竖式算术计算编程Swift && Objective-c && Java && Flutter
作者:互联网
竖式算术计算编程Swift && Objective-c && Java && Flutter
*****闲暇之余弄个竖式算术计算纪念一下。出于好奇弄了Swift、Objective-c、Java、Flutter等几个语言的计算编程;
题目:编程求解该竖式加法运算式子中每一个字所代表的值,取值范围>=[-9, 9];
解法一:使用Swift语言
// 取值范围可无限扩大
let array = [-9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for a in array {
for b in array {
for c in array {
for d in array {
if (2021 == a + (a * 10 + b) + (100 * a + 10 * b + c) + (1000 * a + 100 * b + 10 * c + d)) {
print("a = \(a)", "b = \(b)", "c = \(c)", "d = \(d)")//详细对应打印
print(a, b, c, d)
}
}
}
}
}
解法二:使用Objective-c语言
// 取值范围[-9,9]
for (int a = 9; a >= -9; a--) {
for (int b = 9; b >= -9; b--) {
for (int c = 9; c >= -9; c--) {
for (int d = 9; d >= -9; d--) {
if (2021 == a + (a * 10 + b) + (100 * a + 10 * b + c) + (1000 * a + 100 * b + 10 * c + d)) {
NSLog(@"%d, %d, %d, %d", a, b, c, d);
}
}
}
}
}
解法三:使用Flutter_Dart语言
// 取值范围可无限扩大
var alex21 = [-9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
for(var a in alex21) {
for (var b in alex21) {
for (var c in alex21) {
for (var d in alex21) {
if (2021 == a + (a * 10 + b) + (100 * a + 10 * b + c) + (1000 * a + 100 * b + 10 * c + d)) {
print("$a, $b, $c, $d");
}
}
}
}
}
解法四:使用Java语言
for (a in -9..9) {
for (b in -9..9) {
for (c in -9..9) {
for (d in -9..9) {
if ((2021 == a + (a * 10 + b) + (100 * a + 10 * b + c) + (1000 * a + 100 * b + 10 * c + d))) {
print("$a, $b, $c, $d");
}
}
}
}
}
以上各种解法的答案就不在这一一列举,大家可以去验证。(突然想起,很久很久很久没有人给我点赞了= || =)
标签:alex21,Java,10,竖式,&&,var,100,array 来源: https://blog.csdn.net/weixin_44587801/article/details/119487573