How many items that are double some other item

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.

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


解题思路:
用vector容器存放输入的数字,然后求值时,用双重循环逐个处理
细节处理:
注意最后不要将最后的0放入向量,以免影响处理,还有就是每重
循环后清空向量

代码:

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
    vector<double> a; double c;
    while(1)
    {
        int sum=0;
        while(cin>>c)
        {
            if(c==-1)
            {a.push_back(c);break;}
            if(c==0) break;
            a.push_back(c);
        }
        if(c==-1) break;
        int i,j;
        for(i=0;i<a.size();i++)
        for(j=0;j<a.size();j++)
        if(a[j]*2==a[i]) sum++;
        cout<<sum<<endl;
        a.clear();
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Here is a possible implementation of the class and main program in C++: ```c++ #include <iostream> #include <string> using namespace std; class ShopItem { public: string name; double price; int stock; void load(string n, double p, int s) { name = n; price = p; stock = s; } void display() { cout << name << " - " << price << " - " << stock << endl; } void sell(int quantity) { if (quantity <= stock) { stock -= quantity; cout << "Number of " << name << " items in the shop is now " << stock << endl; } else { cout << "Not enough " << name << " items in stock." << endl; } } }; int main() { const int NUM_ITEMS = 3; ShopItem items[NUM_ITEMS]; // Load data items[0].load("Bread", 3.19, 120); items[1].load("Milk", 2.49, 80); items[2].load("Eggs", 1.99, 200); // Display data for (int i = 0; i < NUM_ITEMS; i++) { items[i].display(); } // Sell items string item_name; int quantity; cout << "Enter item that was just sold: "; getline(cin, item_name); cout << "How many items were sold: "; cin >> quantity; for (int i = 0; i < NUM_ITEMS; i++) { if (items[i].name == item_name) { items[i].sell(quantity); break; } } return 0; } ``` The `ShopItem` class has three variables: `name` (string), `price` (double), and `stock` (integer), as well as three methods: `load`, `display`, and `sell`. The `load` method sets the values of the variables for each object, the `display` method prints out the values, and the `sell` method updates the `stock` variable if there are enough items in stock. In the `main` function, an array of `ShopItem` objects is created with a size of `NUM_ITEMS`. The data is loaded into each object using the `load` method. The data is displayed using the `display` method. The user is prompted to enter an item name and a quantity of items sold, and the `sell` method is called on the corresponding object to update the stock.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值