| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3462 人关注过本帖
标题:难倒名牌大学生的题!你试试
只看楼主 加入收藏
wming
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-12-4
收藏
得分:0 
很好的
2005-12-10 13:22
小高
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2005-12-14
收藏
得分:0 
我顶 我们的课本都有这道题
2005-12-16 23:36
huadian508c
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-2-15
收藏
得分:0 

我把两个程序直接拷贝到VC++6.0中,编译的时候都有问题如下: fatal error C1010: unexpected end of file while looking for precompiled header directive
我不明白是为什么,请指教,谢谢!

2006-02-15 16:55
DarkHero
Rank: 1
等 级:新手上路
威 望:2
帖 子:191
专家分:0
注 册:2006-1-14
收藏
得分:0 

//做了第一题。
/******************************************
*
* Copyright by NJU_SE_SirX
* 2006.2.15
******************************************/
#include <iostream>
using namespace std;

class Person {
public:
bool tellLie;
bool doGood;
Person(bool tl=false,bool dg=false) :tellLie(tl),doGood(dg) {}
void reset() {
tellLie=false;
doGood=false;
}
virtual void say(Person *psn,bool isTrue)=0;
};

class PersonA :public Person {
public:
void say(Person *psn,bool isTrue) {
tellLie=!isTrue;
//doGood=false;默认为false
if(tellLie) doGood=true;
}
};
class PersonBC :public Person {
public:
void say(Person *psn,bool isTrue) {
tellLie=!isTrue;
//doGood=false;默认为false
if(isTrue) psn->doGood=true;
}
};

class PersonD :public Person {
public:
void say(Person *psn,bool isTrue) {
tellLie=!isTrue;
//doGood=false;默认为false
if(isTrue) psn->tellLie=true;
}
};
//End Class
PersonA *a;
PersonBC *b;
PersonBC *c;
PersonD *d;
bool checkOK() {
int sum=0; //if OK:(the sum of doGood person) + (the sum of tellLie person) == 2
if(a->doGood) sum++;
if(b->doGood) sum++;
if(c->doGood) sum++;
if(d->doGood) sum++;
if(a->tellLie) sum++;
if(b->tellLie) sum++;
if(c->tellLie) sum++;
if(d->tellLie) sum++;
if(sum!=2) return false;
return true;
}
void prtMsg() {
char name;
if(a->doGood) name='A';
else if(b->doGood) name='B';
else if(c->doGood) name='C';
else name='D';
cout<<name<<" 做了好事。"<<endl;
}
int main() {
//分析可知B、C矛盾,必有1人说谎,所以要么B说谎,要么是C
//if B tell lies
a=new PersonA;
b=new PersonBC;
c=new PersonBC;
d=new PersonD;

a->say(a,true);
b->say(c,false);
c->say(d,true);
d->say(c,true);

if(checkOK()) {
prtMsg();
return 0;
}
//else C tell lies
//reset
a->reset();
b->reset();
c->reset();
d->reset();

a->say(a,true);
b->say(c,true);
c->say(d,false);
d->say(c,true);
prtMsg();

delete a,b,c,d;
return 0;
}


for( ; me.alive() ; ) { 淡泊名利,志存高远 } //Forever
2006-02-15 23:13
DarkHero
Rank: 1
等 级:新手上路
威 望:2
帖 子:191
专家分:0
注 册:2006-1-14
收藏
得分:0 
欢迎批评指正啊!
我的代码总是比较长,但比较易懂和“维护”

for( ; me.alive() ; ) { 淡泊名利,志存高远 } //Forever
2006-02-15 23:15
DarkHero
Rank: 1
等 级:新手上路
威 望:2
帖 子:191
专家分:0
注 册:2006-1-14
收藏
得分:0 
晕,才发现,这么多人做了啊……回帖有3页啊

for( ; me.alive() ; ) { 淡泊名利,志存高远 } //Forever
2006-02-15 23:18
high20033763
Rank: 1
等 级:新手上路
帖 子:85
专家分:0
注 册:2006-2-13
收藏
得分:0 
版主强,我终于遇到高手了
2006-02-17 00:57
anny1209
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-2-17
收藏
得分:0 
厉害

时间可以让人忘掉一切?
2006-02-17 17:10
wugidugi
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-10-19
收藏
得分:0 

//第一道
#include <iostream.h>
bool DoGood[4];
bool a[4];

void A(void){
DoGood[0]=false;
}

void B(void){
DoGood[0]=false;
DoGood[1]=false;
DoGood[2]=true;
DoGood[3]=false;
}

void C(void){
DoGood[0]=false;
DoGood[1]=false;
DoGood[2]=false;
DoGood[3]=true;
}

void D(void){
DoGood[3]=false;
}

int main(void)
{
for(int i=0;i<4;++i){
for(int j=0;j<4;++j)
a[j]=true;
a[i]=false;
if(a[0])A();
if(a[1])B();
if(a[2])C();
if(a[3])D();
for(int i=0;i<4;++i)
cout<<a[i]<<"\t"<<DoGood[i]<<endl;
cout<<endl;
}

}

2006-02-19 13:39
wugidugi
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-10-19
收藏
得分:0 

上面的写得不好 不好意思 我改了改

#include <iostream.h>
bool DoGood[4];
bool a[4];

void A(void){
DoGood[0]=false;
}

void B(void){
DoGood[0]=false;
DoGood[1]=false;
DoGood[2]=true;
DoGood[3]=false;
}

void C(void){
DoGood[0]=false;
DoGood[1]=false;
DoGood[2]=false;
DoGood[3]=true;
}

void D(void){
DoGood[3]=false;
}

int main(void)
{
for(int i=0;i<4;++i){
for(int j=0;j<4;++j)
a[j]=true;
a[i]=false;
if(a[1])
a[2]=false;
else
a[2]=true;
if(a[0])A();
if(a[1])B();
if(a[2])C();
if(a[3])D();
for(int i=0;i<4;++i)
if(a[1]+a[2]+a[3]+a[0]==3&&DoGood[1]+DoGood[2]+DoGood[3]+DoGood[0]==1)
cout<<a[i]<<"\t"<<DoGood[i]<<endl;
}
}

2006-02-19 13:52
快速回复:难倒名牌大学生的题!你试试
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012442 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved