如何显示用户使用的数量,即用户输入的时间

时间:2013-10-08 13:36:08

标签: c++

好的,使用循环时程序似乎很简单但是如果还有其他的话,那么可以用这些条件做到这一点 我已经尝试了,这是我的努力,但失败完全失败。

#include<iostream>
#include<conio.h>
int main(){
    using namespace std;
    int a,b,c,d,e,f,g,h,j,k,l;
    int c1=0;
    int c2=0;
    int c3=0;
    int c4=0;
    cout<<"please enter first number\n";
    cin>>a;
    cout<<"please enter second number\n";
    cin>>b;
    cout<<"please enter third number\n";
    cin>>c;
    cout<<"please enter fourth number\n";
    cin>>d;
    cout<<"please enter fifth number\n";
    cin>>e;
    cout<<"please enter sixth number\n";
    cin>>f;
    cout<<"please enter seventh number\n";
    cin>>g;
    cout<<"please enter eighth number\n";
    cin>>h;
    cout<<"please enter ninth number\n";
    cin>>j;
    cout<<"please enter tenth number\n";
    cin>>k;
    cout<<"please enter eleventh number\n";
    cin>>l;
    if(a==1)
    {
        c1=a+1;

    }
    else if (a==1 && b==1)
    {
        c2=a+b;
    }
    else if (a==1 && b==1 && c==1)
    {
        c3=a+b+c;
    }
    else if (a==4)
    {
        c4=a+1;
    }
    cout<<"no of 1's="<<c1;
getch();
}

我正在接受价值观并且在取值后我想表明1出现了多少时间

4 个答案:

答案 0 :(得分:3)

使用循环输入N个数字。当你阅读它们时(确保当然要检查错误),计算得到1的次数。

答案 1 :(得分:2)

要计算字母变量中有多少ones,您可以这样做:

int c1=0;

if(a==1)
{
    c1++;
}
if (b==1)
{
    c1++;
}

    ...

if (l==1)
{
    c1++;
}
cout<<"no of 1's="<<c1;

答案 2 :(得分:0)

#include<iostream>
#include<conio.h>
#include <array>
#include <unordered_map>
int main(){
    using namespace std;

    static const int cCount = 11;
    array<int, cCount> values;
    static const array<std::string, cCount> numbers = {
      "first",
      "second",

...

      "eleventh"}; 

    for (int = 0; i < cCount; ++i) {
      cout<<"please enter " << numbers[i] << " number\n";
      cin>> values[i];
    }  


    unordered_map<int, int> counts;
    for (int = 0; i < cCount; ++i) {
      ++counts[values[i]];
    }
    cout << "no of 1's=" << counts[1];
    getch();
}

答案 3 :(得分:-2)

哇,真是一团糟。

使用哈希表,键是用户输入,值是无符号的(此无符号将跟踪用户使用相应键输入值的次数)。每当用户输入一个值时,递增该键的值。然后,最后,返回存储在key =='1'的值。