# 由一个C++版本猜数字游戏引起的效率问题

#include
#include
#include
#include
#include
using std::string;
using std::endl;
using std::cin;
using std::cout;
int main()
{
int min,max;
double k=0;
string text="请输入你要猜测的数字的范围";
string text1="猜数字游戏";
cout< cout< cout<";
cin>>min;
cout<";
cin>>max;
srand(time(0));
int range=max-min+1;
int date=rand()/100%range+min;
cout<";
cin>>date;
if(max>date&&mink)
{
k=(k+max)/2;
if(k==date)
cout< else
cout< }
else
{
do
{
k=(k+min)/2;
}
while(k==date);
cout<<"恭喜你,答对了"< }
}
else
{
cout< <>"< cout<<" 下次再来哦！祝贺你下次成功! "< cout< return 0;
}
return 0;
}

#include
#defineMAX10000000
#defineSL25
int t[MAX], m = 0, num = 0;
void hf(int n, int *t)//整数划分生成树的线性表存储的函数;
{
int i;
if(n) {
for(i = 1; i <= n; i++) {
t[m++] = i;
hf(n-i,t);
}
}
else {
t[m++] = '#';
num++;
}
}
void print(int *t)//打印整数划分生成树的线性表;
{
int l;
printf("Array T is:\n");
for(l = 0; l >",k);
for(i = 0; i < len; i++)
printf(" %d", S[i]);
printf("\n");
k++;
}
void scan(int *t)//扫描存储树的线性表生成可识别的组合结果;
{
int S[SL] = {0}, S1[SL] = {0}, S2[SL] = {0};
int i, leap = 1, top = 0, top1 = 0, top2 = 0;
int z1 = 0, z2 = 0;
for(i = 0; i < m; i++) {
if(leap) {
if(t[i] != '#')
S[top++] = t[i];
else {
leap = 0;
print_s(S,top);
}
}
else {
if(t[i] != '#') {
S1[top1] = t[i];
z1 = z1 + S1[top1];
top1++;
}
else {
while(z1 != z2) {
S2[top2] = S[--top];
S[top] = 0;
z2 = z2 + S2[top2];
S2[top2] = 0;
top2++;
}
top2 = 0;
while(top1) {
S2[top2++] = S1[--top1];
S1[top1] = 0;
}
while(top2) {
S[top++] = S2[--top2];
S2[top2] = 0;
}
print_s(S,top);
z1 = z2 = 0;
}
}
}
}
int main()//整数划分主函数;
{
int n;
printf("Enter n:");
scanf("%d",&n);
hf(n,t);
print(t);
scan(t);
return 0;
}

• 博文量
118
• 访问量
266234