洛谷P1149 [NOIP2008 提高组] 火柴棒等式 暴力枚举 C++(打表)

题目描述

给你n根火柴棍,你可以拼出多少个形如“A+B=C”的等式?等式中的A、B、C是用火柴棍拼出的整数(若该数非零,则最高位不能是0)。用火柴棍拼数字0−9的拼法如图所示:

注意:

  1. 加号与等号各自需要两根火柴棍

  2. 如果A≠B,则A+B=C与B+A=C视为不同的等式(A,B,C>=0)

  3. n根火柴棍必须全部用上

输入格式

一个整数n(n<=24)。

输出格式

一个整数,能拼成的不同等式的数目。

输入输出样例

输入 #1复制

14

输出 #1复制

2

输入 #2复制

18

输出 #2复制

9

说明/提示

【输入输出样例1解释】

22个等式为0+1=1和1+0=1。

【输入输出样例2解释】

9个等式为:

0+4=4
0+11=11
1+10=11
2+2=4
2+7=9
4+0=4
7+2=9
10+1=11
11+0=11

思路简介:

既然已经给出了0-9火柴棒所需要的火柴数,且已知n<=24。可以通过写程序或手算枚举出0到1000数字所需要的火柴数,再按各自对应的下标存入数组中,再利用二重循环枚举出所有可能的等式所需要的火柴盒,和n对比即可。

枚举各个数对应的火柴数和计算所有组合的代码如下:

int a[2001]={6}; //a[0]=6 因为0不能是二位数以上的开头数字,先单独赋值,其他的初始值都为0
int b[10]={6,2,5,5,4,5,6,3,7,6}; //b数组先存储0到9的火柴数
int n,sum=0,i,j;
    cin>>n;
     for(i=1;i<=2000;i++){
           j=i;//如i=32
            while(j>=1){//求每个数所用的火柴棒
             a[i]+=b[j%10];//第一轮循环a[32]=a[32]+b[2]=0+5=5
                          //第二轮a[32]=5+b[3]=10
             j=j/10; //j=32/10=3 //j=3/10=0
          }
        }
     for(i=0;i<=1000;i++){
             for(j=0;j<=1000;j++)
               if(a[i]+a[j]+a[i+j]+4==n)sum++;//还有加号与等号
        }
    cout<

本题有很多AC的方法,还可以把n的24种可能都通过打表算出来,直接存入数组中,录入n输出便可,如:

int a[25]={0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,8,9,6,9,29,39,38,65,88,128};
int n;
cin>>n;
cout<

还可以打表一部分,特判一部分答案,免得判作弊。方法思路都和上述相似: 

#include
using namespace std;
int main(){
	int n;
	cin >> n;
	int sum = 0;
	int b[25] = { 0 };
	b[22] = 65;//打表预先算得的答案直接存到数组里
	b[23] = 88;
	b[24] = 128;
	if (n==22 || n == 23 || n == 24) {
		cout << b[n];
		exit(0);//正常结束程序,返回0给主函数
	}
	int a[900] = { 6,2,5,5,4,5,6,3,7,6 ,8,4,7,7,6,7,8,5,9,8, 11,7,10,10,9,10,11,8,12,11, 11,7,10,10,9,10,11,8,12,11, 10,6,9,9,8,9,10,7,11,10, 11,7,10,10,9,10,11,8,12,11 ,12,8,11,11,10,11,12,9,13,12 ,9,5,8,8,7,8,9,6,10,9 ,13,9,12,12,11,12,13,10,14,13 ,12,8,11,11,10,11,12,9,13,12 ,14,10,13,13,12,13,14,11,15,12 ,10,6,9,9,8,9,10,7,11,10 ,13,9,12,12,11,12,13,10,14,13 ,13,9,12,12,11,12,13,10,14,13 ,12,8,11,11,10,11,12,9,13,12 ,13,9,12,12,11,12,13,10,14,13 ,14,10,13,13,12,13,14,11,15,13 ,11,7,10,10,9,10,11,8,12,11 ,15,11,14,14,13,14,15,12,16,15 ,14,10,13,13,12,13,14,11,15,14 ,17,13,16,16,15,16,17,14,18,17 , 13,9,12,12,11,12,13,10,14,13 ,16,12,15,15,14,15,16,13,17,16 ,16,12,15,15,14,15,16,13,17,16 ,17,13,16,16,15,16,17,14,18,17 ,14,10,13,13,12,13,14,11,15,14 ,18,14,17,17,16,17,18,15,19,18 ,17,13,16,16,15,16,17,14,18,17 ,17,13,16,16,15,16,17,14,18,17 ,13,9,12,12,11,12,13,10,14,13 , 16,12,15,15,14,15,16,13,17,16 ,16,12,15,15,14,16,13,17,16 ,15,11,14,14,13,15,12,16,15 ,16,12,15,15,14,16,13,17,16 ,17,13,16,16,15,17,14,18,17 ,14,10,13,13,12,14,11,15,14 ,18,14,17,17,16,18,15,19,18,17,13,16,16,15,17,14,18,17 ,16,12,15,15,14,15,16,13,17,16 ,12,8,11,11,10,11,12,9,13,12 ,15,11,14,14,13,14,15,12,16,15 ,15,11,14,14,13,14,15,12,16,15 ,14,10,13,13,12,13,14,11,15,14 ,15,11,14,14,13,14,15,12,16,15 ,16,12,15,15,14,15,16,13,17,16 ,13,9,12,12,11,12,13,10,14,13 ,17,13,16,16,15,16,17,14,18,17 ,16,12,15,15,14,15,16,13,17,16 ,17,13,16,16,15,16,17,14,18,17 ,13,9,12,12,11,12,13,10,14,13 ,16,12,15,15,14,15,16,13,17,16 ,16,12,15,15,14,15,16,13,17,16 ,15,11,14,14,13,14,15,12,16,15 ,16,12,15,15,14,15,16,13,17,16 ,17,13,16,16,15,16,17,14,18,17 ,14,10,13,13,12,13,14,11,15,14 ,18,14,17,17,16,17,18,15,19,18 ,17,13,16,16,15,16,17,14,18,17 ,18,14,17,17,16,17,18,15,19,18 ,14,10,13,13,12,13,14,11,15,14 ,17,13,16,16,15,16,17,14,18,17 ,17,13,16,16,15,16,17,14,18,17 ,16,12,15,15,14,15,16,13,17,16 ,17,13,16,16,15,16,17,14,18,17 ,18,14,15,15,16,17,18,13,19,18 ,15,11,12,12,13,14,15,10,16,15 ,19,15,16,16,17,18,19,14,20,19 ,18,14,15,15,16,17,18,13,19,18 ,15,11,14,14,13,14,15,12,16,15 ,11,7,10,10,9,10,11,8,12,11 ,14,10,13,13,12,13,14,11,15,14 ,14,10,13,13,12,13,14,11,15,14 ,13,9,12,12,11,12,13,10,14,13 ,14,10,13,13,12,13,14,11,15,14 ,15,11,14,14,13,14,15,12,16,15 ,12,8,11,11,10,11,12,9,13,12 ,16,12,15,15,14,15,16,13,17,16 ,15,11,14,14,13,14,15,12,16,15 ,19,15,18,18,17,18,19,16,20,19 ,15,11,14,14,13,14,15,12,16,15 ,18,14,17,17,16,17,18,15,19,18 ,18,14,17,17,16,17,18,15,19,18 ,17,13,16,16,15,16,17,14,18,17 ,18,14,17,17,16,17,18,15,19,18 ,19,15,18,18,17,18,19,16,20,19 ,16,12,15,15,14,15,16,13,17,16 ,20,16,19,19,18,19,20,17,21,20 ,19,15,18,18,17,18,19,16,20,19};
	for (int i = 0; i < 450; i++) {//举例是怎么打表出结果的,把一定范围内,如此处是枚举了0到899对应的火柴棍数,可以手算也可以写程序算
		for (int j = 0; j <450; j++) {
			if (a[i] + a[j] + 4 +a[i+j] == n)sum++;
		}
	}
cout << sum;
	return 0;
}

你可能感兴趣的:(c++,算法,枚举类)