In a bunch of number to find out the biggest minimum value
2012-02-17 23:31
531 查看
问题:在一堆数中找出最大跟最小的值
如在5个整数中找出最大跟最小的值:
算法设计:
问题可以简化为:在含n(n是2的幂(n>=2))个元素的集合中寻找极大元和极小元。用分治法(二分法)可以用较少比较次数地解决上述问题:
1) 将数据等分为两组(两组数据可能差1),目的是分别选取
其中的最大(小)值。
2) 递归分解直到每组元素的个数≤2,可简单地找到最大(小)
值。
3) 回溯时将分解的两组解大者取大,小者取小,合并为当前问题的解。
程序:
#include<stdio.h>
#define n 5
int a
;
void maxmin(int i,int j,int *fmax,int *fmin)
{
int mid;
int lmax,lmin,rmax,rmin;
if(i==j)//问题划成一个时
{
*fmax=a[i];
*fmin=a[i];
}
else if(i==j-1)//问题划分成两个时
if(a[i]<a[j])
{
*fmax=a[j];
*fmin=a[i];
}
else {
*fmax=a[i];
*fmin=a[j];
}
//不管是一个或两个都能进行直接运算
else { //问题规模较大此时要进行分治,只有分成两个或一个时才能进行运算
mid=(i+j)/2;
maxmin(i,mid,&lmax,&lmin);//左边数据传递
maxmin(mid+1,j,&rmax,&rmin);//右边数据传递
if(lmax>rmax)
*fmax=lmax;
else
*fmax=rmax;
if(lmin>rmin)
*fmin=rmin;
else
*fmin=lmin;
}
}
int main()
{
int i=1;
int fmax;
int fmin;
for(;i<=5;)
{
scanf("%d",&a[i]);
i++;
}
maxmin(1,n,&fmax,&fmin);
printf("%d %d\n",fmax,fmin);
return 0;
}
如在5个整数中找出最大跟最小的值:
算法设计:
问题可以简化为:在含n(n是2的幂(n>=2))个元素的集合中寻找极大元和极小元。用分治法(二分法)可以用较少比较次数地解决上述问题:
1) 将数据等分为两组(两组数据可能差1),目的是分别选取
其中的最大(小)值。
2) 递归分解直到每组元素的个数≤2,可简单地找到最大(小)
值。
3) 回溯时将分解的两组解大者取大,小者取小,合并为当前问题的解。
程序:
#include<stdio.h>
#define n 5
int a
;
void maxmin(int i,int j,int *fmax,int *fmin)
{
int mid;
int lmax,lmin,rmax,rmin;
if(i==j)//问题划成一个时
{
*fmax=a[i];
*fmin=a[i];
}
else if(i==j-1)//问题划分成两个时
if(a[i]<a[j])
{
*fmax=a[j];
*fmin=a[i];
}
else {
*fmax=a[i];
*fmin=a[j];
}
//不管是一个或两个都能进行直接运算
else { //问题规模较大此时要进行分治,只有分成两个或一个时才能进行运算
mid=(i+j)/2;
maxmin(i,mid,&lmax,&lmin);//左边数据传递
maxmin(mid+1,j,&rmax,&rmin);//右边数据传递
if(lmax>rmax)
*fmax=lmax;
else
*fmax=rmax;
if(lmin>rmin)
*fmin=rmin;
else
*fmin=lmin;
}
}
int main()
{
int i=1;
int fmax;
int fmin;
for(;i<=5;)
{
scanf("%d",&a[i]);
i++;
}
maxmin(1,n,&fmax,&fmin);
printf("%d %d\n",fmax,fmin);
return 0;
}
相关文章推荐
- EF 6.0 The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. c#中的时间
- Find minimum number of people to reach to spread a message across all people in twitter
- [entity framework ] The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
- Unable to find a value for "字段名" in object of class java.lang.String using operator "."
- How-to find the SQL that using lots of temp tablespace in Oracle
- Please change the value of 'Security.salt' in app/config/core.php to a salt value specific to your a
- how to find out the response code(200,404,400...) of the URL?
- Failure to find xxx in xxx was cached in the local repository, resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced @ xxx
- Unable to find a value for "dwmc" in object of class "java.lang.String" using operator "." (null
- Unable to See the Legal Entity List of Value in the Bank Account Owner Field [ID 415529.1]
- Unable to find a value for "tStatus" in object of class org.entity.Passport using operator "."
- python: find the index of a given value in a list
- Server-02 To view the connection number of shared folder in windows OS
- Could not find output/file.out in any of the configured local directories
- How to calculate the number of parameters in CNNs?
- #146 – Use GetValueSource Method to Find the Source of a Dependency Property Value
- Deployment is out of date due to changes in the underlying project contents. You'll need to manually 'Redeploy' the project to u
- find all pairs of elements in a balanced BST that sum to a certain number
- Find the number of subsets such that the sum of numbers in the subset is a prime number
- 1. 找出数组中的单身狗OddOccurrencesInArray Find value that occurs in odd number of elements.