算法练习之:Doubles
2024-09-01 06:54:40
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
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
最新文章
- Ngnix下安装python2.7
- 织梦cms、帝国cms、PHPcms优缺点解析
- GEF: 图形拖拽处理
- Android抓包方法(二)之Tcpdump命令+Wireshark
- ubunto安装pycharm
- memache session
- Javascript Date类常用方法详解
- C# Lambda Expressions 简介
- WPF之小动画三
- HTML5 canvas标签绘制正三角形 鼠标按下点为中间点,鼠标抬起点为其中一个顶点
- OJ python答题结果";返回非零";
- WKE——Webkit精简的纯C接口的浏览器
- 《JS权威指南学习总结--6.6属性getter和setter》
- 【学习总结】GirlsInAI ML-diary day-15-读/写txt文件
- 如何在Idea提交代码到Github上
- [C]内存管理、内存泄露、堆栈
- Java 微服务框架选型 Dubbo 和 Spring Cloud
- 2017/05/21 java 基础 随笔
- 远程阿里云window服务器报错身份验证错误
- 【BZOJ2658】[Zjoi2012]小蓝的好友(mrx) 平衡树维护笛卡尔树+扫描线