两倍问题

题目源地址

Description

给定2到15个不同的正整数,你的任务是计算这些数里面有多少个数对满足:数对中一个数是另一个数的两倍。比如给定1 4 3 2 9 7 18 22,得到的答案是3,因为2是1的两倍,4是2个两倍,18是9的两倍。 

Input

输入包括n组测试数据。每组数据包括一行,给出2到15个两两不同且小于100的正整数。每一行最后一个数是0,表示这一行的结束后,这个数不属于那2到15个给定的正整数。

Output

对每组输入数据,输出一行,给出有多少个数对满足其中一个数是另一个数的两倍。

Sample Input

3
1 4 3 2 9 7 18 22 0
2 4 8 10 0
7 5 11 13 1 3 0

Sample Output

3
2
0

经过苦苦思索的结果,发现并没有想象中那么的难~好高兴

 1 #include <iostream>
 2 #include <math.h>
 3 #include <stdio.h>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     int n,a[15],i,j,k,count;
10     cin>>n;
11     while (n--)
12     {
13         i=count=0;
14         while (1)
15         {
16           cin>>a[i];
17           if (a[i]==0)
18           break;
19           i++;
20         }
21         k=i;
22         sort(a,a+k);
23         for (i=0;i<k;i++)
24         {for (j=i;j<k;j++)
25             if (a[j]==2*a[i])
26             count++;}
27 
28 
29 cout << count<< endl;
30 
31     }
32 
33 
34 
35 
36     return 0;
37 }
 
原文地址:https://www.cnblogs.com/twomeng/p/9475841.html