这个程序可以算当一个人知道和,另一个人知道积的时候的
各种对话以后的结果。
目前的设置是计算和前述的题目一样的情况。
程序头上的RANGE_MIN和RANGE_MAX为取数字的范围。
SAME为两个数字是否可以相等。
程序尾部的一系列调用就是对话的过程。
talk函数的三个参数分别是:
1.是否是知道和的那个人说的话
2.说话中的对象是否是知道和的那个人
3.那个对象是被断言知道还是不知道。
比如:
p说我不知道,就是true,true,false
p说q不知道,就是true,false,false
q说我不知道,就是false,false,false
p说我知道了,就是true,true,true
说完一组同一个人说的话以后,需要运行一下commit。
这样设置的原因是,比如p说我不知道和p说q不知道之间,不能有commit。
否则,p说q不知道,就变成了“p说q在听到我说‘我不知道’以后肯定仍然不知道”
而原始题目并不是这个意思。
又比如将前面的设置改为2和100,
后面的序列改成
true true false
true false false
commit
false false true
commit
true true true
commit
的话,得到的就是最原始的那道题目的结果4 13
#include <stdio.h>
#include <string.h>
#define RANGE_MIN 2
#define RANGE_MAX 49
#define SAME false
struct node {
int x,y;
struct node *nexts,*nextp;
struct node *lasts,*lastp;
};
int sum[RANGE_MAX+RANGE_MAX+1];
struct node *sumh[RANGE_MAX+RANGE_MAX+1];
int prod[RANGE_MAX*RANGE_MAX+1];
struct node *prodh[RANGE_MAX*RANGE_MAX+1];
int map[RANGE_MAX+1][RANGE_MAX+1];
void talk(bool fromSum, bool toSum, bool know)
{
int i;
struct node *p;
for(i=(fromSum?RANGE_MIN+RANGE_MIN:RANGE_MIN*RANGE_MIN);i<=(fromSum?RANGE_M
AX+RANGE_MAX:RANGE_MAX*RANGE_MAX);i++)
{
p=(fromSum?sumh[i]:prodh[i]);
while (p!=NULL)
{
if (((toSum?sum[p->x+p->y]:prod[p->x*p->y])==1)^know) break;
p=(fromSum?p->nexts:p->nextp);
}
if (p!=NULL)
{
p=(fromSum?sumh[i]:prodh[i]);
while (p!=NULL)
{
map[p->x][p->y]=2;
p=(fromSum?p->nexts:p->nextp);
}
}
}
}
void submit()
{
int i;
struct node *p,*q;
for(i=RANGE_MIN*2;i<=RANGE_MAX*2;i++)
{
p=sumh[i];
while (p!=NULL)
{
if (map[p->x][p->y]==2)
{
map[p->x][p->y]=0;
if (p->nexts!=NULL)
{
p->nexts->lasts=p->lasts;
}
if (p->lasts!=NULL)
{
p->lasts->nexts=p->nexts;
}
else
{
sumh[p->x+p->y]=p->nexts;
}
if (p->nextp!=NULL)
{
p->nextp->lastp=p->lastp;
}
if (p->lastp!=NULL)
{
p->lastp->nextp=p->nextp;
}
else
{
prodh[p->x*p->y]=p->nextp;
}
sum[p->x+p->y]--;
prod[p->x*p->y]--;
q=p->nexts;
delete p;
p=q;
}
else
{
p=p->nexts;
}
}
}
}
void showResult()
{
int i,j;
for(i=RANGE_MIN;i<=RANGE_MAX;i++)
{
for(j=i+1-SAME;j<=RANGE_MAX;j++)
{
if (map[i][j]==1)
{
printf("%d %d\n",i,j);
}
}
}
}
void main()
{
int i,j;
struct node *p;
memset(sum,0,sizeof(sum));
memset(prod,0,sizeof(prod));
memset(map,0,sizeof(map));
memset(sumh,0,sizeof(sumh));
memset(prodh,0,sizeof(prodh));
for(i=RANGE_MIN;i<=RANGE_MAX;i++)
{
for(j=i+1-SAME;j<=RANGE_MAX;j++)
{
map[i][j]=1;
p=new struct node;
p->x=i;
p->y=j;
p->nexts=sumh[i+j];
if (sumh[i+j]!=NULL)
{
sumh[i+j]->lasts=p;
}
p->lasts=NULL;
sumh[i+j]=p;
p->nextp=prodh[i*j];
if (prodh[i*j]!=NULL)
{
prodh[i*j]->lastp=p;
}
p->lastp=NULL;
prodh[i*j]=p;
sum[i+j]++;
prod[i*j]++;
}
}
talk(true,true,false);
talk(true,false,false);
submit();
talk(false,false,false);
submit();
talk(true,true,true);
submit();
showResult();
}
--
相关新闻
相关关键词: 程序
全站导航
gmail.com