2012蓝桥杯初赛第四题

来源:互联网 发布:国际象棋软件 编辑:程序博客网 时间:2024/05/12 13:24

这道题其实就是枚举,发现蓝桥杯的题目大部分都是枚举,枚举2^10种可能,每一题都有两种状态,不是对就是错,用深搜加剪枝就可以完成,剪枝的关键在于对于当前分数小于或等于0的情况要舍去,下面是代码:

#include <stdio.h>#include <stdlib.h>using namespace std;int record[11]; //记录结果数组void Dfs(int now,int pos){if(now<=0)return ;if(pos==11){   if(now==100){   for(int i=1;i<=10;i++)   printf("%d",record[i]);   printf("\n");   }   return ;}int temp=now;record[pos]=0;temp-=pos;Dfs(temp,pos+1);temp=now;    record[pos]=1;temp+=temp;Dfs(temp,pos+1);}int main(){Dfs(10,1);return 0;}


0 0