如何计算数字x的频率

时间:2019-08-12 04:32:42

标签: c++ counter

我想打印第一个数到4的第一个数字,例如,我具有此随机函数,我想查看达到4次的第一个数字。因此这是第一个打印出4次的数字。例如:

this.Code = this.project1.code;
this.CustomerName = this.project1.name;
this.Id = this.project1.id;
this.projectcode = this.project1.projectCode;
console.log(this.projectcode)
this.projectId = this.project1.projectId;
this.Sortcode = this.project1.sortCode;
this.Contact = this.project1.contactPerson;
this.Address = this.project1.address1;
this.Address2 = this.project1.address2;
this.Country = this.project1.country;
this.Postalcode = this.project1.postalCode;
this.Phone = this.project1.phone;
this.Fax = this.project1.fax;
this.Checkbox = this.project1.isActive;
this.Description = this.project1.description;

但是如果它是1-1000,而不仅仅是1-3,我该怎么办?

我想这样做,但是要使用随机函数-该数字的计数是4的第一个数字会打印该数字-

<ion-col>
    <select [(ngModel)]="projectcode" (ngModelChange)="projectLoad()">
        <option *ngFor="let project of projectlist">{{ project.projectCode }}</option>
    </select>
</ion-col>

然后我主要要做的是第一个达到4次的数字打印此数字。

我试图做这样的事情:

this.http.get('http://localhost:2401/api/ProjectAPI')
  .map(res => (res))
  .subscribe(data => {
    this.projectlist = data.json();
    let newObj = this.projectlist.find(x => x.projectCode == this.projectcode);
    this.projectcode = newObj.projectCode;
    console.log(this.projectcode);
  }, err => { console.log(err) });
}

projectLoad() {
  let newObj = this.project1.find(x => x.projectName == this.Projectname);
  this.projectId = newObj.id;
}

2 个答案:

答案 0 :(得分:4)

您将为此使用一个集合(例如向量),而不是使用一千个单独的变量:-)

首先,如果要使用1..3范围内的随机数,则可以使用(rand() % 3) + 1。但是,您可以使用范围0..n-1,而不是1..n,并且只需在循环后调整值即可。

第一步是将每个数字的计数创建并初始化为零:

const int SZ = 1000;
std::vector<int> count(SZ, 0);

然后,您的循环仅生成随机数并调整相关计数,直到其中之一达到目标值为止:

int num;
for (;;) { // infinite loop
    num = rand() % SZ;
    ++count[num];
    if (count[num] == 4)
        break; // exit infinite loop when one of them reaches four.
}

然后,您只需输出首先达到四个的那个。请注意,由于我们正在做0..999,因此我们将其映射1.1000

std::cout << ++num << " reached a count of four first\n";

完整的程序可以显示以下内容:

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

int main() {
    srand(time(nullptr));

    const int SZ = 1000;
    std::vector<int> count(SZ, 0);

    int num;
    for (;;) { // infinite loop
        num = rand() % SZ;
        ++count[num];
        if (count[num] == 4)
            break; // exit loop when one of them reaches four.
    }

    std::cout << ++num << " reached a count of four first\n";
}

其中的一个示例运行(确保有一个延迟,以便随机数生成器获得不同的种子):

>> for i in {1..10} ; do sleep 1 ; ./testprog ; done )
296 reached a count of four first
520 reached a count of four first
205 reached a count of four first
239 reached a count of four first
822 reached a count of four first
260 reached a count of four first
421 reached a count of four first
444 reached a count of four first
21 reached a count of four first
92 reached a count of four first

答案 1 :(得分:-1)

最后一个答案超出范围。我的答案是仅使用数组

while(1) {
        int f = rand() % 3;
        count[f]++;
        cout << f << endl;
        if (count[f] == 4) {
            cout <<"4 times" <<f;
            break;
        }
    }