How Many Equations Can You Find

How Many Equations Can You Find

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 604    Accepted Submission(s): 395


Problem Description
Now give you an string which only contains 0, 1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9.You are asked to add the sign ‘+’ or ’-’ between the characters. Just like give you a string “12345”, you can work out a string “123+4-5”. Now give you an integer N, please tell me how many ways can you find to make the result of the string equal to N .You can only choose at most one sign between two adjacent characters.
 

Input
Each case contains a string s and a number N . You may be sure the length of the string will not exceed 12 and the absolute value of N will not exceed 999999999999.
 

Output
The output contains one line for each data set : the number of ways you can find to make the equation.
 

Sample Input
  
  
123456789 3 21 1
 

Sample Output
  
  
18 1
分析,强大的dfs,来自于 学姐 的博客。

code:

<pre name="code" class="cpp">#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
__int64 num,n,len;
char str[15];
void DFS(__int64 x,__int64 sum)
{
    if(x==len)
    {
        if(sum==n)
            num++;
        return ;
    }
    __int64 k=0;//orz,跪了。
    for(int i=x;i<len;i++)
    {
        k=k*10+str[i]-'0';
        DFS(i+1,sum+k);
        if(x!=0)//第一个数字前不能加“-”号
            DFS(i+1,sum-k);
    }
}
int main()
{
    while(scanf("%s %I64d",str,&n)!=EOF)
    {
        len=strlen(str);
        num=0;
        DFS(0,0);
        printf("%I64d\n",num);
    }
    return 0;
}


 
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">一入搜索深似海,从此AC是路人!!!<img alt="抓狂" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/crazy.gif" /><img alt="抓狂" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/crazy.gif" /><img alt="抓狂" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/crazy.gif" /></span><span style="font-size:18px;"></span>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 你可以使用 numpy 库中的 linalg.lstsq() 函数来解决超定方程组。具体步骤如下: 1. 将超定方程组表示为矩阵形式 Ax = b,其中 A 是 m 行 n 列的系数矩阵,x 是 n 维未知向量,b 是 m 维常数向量。 2. 使用 linalg.lstsq() 函数求解 x,该函数的参数为 A 和 b。 3. 检查解是否存在,如果存在,则输出解 x;如果不存在,则说明方程组无解或有无穷多解。 注意:在使用 linalg.lstsq() 函数时,需要注意矩阵 A 的秩是否等于 n,如果不等于,则说明方程组无解或有无穷多解。 ### 回答2: 使用Python解决超定方程的步骤如下: 1. 导入所需的库:首先,需要导入numpy库,以便在Python中使用矩阵和向量的功能。 ``` import numpy as np ``` 2. 定义方程:将超定方程表示为矩阵形式,其中方程的系数作为矩阵的元素,等式的右侧作为结果向量。 ``` A = np.array([[1, 2], [3, 4], [5, 6]]) b = np.array([7, 8, 9]) ``` 3. 求解方程:使用numpy库的线性代数模块中的函数`lstsq()`来求解超定方程。该函数将返回最小二乘解向量x。 ``` x = np.linalg.lstsq(A, b, rcond=None)[0] ``` 4. 打印解向量:最后,将解向量打印出来以查看结果。 ``` print(x) ``` 完整的代码示例: ``` import numpy as np A = np.array([[1, 2], [3, 4], [5, 6]]) b = np.array([7, 8, 9]) x = np.linalg.lstsq(A, b, rcond=None)[0] print(x) ``` 此代码将返回超定方程的最小二乘解向量x。 ### 回答3: 要用Python解决一个过度确定方程组,可以使用线性代数库NumPy和SciPy中的函数来实现。下面是一种可能的解决方案: 1. 首先,安装NumPy和SciPy库,可以使用pip命令在命令行中执行以下命令: ``` pip install numpy scipy ``` 2. 创建一个Python脚本文件,并在文件开头引入NumPy和SciPy库: ```python import numpy as np from scipy.linalg import lstsq ``` 3. 定义过度确定方程组的系数矩阵A和结果向量b。可以将它们表示为NumPy数组: ```python A = np.array([[2, 3], [4, 5], [6, 7], [8, 9]]) b = np.array([10, 20, 30, 40]) ``` 4. 使用lstsq函数来解决过度确定方程组。该函数返回一个包含最小二乘解的数组x,以及一些其他信息: ```python x, residuals, rank, s = lstsq(A, b) ``` 在上述代码中,x是解向量,residuals是残差(方程组的近似度量),rank是系数矩阵的秩,s是系数矩阵的奇异值。 5. 打印解向量x: ```python print("Solution vector:") print(x) ``` 这将输出解向量x的值。 6. 运行Python脚本,即可得到过度确定方程组的解。 上述步骤展示了如何使用Python中的NumPy和SciPy库来解决过度确定方程组。这种方法适用于大多数线性方程组,但对于非线性方程组,需要使用其他方法来解决。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值