怎么做这个程序 [英] How to do this programme

查看:82
本文介绍了怎么做这个程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如下所述。

首先,要求用户输入介于1和1024之间的数字。用户定义的函数

应验证用户的输入值。如果该值不在1到1024之间,则应显示错误

消息。

其次,递归函数以系统方法搜索用户输入值并且

在此过程中打印所有猜测值。递归函数使用其猜测值找到下面显示的数字



假设用户输入625.然后程序在搜索时打印其猜测值

用户的输入值如下所示。

1024,

512,

768,

640 ,

576,

608,

624,

632,

628 ,

626,



假设用户输入300.然后程序在搜索时打印其猜测值对于

用户的输入值如下所示。

1024,

512,

256,
384,

320,

288,

304,

296,
$



我尝试过:



as described below.
First, the user is requested to enter a number between 1 and 1024. A user defined function
should validate the user’s input value. If the value is not between 1 and 1024, then an error
message should be displayed.
Second, a recursive function searches the user input value in a systematic method and
prints all the guess values during the process. The recursive function find the number as
shown below with its guess values.
Assume user enters 625. Then the programme print its guess values while searching for the
user’s input value as shown below.
1024,
512,
768,
640,
576,
608,
624,
632,
628,
626,
625

Assume user enters 300. Then the programme print its guess values while searching for the
user’s input value as shown below.
1024,
512,
256,
384,
320,
288,
304,
296,
300

What I have tried:

#include <iostream>
#include <math.h>
int search(int num);
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int sum=0;
int main(int argc, char** argv) {
	int x;
	cout <<"enter number between 0 to 1024 ";
	cin>>x;
	search(x);
	
	return 0;
}
int search(int num){
	int p=num;
	if(sum!=num){
		for(int i=0;i<100;i++){
			if(sum<num){
			sum=1024/2*pow(2,i)+sum;
			cout<<sum<<endl;
		}else{
			sum=1024/(2*pow(2,i))-sum;
			cout<<sum<<endl;
		}
		
		
	}return search(sum);
}else{
	return 1;
}
}

推荐答案

这是二元搜索算法的一个例子,你可以在其中取最大值和最小值价值并走到中途点。如果该值仍然大于要搜索的值,则最小值变为中途且不到一半,最大值变为中途。

你应该尝试它很有趣并且有这个递归的事情到把它扭了一下。但最后它只是递归和其他一些。
This is an example of binary search algorithm in which you take the maximum and minimum value and go to halfway point. If the value is still larger than the value to be searched then, the minimum becomes halfway and less than halfway, the maximum becomes halfway.
You should try it it is fun and there is this recursion thing to twist it a little bit. But in the end it is just recursion and some if else.


这是一个简单的二进制文章:二进制搜索算法 - 维基百科 [ ^ ] - 其中一个在一个范围内缩小未知值的最基本方法。

你从最大和最小可能值开始,你猜它们之间的中间位置。

如果值大于中途,则最小值变为中途。

如果相同,则找到它。

如果小于中途,最大值变为中途

重复直到找到。



这里的扭曲是你的作业要求它是递归的(这是递归的一个非常糟糕的例子,但是嘿!这是给你的功课!)



我会在你的代码中添加一个测试来检查用户输入是否在范围内......
It's a simple binary chop: Binary search algorithm - Wikipedia[^] - one of the most basic ways of "narrowing in" on a unknown value in a range.
You start with the max and min possible values, and you guess halfway between them.
If the value is greater then halfway, the minimum becomes halfway.
If it is the same, you found it.
if it is less than halfway, the maximum becomes halfway
Repeat until found.

The twist here is that your homework requires it to be recursive (which is a pretty bad example of recursion, but hey! That's homework for you!)

And I'd add a test to your code to check the user input is in range...


这篇关于怎么做这个程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆