HDU——1303Doubles(水题,试手二分查找)

Doubles

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4266    Accepted Submission(s): 2945

Problem Description
As part of an arithmetic competency program, your students will be given randomly generated lists of from 2 to 15 unique positive integers and asked to determine how many items in each list are twice some other item in the same list. You will need a program to help you with the grading. This program should be able to scan the lists and output the correct answer for each one. For example, given the list
1 4 3 2 9 7 18 22

your program should answer 3, as 2 is twice 1, 4 is twice 2, and 18 is twice 9.
 
Input
The input file will consist of one or more lists of numbers. There will be one list of numbers per line. Each list will contain from 2 to 15 unique positive integers. No integer will be larger than 99. Each line will be terminated with the integer 0, which is not considered part of the list. A line with the single number -1 will mark the end of the file. The example input below shows 3 separate lists. Some lists may not contain any doubles.
 
Output
The output will consist of one line per input list, containing a count of the items that are double some other item.
 
Sample Input
1 4 3 2 9 7 18 22 0 2 4 8 10 0 7 5 11 13 1 3 0 -1
 
Sample Output
3 2 0
题目简单,就是注意下数组何时进行创建并初始化、输入即可。既然今天学了二分查找,那就拿这道题试试手吧。最基础的二分查找(用二分一般得是有序的比如sort过的序列,不然难办)
代码:
#include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
using namespace std;
bool sea(const int a[],const int &len,const int &n)
{
    int low=0,high=len-1,mid;//定义数组的开始,结尾,以及要用到的mid伪下标中值
    while (low<=high)
    {
        mid=(high+low)/2;
        if(a[mid]==n)
            return true;找到直接返回true
        else if(a[mid]<n)
        {
            low=mid+1;
        }
        else if(a[mid]>n)
        {
            high=mid-1;
        }
    }
    return false;//上面没有进行返回说明没找到,返回false
}
int main(void)
{
    int t,sum,j,k,i;
    while (cin>>t&&t!=-1)
    {
        i=0;
        int list[20000]={0};
        list[i++]=t;
        while (cin>>t&&t)
        {
            list[i++]=t;            
        }    
        sort(list,list+i);
        sum=0;
        for (j=0; j<i; j++)
        {
            sum+=sea(list,i,2*list[j]);//用布尔值计算挺方便
        }
        cout<<sum<<endl;
        i=0;
    }
    return 0;
}
之前想用vector的二分搜索函数的,调试半天发现怎么连例子数据都对不上...郁闷半天原来if后面多了个分号
#include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
using namespace std;
int main(void)
{
	int t,sum,temp;
	vector<int>list;
	while (cin>>t&&t!=-1)
	{
		vector<int>::iterator it;
		if(t!=0)
		{
			list.push_back(t);
		}			
		else
		{
			sum=0;
			sort(list.begin(),list.end());
			for (it=list.begin(); it!=list.end(); it++)
			{
				temp=2*(*it);
				if(binary_search(list.begin(),list.end(),temp))//之前这里多了个分号难怪错的....
				{
					sum++;
				}	
			}
			cout<<sum<<endl;
			list.clear();	
		}
	}
	return 0;
}
原文地址:https://www.cnblogs.com/Blackops/p/5356436.html