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. 职场PPT达人装酷的13条秘诀

    对<说服力-让你的PPT会说话>读者调查显示,88.8%的白领认为“做出漂亮的幻灯片对晋升有帮助”,99.9%的白领一致认为职场装酷神器排行榜第一位是PPT,甚至有位程序员说哥最牛的编程环 ...

  2. E: Sub-process /usr/bin/dpkg returned an error code (1)

    E: Sub-process /usr/bin/dpkg returned an error code (1) 错误描述 dpkg: error processing archive /var/cac ...

  3. 【转】Android 应用测试总结

    前提所有的功能分支已完成 启动:1. 启动入口:桌面正常启动,最近运行启动,所有程序列表中启动,锁屏快捷启动2. 其他入口:从其他程序开启应用,从外部以文件形式打开应用(如果有)3. 退回:从其他程序 ...

  4. 企业级应用框架(五)IOC容器在框架中的应用

    前言 在上一篇我大致的介绍了这个系列所涉及到的知识点,在本篇我打算把IOC这一块单独提取出来讲,因为IOC容器在解除框架层与层之间的耦合有着不可磨灭的作用.当然在本系列前面的三篇中我也提供了一种基于反 ...

  5. 介绍SmartUpload很好的网站

    附带链接:http://www.cnblogs.com/elleniou/archive/2012/09/24/2700583.html

  6. NUnit使用详解(二)

    转载:http://hi.baidu.com/grayworm/item/39aa11c5d9375d56bdef6990 五:常用断言 在NUnit中,断言是单元测试的核心.NUnit提供了一组丰富 ...

  7. mvc给html扩展方法:

    mvc给html扩展方法: 注意:扩展方法和所在的类都必须是 public static如果在页面直接使用新扩展的方法,需要web.config里把Web.Helper名称命名空间加上,页面才能访问到 ...

  8. 那天有个小孩跟我说LINQ(二)转载

    1  LINQ TO Objects续(代码下载)      新建项目 linq_Ch2控制台程序,新建一个Entity文件夹    1.1 学生成绩查询(练习Join)         有三张表如下 ...

  9. ArcGIS Runtime SDK for WPF已不更新,后续将被ArcGIS Runtime SDK for .NET取代

    ArcGIS Runtime SDK 10.2.5 for WPF is now available! by mbranscomb and Rex Hansen on January 27, 2015 ...

  10. Java并发编程:进程和线程之由来__进程让操作系统的并发性成为可能,而线程让进程的内部并发成为可能

    转载自海子:http://www.cnblogs.com/dolphin0520/p/3910667.html Java多线程基础:进程和线程之由来 在前面,已经介绍了Java的基础知识,现在我们来讨 ...