1-9添加运算符使其和为100

问题描述:

设计一个算法再1、2、3.......9(顺序不能变)数字之间插入+或者-或者什么都不插入,使得计算结果为100的程序,输出所有的可能性,例如:1+2+34-5+67-8+9=100;

思路:

如果1+2+3...到最后一个数字发现不能为100,则先退回到倒数第二个结点,从该结点选择另外的符号;

这个过程就是典型的回溯方法,因此可以使用回溯的方法来求解该问题:

代码:

#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include <stdlib.h>
using namespace std;

//1,2,3,4,5,6,7,8,9在其中任意插入+,-或者不加,使得结果为100,-1为加,-2为减
int calResult(int *result) {
	int sum = 0;
	int number = 0;
	int status = -1;
	int index = 0;
	for (int i = 0; i < 20; i++) {
		if (result[i] != 0) {	//表示是有内容的
			if (result[i] < 0) {
				char *result2 = new char[i-index];
				for (int j = 0; j < i-index; j++) {
					result2[j] = char(result[index+j]+48);
				}
				int n = atoi(result2);
				if (status == -1) {
					sum += n;
				}
				else {
					sum -= n;
				}

				if (result[i] == -1) {
					status = -1;
				}
				else {
					status = -2;
				}
				index = i+1;
			}
		}
	}
	return sum;
}

void display(int *result) {
	for (int i = 0; i < 20; i++) {
		if (result[i] == -1 && result[i + 1] == -1 ) {
			break;
		}
		if (result[i] == -2 && result[i + 1] == -1) {
			break;
		}
		if (result[i] == -1) {
			cout << " + ";
		}
		else if (result[i] == -2) {
			cout << " - ";
		}
		else {
			cout << result[i];
		}
	}
	cout << " = 100" << endl;
	cout << endl;
}

//回溯
void findOnehundard(int *a, int *result, int n, int re_n) {
    //n为a循环到了第几个数,re_n为reuslt循环到了哪个数
	if (n==9) {
		if (calResult(result) == 100) {
			display(result);
		}
		return;
	}
	for (int i = 0; i < 3; i++) {//0为加(-1),1为减(-2),2为空,不添加操作符
		if (i == 0) {
			result[re_n] = a[n];
			result[re_n + 1] = -1;
			if (n == 8) {
				result[re_n + 2] = -1;
			}
			findOnehundard(a, result, n + 1, re_n+2);
		}
		else if (i == 1) {
			result[re_n] = a[n];
			result[re_n + 1] = -2;
			if (n == 8) {
				result[re_n + 2] = -1;
			}
			findOnehundard(a, result, n + 1, re_n + 2);
		}
		else {
			result[re_n] = a[n];
			if (n == 8) {
				result[re_n + 1] = -1;
			}
			findOnehundard(a, result, n + 1, re_n + 1);
		}
		
	}
}


int main() {
	int a[9] = { 1,2,3,4,5,6,7,8,9 };//原数字空间
	int result[20];
	for (int i = 0; i < 20; i++) {
		result[i] = 0;
	}
	findOnehundard(a, result, 0, 0);
	system("pause");
	return 0;
}

运行结果:

(中间还有一些结果没有显示出来)

在C#中,类结构体都可以通过运算符重载来扩展它们的行为,包括对`++`(递增)`--`(递减)操作符的支持。这里分别解释一下: **类(Class)的运算符重载:** 为了重载`++``--`操作符,你需要在类中声明两个特殊的方法,通常命名为`op_Increment()``op_Decrement()`。例如,对于一个名为`MyNumber`的自定义类: ```csharp public class MyNumber { public int Value { get; set; } // 运算符重载:前缀递增 public static MyNumber operator ++(MyNumber obj) { int oldValue = obj.Value; obj.Value++; return new MyNumber { Value = oldValue }; } // 运算符重载:前缀递减 public static MyNumber operator --(MyNumber obj) { int oldValue = obj.Value; obj.Value--; return new MyNumber { Value = oldValue }; } // 如果你想支持后缀形式,还需要这两个方法: // 后缀递增 public MyNumber Increment() { Value++; return this; } // 后缀递减 public MyNumber Decrement() { Value--; return this; } } ``` **结构体(Struct)的运算符重载:** 由于结构体默认为值类型,这意味着每次对结构体变量的操作都会创建新的实例。所以,你不需要显式地提供`op_Increment``op_Decrement`,因为它们会隐式地创建一个新的实例。但是,你可以为`Increment``Decrement`方法提供实现。 ```csharp public struct MyNumber { public int Value; // 结构体不会自动实现前缀递增/递减,但可以添加后缀版本 public MyNumber Increment() { Value++; return this; } public MyNumber Decrement() { Value--; return this; } } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值