Doubles

Time Limit: 1000MS Memory Limit: 65536KB

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.

Example Input

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

Example Output

3
2
0 题目意思找输入数字中的2倍关系:
注意:推荐用乘法以避免除以0的尴尬。 代码:
#include <cstdio>
#include <string>
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
int compare(const void*a,const void*b)
{
return *(int*)a - *(int*)b;
}
int main(int argc, const char * argv[]){
int nums[], i, count;
while () {
count = i = ;
while (scanf("%d", &nums[i])) {
if (nums[i] == -) {
return ;
}
i++ ;
if (getchar() == '\n') {
break;
}
}
qsort(nums, i, sizeof(int), compare);
for (int j = ; j < i; j++) {
for (int k = j - ; k > -; k--) {
if (nums[k] * == nums[j]) {
count ++;
} else if(nums[k] * < nums[j]) {
break;
}
}
}
printf("%d\n", count);
}
return ;
}
//0ms 148kb g++ 930B

算法练习之:Doubles的更多相关文章

  1. 非阻塞同步算法与CAS(Compare and Swap)无锁算法

    锁(lock)的代价 锁是用来做并发最简单的方式,当然其代价也是最高的.内核态的锁的时候需要操作系统进行一次上下文切换,加锁.释放锁会导致比较多的上下文切换和调度延时,等待锁的线程会被挂起直至锁释放. ...

  2. STL笔记(6)标准库:标准库中的排序算法

    STL笔记(6)标准库:标准库中的排序算法 标准库:标准库中的排序算法The Standard Librarian: Sorting in the Standard Library Matthew A ...

  3. 【Java并发编程】9、非阻塞同步算法与CAS(Compare and Swap)无锁算法

    转自:http://www.cnblogs.com/Mainz/p/3546347.html?utm_source=tuicool&utm_medium=referral 锁(lock)的代价 ...

  4. 算法之路 level 01 problem set

    2992.357000 1000 A+B Problem1214.840000 1002 487-32791070.603000 1004 Financial Management880.192000 ...

  5. HashMap的结构算法及代码分析

    HashMap算是日常开发中最长用的类之一了,我们应该了解它的结构跟算法: 参考文章: http://blog.csdn.net/vking_wang/article/details/14166593 ...

  6. 算法(Algorithms)第4版 练习 2.2.23

    测试结果: 算法(Algorithms)第4版 练习 2.2.10 算法(Algorithms)第4版 练习 2.2.11(1) 算法(Algorithms)第4版 练习 2.2.11(2) 算法(A ...

  7. 排序算法-桶排序(Java)

    package com.rao.sort; import java.util.*; /** * @author Srao * @className BucketSort * @date 2019/12 ...

  8. 常用算法之排序(Java)

    一.常用算法(Java实现) 1.选择排序(初级算法) 原理:有N个数据则外循环就遍历N次并进行N次交换.内循环实现将外循环当前的索引i元素与索引大于i的所有元素进行比较找到最小元素索引,然后外循环进 ...

  9. JAVA实现BP神经网络算法

    工作中需要预测一个过程的时间,就想到了使用BP神经网络来进行预测. 简介 BP神经网络(Back Propagation Neural Network)是一种基于BP算法的人工神经网络,其使用BP算法 ...

随机推荐

  1. IDispose(), Finalize()

    C#  using 用法: 1. 作为指令,用于为命名空间创建别名或导入其他命名空间中定义的类型.(见例1-1) 2. 作为语句,用于定义一个范围,在此范围的末尾将释放对象.(见例1-2) (例子1- ...

  2. CentOS6.3挂载读写NTFS分区

    CentOS不像Fedora,默认是没有自动挂载NTFS的,而它可以利用NTFS-3G来实现挂载及读写. NTFS-3G 是一个开源的软件,可以实现 Linux.Free BSD.Mac OSX.Ne ...

  3. Oracle JDBC通过占位符可以查询可变长字段,不可查询固定长度字段

    这个问题在半年前,花了我两天的时间才解决,现在刚好想起来,顺便记录一下以防忘了. 通过jdbc,预编译的sql,无法查询到固定长度的字段. 用例子说话: 创建一个表,只有一个字段,长度为固定的char ...

  4. YII中URL地址美化

    URL地址美化:urlManager地址管理(通过程序来实现url的地址美化) 例如: 原地址:http://localhost/项目/app/index.php?r=控制器/方法 新地址:http: ...

  5. 20151224jquery学习笔记---cookie插件

    hello,祝自己平安夜快乐. Cookie 是网站用来在客户端保存识别用户的一种小文件.一般来用库可以保存用户登录信息.购物数据信息等一系列微小信息.一. 使用 cookie 插件官方网站: htt ...

  6. zoj2432 hdoj1423 最长公共上升子序列(LCIS)

    zoj2431  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2432 hdoj 1423 http://acm.hdu. ...

  7. java中数据类型及运算符的注意事项

    数据类型: boolean 类型数据只允许取值true 或 false(不可以使用0 或非0的整数来代替true和false,区分于C语言). char:Java中用" \u四位十六进制的数 ...

  8. JavaScript高级程序设计(五): js的关键字instanceof和typeof使用

    JavaScript中instanceof和typeof 常用来判断一个变量是否为空,或者是什么类型的.但它们之间还是有区别的: 一.typeof 1.含义:typeof返回一个表达式的数据类型的字符 ...

  9. sql查询结果集根据指定条件排序的方法

    oracle认为 null 最大. 升序排列,默认情况下,null值排后面. 降序排序,默认情况下,null值排前面. 有几种办法改变这种情况: (1)用 nvl 函数或decode 函数 将null ...

  10. ios专题 -block用法

    what is block Blocks are a language-level feature added to C, Objective-C and C++, which allow you t ...