请做我的作业:对销售数据进行排序. [英] Please do my homework: sort sales data.

查看:79
本文介绍了请做我的作业:对销售数据进行排序.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个程序,可以每月保存10年的销售数据.该数组应具有大小为12的月份索引.给定此数据,请按排序顺序计算一年中销售最佳的月份.

输出要求:
1990 1991 1992 1993 1994 1994月份
1 10,000 9,000 8,000 10,000 10,000 10,000
2 20,000 3,000 3,000 40,000 20,000 20,000
3 30,000 4,000 5,000 30,000 10,000 20,000
4 40,000 5,000 5,000 20,000 10,000 20,000
5 50,000 6,000 5,000 20,000 20,000 30,000
6 10,000 3,000 5,000 20,000 20,000 30,000
7 10,000 2,000 5,000 20,000 10,000 20,000
8 10,000 5,000 4,000 20,000 20,000 10,000
9 10,000 4,000 4,000 20,000 20,000 20,000
10 10,000 7,000 4,000 10,000 20,000 20,000
11 10,000 4,000 4,000 10,000 20,000 40,000
12 10,000 4,000 4,000 40,000 30,000 30,000
总计220,000 116,000 56,000 260,000 210,000 270,000

最佳销售年份是 1995年12月
总销售额为 P 270,000

A program that keeps sales data for 10 years by month. The array should have a month index of size 12. Given this data, compute by sorted order the months of the year for which sales are best.

Output Requirment:
Month 1990 1991 1992 1993 1994 1995
1 10,000 9,000 8,000 10,000 10,000 10,000
2 20,000 3,000 3,000 40,000 20,000 20,000
3 30,000 4,000 5,000 30,000 10,000 20,000
4 40,000 5,000 5,000 20,000 10,000 20,000
5 50,000 6,000 5,000 20,000 20,000 30,000
6 10,000 3,000 5,000 20,000 20,000 30,000
7 10,000 2,000 5,000 20,000 10,000 20,000
8 10,000 5,000 4,000 20,000 20,000 10,000
9 10,000 4,000 4,000 20,000 20,000 20,000
10 10,000 7,000 4,000 10,000 20,000 20,000
11 10,000 4,000 4,000 10,000 20,000 40,000
12 10,000 4,000 4,000 40,000 30,000 30,000
Total 220,000 116,000 56,000 260,000 210,000 270,000

The Best Sales is the month of DECEMBER 1995
Gross sales is P 270,000

推荐答案

因为OriginalGriff的答案在 C#中,并且您的问题已被标记 C ++ ,我会尽力帮助您.

在C ++中,大多数必需的例程都在标准C ++库中构建,您只需要使用它们即可.

1 将数据保存在std::vector中,例如:
As OriginalGriff''s answer is in C# and your question is tagged C++, I will try to put you on the way.

In C++ most of the necessary routines are built in the Standard C++ Library, you just have to use them.

1 Hold your data in a std::vector, for instance:
#include <vector>
typedef std::vector<int> Sales_t;
typedef Sales_t::iterator Sales_it;
const int YearStart = 1990;
int sales[] =
{
    10000, 20000, 30000, 40000, 50000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, // 1990
    9000, 3000, 4000, 5000, 6000, 3000, 2000, 5000, 4000, 7000, 4000, 4000,             // 1991
    // etc ...
};
Sales_t Sales(sales, sales + sizeof(sales) / sizeof(int));


2 现在,您必须将Sales_it迭代器转换为年和月的值,例如:


2 Now you have to convert Sales_it iterators to year and months values for instance:

#include <algorithm>
int GetYear(Sales_it it)
{
    return YearStart + std::distance(Sales.begin(), it) / 12;
}
int GetMonth(Sales_it it)
{
    return std::distance(Sales.begin(), it) % 12;
}
// TODO
Sales_it Get_it(int year, int month);


3 现在使用标准C ++库算法执行任务,如下所示:


3 Now use the Standard C++ Library algorithms to execute your tasks, as:

#include <numeric>
#include <iostream>
int main()
{
    int totalSales = std::accumulate(Sales.begin(), Sales.end(), 0);

    Sales_it it = std::max_element(Sales.begin(), Sales.end());
    std::cout << "Maximum sale: " << *it << " Year: " << GetYear(it) << " ,Month: " << GetMonth(it) << std::endl;
}


欢呼声,
AR


cheers,
AR


我们(通常)不为您做作业,所以请尝试自己开发程序,并在遇到困难时在此处发布特定问题.并且,请在发布前阅读FAQ.
:)
We (usually) don''t do the homework for you, so please, try to develop the program yourself and post here specific questions when you''re stuck. And please, please, read the FAQ, before posting.
:)


我没有给您代码:您的讲义将涵盖该内容.
但是,我将为您提供所需的步骤.在进行下一阶段之前,可能首先应该执行并检查每个阶段.

1)声明您的销售数据:您需要在某个地方存储销售信息,以便您的代码可以访问它.我会使用Dictionary< int,int []>这样我就可以将年份用作关键.
I''m not giving you the code: your lecture notes will cover that.
But, I will give you the steps you need to go though. It may be worth implementing and checking each stage first, before moving to the next.

1) Declare your sales data: You need somewhere to store the sales information, so your code can access it. I would use a Dictionary<int, int[]> so that I could use the year as a key.
Dictionary<int, int[]> salesByYear = new Dictionary<int, int[]> {
    {1990, new int[] { 10000, 20000, 30000, 40000, 50000, 10000, 10000, 10000, 10000, 10000, 10000, 10000}},
    {1991, new int[] { 9000, 3000, 4000, 5000, 6000, 3000, 2000, 5000, 4000, 7000, 4000, 4000}}
};

助您入门!
2)生成总计:再次,您需要存储总计.同样,我会使用字典.将总计填入您的代码中-逐年循环销售,然后将它们加在一起.将总和放入该年的字典条目中.
3)查找最佳销售年份:循环浏览总计中的每一年,并找出最高的年份.
4)然后,您需要将月份数字按顺序排序,并带有每月的销售额.

祝你好运!如果您确实卡在某个特定的部分上,请再次询问,但是请尝试自己动手!

Gets you started!
2) Generate your totals: Again, you need to store the totals. Again, I would use a Dictionary. Fill the totals in your code - loop though each years sales, and add them together. Put the sum in a dictionary entry for that year.
3) Find the best sales year: Loop through each year in your totals, and find which is the highest.
4) You then need to sort the month numbers into order, with the sales value per month.

Good luck! If you do get stuck on some particular part, ask again, but please, do try to do it yourself!


这篇关于请做我的作业:对销售数据进行排序.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆