如何在向量中的特定数字之后重新计算过程。

huangapple go评论76阅读模式
英文:

How to recount a process after a certain number in a vector

问题

假设我有一个年份数组。

int year[20];

计划是创建一个if条件并处理年份向量中的一个月份段。

我想要处理的段是从九月到二月。因此,我总是想要选择的年份范围是 (year >= 9 && year <= 2)

因此,我有两个向量。一个是年份,另一个是我用来计数的向量。

例如:

vector<int> year{1,2,3,5,10,2,10,12,11,12,2,2,3,5,8,2,8,12,8,12};
vector<int> list{1,2,3,2,1,1,2,3,2,1,5,3,2,5,6,5,3,2,5,6};

上述两个向量的大小都为20。我想要做的是统计我正在查看的每个年份段中的list向量的条目。

如果我们查看上面的两个向量,年份向量中的第一个月段将是:

从 year[0] = 1 到 year[1] = 2,计数为2。

接下来的段将是:

从 year[4] = 10 到 year[5] = 2。所以输出的条目将来自于向量 listlist[4] = 1list[5] = 1,总计为2。

在段操作完成后,我希望将计数重新赋值为零,以便从头开始并遍历整个year向量。

以下是我做的一些工作。我的问题是,我可以在if语句中创建段,但我试图在每个年份段完成后将计数归零。

#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
using namespace std;
int main()
{
    unsigned int count = 0;
    vector<int> year{1,2,3,5,10,2,10,12,11,12,2,2,3,5,8,2,8,12,8,12};
    vector<int> list;
  
    for (int x = 0; x < 20; x++)
    {
        list.push_back((1 + rand() % 4));
    }
  
    for (int j = 0; j <= 20; j++)
    {
        if (year[j] >= 9 && year[j] <= 2)
        {
            count++;
            cout << list[j] << "计数每个段的条目" << count << endl;
        }
    }
}

我想要选择的段的方式是,如果向量 year 中的条目,例如,1 = 一月,2 = 二月,满足九月到二月的条件。因此,每个段都会大于或等于9且小于3。

当我增加到向量 year 时,我找到一个满足条件的段,然后转到下一个再次满足条件的段。

每次我在if条件中处理完每个段的操作后,我想将计数重新赋值为零,以便在下一个段中重新计数。因为月份按升序排列,所以从示例 {11, 10} 中,将计算为1而不是2。段将是 {11}{10},而不是 {11, 10},其中计数元素条目为2。

英文:

Let's assume I have an array of years.

int year[20];

The plan is to make an if condition and work with a segment of the months in the year vector.

The segment I want to work with is from September to February. Therefore the range of year I always want to pick is (year &gt;=9 &amp;&amp; year &lt;3)

Therefore I have two vectors. One is for the year, and another one is for that I count to count the entry.

For example:

vector &lt;int&gt; year{1,2,3,5,10,2,10,12,11,12,2,2,3,5,8,2,8,12,8,12};
vector &lt;int&gt; list{1,2,3,2,1,1,2,3,2,1,5,3,2,5,6,5,3,2,5,6};

the size of both the above vectors is 20. What I want to do is to count the vector entry from list in each year segment I'm looking into.

If we look at both vectors above, the first month segment in year vector would be:
from year[0] =1 to year[1] =2. and the count is 2.

The next segment would be: from year[4] =10 to year[5] =2. So the print out entries would be from the vector list: list[4]=1, and list[5] =1. and the total count is 2.

After the segment work is done I want to reassign the count to zero to start it again and iterate through out the whole vector of year.

Here is the some work I did. My problem is that, I can make the segment in the if statement, but I'm trying to assign the count =0 each time each year segment is complete.

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;cstdlib&gt;
#include &lt;vector&gt;
using namespace std;
int main()
{
    unsigned int count =0;
     vector &lt;int&gt; year{1,2,3,5,10,2,10,12,11,12,2,2,3,5,8,2,8,12,8,12};
    vector &lt;int&gt; list;
  
    for (int x =0; x&lt;20; x++)
    {
        //cout &lt;&lt; &quot;random number&quot; &lt;&lt; (1+ rand()%12)&lt;&lt; endl;
        list.push_back((1+ rand()%4));
    }
  
    for (int j=0; j&lt;=20; j++)
    {
        if (year[j] &gt;=9 &amp;&amp; year[j]&lt;3)
        {
            count++;
            cout &lt;&lt;list[j]&lt;&lt; &quot;counting the entry of each segment&quot;&lt;&lt; count&lt;&lt;endl;
        }
    }
}

The way I want to select the segment if the entries in the vector year for example, 1= January, 2 = February satisfies the September to February conditions. So each segment would be greater or equal to 9 and less than 3.
As I increment to the vector year, I find a segment that satisfies the condition and goes to the next segment that satisfies the condition again.

Each time I' done with the operation in if condition with each segment, I want to assign the count =0, so I can recount how many entries in the next segment.

Since the month goes like in ascending order, from the example {11, 10}
will count as 1 with two segments instead of two. The segments will be {11}, and {10}, instead of {11, 10} where it counts the elements entry =2.

答案1

得分: 2

我建议查看(year[j]+9)%12而不是直接查看year[j]
一切≥ 6都在你的范围内(6,7,8,9,10,11),即不需要的其他6个月(0,1,2,3,4,5)在外面。这也使得检测新段落的开始变得更容易。

year[j]  (year[j]+9)%12    wanted
 1       10                 x
 2       11                 x
 3        0
 4        1
 5        2
 6        3
 7        4
 8        5
 9        6                 x
10        7                 x
11        8                 x
12        9                 x

你的向量中的值变为:

          wanted   reset
 1  10    x        
 2  11    x
 3   0             r
 5   2
10   7    x    
 2  11    x
10   7    x        r
12   9    x
11   8    x        r
12   9    x
 2  11    x
 2  11    x
 3   0             r
 5   2
 8   5
 2  10    x
 8   5             r
12   9    x
 8   5             r
12   9    x

这显示了您希望重置计数并开始处理新段落的位置,即每当(year[j]+9)%12不大于前一个索引时。

公式(year[j]+9)%12的工作方式是通过使用+9部分来移动您查看的“虚拟月份”,并确保结果仍在十二个月的0-11范围内,通过%12部分。即,“1”(看作“10”)被视为比“12”(看作“9”)高。因此,从12 (9)到1 (10)不会触发重置,但从2 (11)到3 (0)会触发重置,因为明显段落结束了。此外,从2 (11)到10 (7)的转换也会触发重置;这是必要的,因为(3-8)之间发生了变化,因此需要开始一个新的段落,即使10大于2。

英文:

I propose to look at (year[j]+9)%12 instead of directly at year[j].
Everything >= 6 is in your range (6,7,8,9,10,11) , i.e. the unwanted other 6 months (0,1,2,3,4,5) are outside. That also makes detecting the start of a new segment much easier.

year[j]  (year[j]+9)%12    wanted
 1       10                 x
 2       11                 x
 3        0
 4        1
 5        2
 6        3
 7        4
 8        5
 9        6                 x
10        7                 x
11        8                 x
12        9                 x

The values in your vector become:

          wanted   reset
 1  10    x        
 2  11    x
 3   0             r
 5   2
10   7    x    
 2  11    x
10   7    x        r
12   9    x
11   8    x        r
12   9    x
 2  11    x
 2  11    x
 3   0             r
 5   2
 8   5
 2  10    x
 8   5             r
12   9    x
 8   5             r
12   9    x

This shows where you want to reset the count and start working on a new segment, which is whenever (year[j]+9)%12 is not greater than at previous index.

The formula (year[j]+9)%12 works by shifting the "virtual month" you are looking at, by using the +9 part and making sure that the result still is in the 0-11 range for twelve months, by the %12 part. I.e. that "1" (seen as "10") is seen higher than "12" (seen as "9"). So that going from 12 (9) to 1 (10) does not trigger the reset, but going from 2 (11) to 3 (0) does trigger the reset, because the segment obviously ended. Also going from , 2 (11) to 10 (7), triggers the reset; which is needed, because (3-8) occur in between and hence a new segment needs to be started, even though 10 is higher than 2.

huangapple
  • 本文由 发表于 2020年1月3日 13:51:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/59573778.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定