POJ 1552 Doubles
2015-04-14 22:16
423 查看
Doubles
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
your program should answer 3, as 2 is twice 1, 4 is twice 2, and 18 is twice 9.
Input
The input 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
Sample Output
因为每个数都不一样,用二分查找,先升序排序,然后对于每个数ai在其后面的数中查找是否有ai*2,有的话答案就加1.
这个题的数据规模很小,用二分是看的起它,其实可以直接搞,
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 19859 | Accepted: 11492 |
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 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
因为每个数都不一样,用二分查找,先升序排序,然后对于每个数ai在其后面的数中查找是否有ai*2,有的话答案就加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 ),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; }
这个题的数据规模很小,用二分是看的起它,其实可以直接搞,
#include <stdio.h> #include <string.h> #include <math.h> #define eps 1e-9 #define max(a,b) (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 ),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; }
相关文章推荐
- (DS1.4.)poj 1552 Doubles(测试组数和每组测试用例的个数不定)
- poj 1552 Doubles(简单的模拟一下)
- ZOJ 1760 &&POJ1552 Doubles (模拟)
- POJ-1552 Doubles-二倍关系
- POJ 1552 Doubles
- poj 1552 Doubles
- poj 1552 Doubles【goto语句】
- POJ 1552 Doubles
- POJ 1552-Doubles
- poj 1552 Doubles 水
- poj1552--Doubles
- POJ1552 HDU1303 UVALive2782 ZOJ1760 Doubles【序列】
- POJ 1552 - Doubles
- POJ 1552 Doubles
- poj 1552 Doubles
- [1552]Doubles (纯水题) POJ
- poj 1552 Doubles
- poj 1552 Doubles
- POJ 1552 && HDU 1303 Doubles(水~)
- POJ 1552 Doubles 水