uva 10317 Equating Equations

点击打开链接uva 10317


思路:搜索
分析:
1 给定一个等式判断两边是否相等,如果一个等式相等那么通过移项到同一边可以得到正数的和等于负数
2 那么通过分析1我们可以知道我们可以求出这个等式的所有数字的和,判断和是否为偶数。如果和为奇数那么肯定不可能成立,因为不能被平分
3 通过分析2的剪枝之后,那么可以知道题目说了最多有16 个数字。那么我们就搜索这个2^16种状态,找到一个满足的即可。注意在搜索的过程中可以进行剪枝

代码:


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int MAXN = 10000;
char inStr[MAXN]; 
char expStr[MAXN]; 
int num[MAXN];
int pos , cnt , sum;

void init(char *str){
    int len = strlen(str);
    bool isLeft = true;
    pos = sum = 0;
    cnt = 1;
    int i = 0;
    while(i < len){
        if(isalnum(str[i])){
            int j = i;
            int tmp = 0;
            while(j < len && isalnum(str[j])){
                tmp = tmp*10+str[j]-'0';
                j++; 
            }
            sum += tmp;
            num[pos++] = tmp;
            i = j-1;
        }
        else{
            if(str[i] != ' ')
                expStr[pos] = str[i];
            if(str[i] == '+' && isLeft)   
                cnt++;
            if(str[i] == '=')
                isLeft = false;
            if(str[i] == '-' && !isLeft)
                cnt++; 
        } 
        i++;
    }
}

void output(int *arr , int tmpPos){
    bool vis[MAXN];
    bool isLeft = true;
    memset(vis , false , sizeof(vis));
    for(int i = 0 ; i <= tmpPos ; i++)
        vis[arr[i]] = true;
    int tmpArr[MAXN];
    int tmpArrPos = 0;
    for(int i = 0 ; i < pos ; i++)
        if(!vis[i])
          tmpArr[tmpArrPos++] = i;
    tmpArrPos--;

    printf("%d" , num[arr[tmpPos--]]);
    for(int i = 1 ; i < pos ; i++){
        if(expStr[i] == '+'){
            if(isLeft)
                printf(" + %d" , num[arr[tmpPos--]]);
            else
                printf(" + %d" , num[tmpArr[tmpArrPos--]]);
        }
        else if(expStr[i] == '-'){
            if(!isLeft)
                printf(" - %d" , num[arr[tmpPos--]]); 
            else
                printf(" - %d" , num[tmpArr[tmpArrPos--]]); 
        }
        else{
            isLeft = false;
            printf(" = %d" , num[tmpArr[tmpArrPos--]]); 
        }
    }    
    printf("\n");
}

void solve(){
    int tmpNum = (1<<pos)-1;
    int arr[MAXN]; 
    sum >>= 1;
    while(tmpNum){
        int tmp = tmpNum;
        int tmpCnt = -1;
        int tmpPos = pos-1;
        int tmpSum = 0;
        while(tmp){
            if(tmp&1){
                tmpCnt++;
                arr[tmpCnt] = tmpPos;
                tmpSum += num[tmpPos];
            } 
            tmpPos--;
            tmp >>= 1;
            if(tmpSum > sum || tmpCnt+1 > cnt)
                break;
        } 
        if(tmpCnt+1 == cnt && tmpSum == sum){
            output(arr , tmpCnt);
            return; 
        }
        tmpNum--;
    }
    printf("no solution\n");
}

int main(){
    while(gets(inStr)){
        init(inStr); 
        if(sum&1)//如果是奇数
            printf("no solution\n");
        else
            solve();
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值