Directly on the sample, not much bb, their own experience.
[Example 1]
[] [] [] + [] [] [] = [] [] [], the numbers 1 to 9 were filled wherein each number is used only once and that the equation holds. Will a reasonable combination of how many there are?
//用a[1]~a[9]表示填入的九个数,用一个book数组来标记已经出现的数字
#include<stdio.h>
int main()
{
int a[10],i,total=0,book[10],sum;
for(a[1]=1;a[1]<=9;a[1]++)
for(a[2]=1;a[2]<=9;a[2]++)
for(a[3]=1;a[3]<=9;a[3]++)
for(a[4]=1;a[4]<=9;a[4]++)
for(a[5]=1;a[5]<=9;a[5]++)
for(a[6]=1;a[6]<=9;a[6]++)
for(a[7]=1;a[7]<=9;a[7]++)
for(a[8]=1;a[8]<=9;a[8]++)
for(a[9]=1;a[9]<=9;a[9]++)
{
for(i=1;i<=9;i++)//初始化book数组
book[i]=0;
for(i=1;i<=9;i++)//如果出现某个数就标记一下
book[a[i]]=1;
//统计共出现了多少个不同的数
sum=0;
for(i=1;i<=9;i++)
sum+=book[i];
//如果刚好出现了9个不同的数,并且满足等式就输出
if(sum==9 && a[1]*100+a[2]*10+a[3]+a[4]*100+a[5]*10+a[6]
==a[7]*100+a[8]*10+a[9])
{
total++;
printf("%d%d%d+%d%d%d=%d%d%d\n",a[1],a[2],a[3],a[4],
a[5],a[6],a[7],a[8],a[9]);
}
}
printf("total=%d",total/2);
getchar();getchar();
return 0;
}
[Example 2]
#include<stdio.h>
int main()
{
char a[20][21];//假设这里的地图大小不超过20*20
int i,j,sum,map=0,p,q,x,y,n,m;
//读入n和m,n表示有多少行字符,m表示每行有多少列
scanf("%d %d",&n,&m);
//读入n行字符
for(i=0;i<=n-1;i++)
scanf("%s",a[i]);
//用两重循环枚举地图中的每个点
for(i=0;i<=n-1;i++)
{
for(j=0;j<=m-1;j++)
{
//首先判断这个点是不是平地,是平地才能放炸弹
if(a[i][j]=='.')
{
sum=0;//sum用来计数(可以消灭的敌人数),初始化为0
//将当前坐标i,j复制到两个新变量x,y中,以便向上下左右四个方向分别统计
//可以消灭的敌人数
//向上统计可以消灭的敌人数
x=i; y=j;
while(a[x][y]!='#')//判断是不是墙,不是墙就可以继续
{
//如果当前点是敌人,则进行计数
if(a[x][y]=='G')
sum++;
//x--的作用是继续向上统计
x--;
}
//向下统计可以消灭的敌人数
x=i; y=j;
while(a[x][y]!='#')
{
if(a[x][y]=='G')
sum++;
//x++的作用是继续向下统计
x++;
}
//向左统计可以消灭的敌人数
x=i; y=j;
while(a[x][y]!='#')
{
if(a[x][y]=='G')
sum++;
//y--的作用是继续向左统计
y--;
}
//向右统计可以消灭的敌人数
x=i; y=j;
while(a[x][y]!='#')
{
if(a[x][y]=='G')
sum++;
//y++的作用是继续向右统计
y++;
}
//更新map的值
if(sum>map)
{
//如果当前点所能消灭的敌人总数大于map,这更新map
map=sum;
//并用p和q记录当前点的坐标
p=i;
q=j;
}
}
}
}
printf("将炸弹放在(%d,%d),最多可以消灭%d个敌人",p,q,map);
getchar();getchar();
return 0;
}
[Example 3]
An n a match, it is desirable to spell the equation of A + B = C. Equation A, B, C, are used to spell out the integer match (if the number of non-zero, then the most significant bit is not 0), 0 to 9 numeric spelling follows:
NOTE:
1. The number and the like plus no matches each require two;
2. If a does not equal B, then a + B = C and B + a = C as different equations;
3. All matches are on use;
4. number of matches does not exceed 24 ;
5. 1 second limit.
tips:
Title maximum of 24 matches, out of "+" and "=" occupied four matches, up to 20 matches left, and 0 to 9 ten numbers, the number 1 is used matches the number of minimum , only two matches. 20 and 10 match up to 1 composition. Thus A + B = C this equation, A, B, C in the figures are not any more than 1111.
#include<stdio.h>
int fun(int x)
{
int num=0;//用来计数的变量,要初始化
int f[10]={6,2,5,5,4,5,6,3,7,6};
//用一个数组来记录0~9每个数字需要用到多少根火柴
while(x/10!=0)
{
//获得x的末尾数字并将此数用到的火柴数累加到sum中
num +=f[x%10];
x=x/10;//去掉x的末尾数字
}
//最后加上此时x所需用到的火柴数(此时x一定是一个个位数)
num +=f[x];
return num;//返回需要用到的火柴总数
}
int main()
{
int a,b,c,m,sum=0;//sum是用来计数的,要初始化
scanf("%d",&m);//读入火柴的个数
//开始枚举a和b
for(a=0;a<=1111;a++)
{
for(b=0;b<=1111;b++)
{
c=a+b;//计算出c
//fun是我们自己写的子函数,用来计算一个数所需要用到的火柴数
//当a使用的火柴总数+b使用的火柴总数+c使用的火柴总数之和恰好
//等于m-4的时候,则成功找到一组解
if(fun(a)+fun(b)+fun(c)==m-4)
{
printf("%d+%d=%d\n",a,b,c);
sum++;
}
}
}
printf("一共可以拼出%d个不同的等式",sum);
getchar();;getchar();
return 0;
}