查找数字中的特定重复数字

时间:2015-10-19 12:19:36

标签: c++

我正在尝试编写一个程序来计算数字中52315 的数量并显示它。因此,如果用户输入:

Yes, there are '5's and they're 2

然后程序应该ouptut:

{
    int n,m;
    int count = 0;
    cout << "Enter an: ";
    cin >> n;

    int *arr;
    arr = new int[count];

    // Getting digits from number
    while (n > 0)
    {
        count++; 
        m = n%10;  
        n = n/10;

    //Trying to put every digit in an array

        for (int i = 0; i<count; i++)
        {
            cin>>arr[i];
            arr[i] = m;
            cout << arr[i];
        }
        int even = 5;
    //Trying to see if there's a digit 5 in inputed number and how many if so.
        for (int j = 0; j<count; j++)
        {
            if (arr[j]==even)
            {
                cout << "Yes, there's a digit '5' " << endl;
                s++;
            }
        }



        cout << "Count of '5's : " << even;
        delete[] arr;
    }



     return 0;
}

这是我的代码,但它有问题。

create: function (createEvent) {

    var typeOfEventID = $("#selectEvent").val();
    var usernameID = $("#selectUsername").val();
    var dataStartTemp = $("#dataStart").val();
    var dataEndTemp = $("#dataEnd").val();
    var note = $("#note").val();

    var res = $("#customViewScheduler").data("kendoScheduler");
    var res1 = res.resources[1].dataSource.data();

    var dataStart = convertToJSONDate(dataStartTemp);
    var dataEnd = convertToJSONDate(dataEndTemp);

    var changeSet = [];
    var id = 0;

    usernameID.forEach(function (userID) {
        typeOfEventID.forEach(function (eventID) {
            var titletemp = $.grep(res1, function (elem) {
                if (elem.TypeOfEventID == eventID) {
                    return true;
                }
            })

            if (titletemp.length > 0) {
                note = titletemp[0].title;
            }

            var entityChange = {};
            entityChange.Id = id;
            entityChange.Entity = {
                '__type': "Events:#BlahBlahWeb",
                    'UsernameID': userID,
                    'TypeOfEventID': eventID,
                    'startDate': dataStart,
                    'endDate': dataEnd,
                    'Title': note
            };
            entityChange.Operation = 2;
            changeSet.push(entityChange);
            id++
        })
    })

    var changesetPayload = JSON.stringify({
        "changeSet": changeSet
    });

    //Create jQuery ajax request
    var Params = {}
    Params.type = "POST";
    Params.url = "./../Services/BlahBlahWeb-BlahDomainService.svc/JSON/SubmitChanges";
    Params.dataType = "json";
    Params.data = changesetPayload;
    Params.contentType = "application/json";
    Params.error = function (httpRequest, textStatus, errorThrown) {
        //SendErrorByEmail(errorThrown, httpRequest.responseText)
    }
    Params.success = function (data) {
        //createEvent.success(data);
        var scheduler = $("#customViewScheduler").data("kendoScheduler");
        var elem = tratanewelem(data.SubmitChangesResult[0].Entity)
        scheduler.dataSource.read();
    }
    //Make the ajax request
    $.ajax(Params);
},

2 个答案:

答案 0 :(得分:1)

for (int i = 0; i<count; i++)
{
    cin >> arr[i];
}

您尝试使用另一个用户输入而不是现有用户输入来填充数组。

你也可以在没有数组的情况下完成:

int count = 0;
int n;
cin >> n;

do {
    if (n%10 ==5) count++;
    n /= 10;
} while (n);

cout << "Count of '5's : " << count;

答案 1 :(得分:0)

这应该为每个号码都这样做。 如果您只想知道像5这样的特殊数字,只需删除for循环和打印计数[theNumberYouWantToKnow]。

#define BASE 10

void output() {
    int givenNumber;

    cout << "Please enter a number: ";
    cin >> givenNumber;

    int count[BASE] = { 0 };

    while(givenNumber > 0) {
        int digit = givenNumber % BASE;
        givenNumber /= BASE;
        count[digit]++;
    }

    for(int i = 0; i < BASE; i++) {
        cout << "Found the number " << i << " " << count[i] << " times." << endl;
    }
}