Doubles
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 19859 |
|
Accepted: 11492 |
Description
As part of a arithmetic competency program, your students would be given randomly generated lists of from 2 to the unique P ositive integers and asked to determine what many items in each list is twice some other item in the same list. You'll need a program to help you with the grading. This program should is able to scan the lists and output of 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 are twice 1, 4 is twice 2, and are twice 9.
Input
The input would consist of one or more lists of numbers. There would be a list of numbers per line. Each list would contain from 2 to the unique positive integers. No integer would be larger than 99. Each line would be terminated with the integer 0, which was not considered part of the list. A line with the number-1 would mark the end of the file. The example input below shows 3 separate lists. Some lists may not be contain any doubles.
Output
The output would consist of one line per input list, containing a count of the items is double some other item.
Sample Input
1 4 3 2 9 7 18 22 02 4 8 10 07 5 11 13 1 3 0-1
Sample Output
320
because each number is different, search by binary, sort in ascending order, and then for each number AI in the number after it to find whether there is ai*2, the answer is added 1.
#include <stdio.h> #include <string.h> #include <algorithm>using namespace Std;int a[30];int main () { int I,k,n,res;while (scanf ("%d", &a[0]), a[0]!=-1) {N=1;while (scanf ("%d", &a[n]), a[n++]); N--;res=0;sort (a,a+ n); for (i=0;i<n;i++) {k=lower_bound (a+i+1,a+n,a[i]*2)-a;if (a[k]==a[i]*2) res++;} printf ("%d\n", res);} return 0;}
The data size of the problem is very small, with two points is to look at it, in fact, can directly engage,
#include <stdio.h> #include <string.h> #include <math.h> #define EPS 1e-9#define Max (a) (a) > (b)? (a):(b) int res,a[20];int vis[110];int B[110];int main () {int i,n;while (scanf ("%d", &a[0]) && a[0]!=-1) {n=1; while (scanf ("%d", &a[n]), a[n++]); N--;memset (vis,0,sizeof (Vis)); Memset (b,0,sizeof (b)); for (i=0;i<n;i++) b[a [I]] =1;res=0;for (i=0;i<n;i++) {if (b[a[i]*2]) {vis[a[i]*2]=1;vis[a[i]]=1;res++;}} printf ("%d\n", res);} return 0;}
POJ 1552 Doubles