C ++ da hisoblagich bilan aniq sonning yuzaga kelishini toping

Menda juda ko'p sonli raqam bor va shu bilan belgilangan raqamlarning yuzaga kelishini topmoqchiman. Menimcha, taymer yordamida ishlaydi. Mening kodim quyidagicha:

#include 
using namespace std;

int main( )
{
int number;
int n1;
int n2;
int n3;
int n4;
int n5;
int n6;
int n7;
int n8;
int n9;
int n10;
int digit;
int digitCounter = 0;


    cout << "Please enter a number between 100 to 2000000000." << endl; 
    cin >> number;

    cout << "Please enter a digit that you want to find the number of occurances." << endl;
    cin >> digit;

    if (number > 0)
    { 
     n1 = number % 10;
     n2 = (number/10) % 10;
     n3 = (number/100) % 10;
     n4 = (number/1000) % 10;
     n5 = (number/10000) % 10;
     n6 = (number/100000) % 10;
     n7 = (number/1000000) % 10;
     n8 = (number/10000000) % 10;
     n9 = (number/100000000) % 10;
     n10 = (number/100000000) % 10;

     if (n1 == digit)
     { digitCounter++;}

     else if (n2 == digit)
     { digitCounter++;}

     else if (n3 == digit)
     { digitCounter++;}

     else if (n4 == digit)
     { digitCounter++;}

     else if (n5 == digit)
     { digitCounter++;}

     else if (n6 == digit)
     { digitCounter++;}

     else if (n7 == digit)
     { digitCounter++;}

     else if (n8 == digit)
     { digitCounter++;}

     else if (n9 == digit)
     { digitCounter++;}

     else if (n10 == digit)
     { digitCounter++;}

     cout<< "The total number of occurances of " << digit << " in " << number <<" is "<< endl;

     }
        else

        cout<< "You have entered an invalid number."<

Ammo hisoblagich ishlamayapti. Kimdir nopok noto'g'ri maslahat berishi mumkinmi? Har qanday yordam, albatta, minnatdor, rahmat.

0
Nima uchun bu narsani faqat mag'lubiyatga aylantira olmaysiz va mag'lubiyatga aylantirmoqdasiz?
qo'shib qo'ydi muallif Roope, manba
Nima uchun bu narsani faqat mag'lubiyatga aylantira olmaysiz va mag'lubiyatga aylantirmoqdasiz?
qo'shib qo'ydi muallif Roope, manba
Nima uchun bu narsani faqat mag'lubiyatga aylantira olmaysiz va mag'lubiyatga aylantirmoqdasiz?
qo'shib qo'ydi muallif Roope, manba
Buni amalga oshirish uchun mag'lubiyatni ishlata olmaymiz, modni ishlatishimiz mumkin. rahmat
qo'shib qo'ydi muallif herblackcat, manba
Buni amalga oshirish uchun mag'lubiyatni ishlata olmaymiz, modni ishlatishimiz mumkin. rahmat
qo'shib qo'ydi muallif herblackcat, manba
Buni amalga oshirish uchun mag'lubiyatni ishlata olmaymiz, modni ishlatishimiz mumkin. rahmat
qo'shib qo'ydi muallif herblackcat, manba

12 javoblar

buni sinab ko'ring

#include 

int main(int argc, char **argv) {

  unsigned long long large(0);

  int digitToFind(0);

  std::cout << "enter a large number [100 to 2000000000]" << std::endl;
  std::cin >> large;

  if (large < 100 || large > 2000000000) {
    std::cout << "invalid input." << std::endl;
    return -1;
  }

  std::cout << "enter the digit to find" << std::endl;
  std::cin >> digitToFind;

  if (digitToFind < 0 || digitToFind > 9) {
    std::cout << "invalid input." << std::endl;
    return -1;
  }

  std::size_t counts[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

  while (large > 0) {
    int rem = large % 10;

    counts[rem]++;

    large /= 10;
  }

  std::cout << "number of occurrances of " << digitToFind << " is "
            << counts[digitToFind] << std::endl;

  std::cout << "press enter to continue" << std::endl;
  std::cin.get();
  return 0;
}
0
qo'shib qo'ydi

buni sinab ko'ring

#include 

int main(int argc, char **argv) {

  unsigned long long large(0);

  int digitToFind(0);

  std::cout << "enter a large number [100 to 2000000000]" << std::endl;
  std::cin >> large;

  if (large < 100 || large > 2000000000) {
    std::cout << "invalid input." << std::endl;
    return -1;
  }

  std::cout << "enter the digit to find" << std::endl;
  std::cin >> digitToFind;

  if (digitToFind < 0 || digitToFind > 9) {
    std::cout << "invalid input." << std::endl;
    return -1;
  }

  std::size_t counts[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

  while (large > 0) {
    int rem = large % 10;

    counts[rem]++;

    large /= 10;
  }

  std::cout << "number of occurrances of " << digitToFind << " is "
            << counts[digitToFind] << std::endl;

  std::cout << "press enter to continue" << std::endl;
  std::cin.get();
  return 0;
}
0
qo'shib qo'ydi

buni sinab ko'ring

#include 

int main(int argc, char **argv) {

  unsigned long long large(0);

  int digitToFind(0);

  std::cout << "enter a large number [100 to 2000000000]" << std::endl;
  std::cin >> large;

  if (large < 100 || large > 2000000000) {
    std::cout << "invalid input." << std::endl;
    return -1;
  }

  std::cout << "enter the digit to find" << std::endl;
  std::cin >> digitToFind;

  if (digitToFind < 0 || digitToFind > 9) {
    std::cout << "invalid input." << std::endl;
    return -1;
  }

  std::size_t counts[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

  while (large > 0) {
    int rem = large % 10;

    counts[rem]++;

    large /= 10;
  }

  std::cout << "number of occurrances of " << digitToFind << " is "
            << counts[digitToFind] << std::endl;

  std::cout << "press enter to continue" << std::endl;
  std::cin.get();
  return 0;
}
0
qo'shib qo'ydi

Ba'zi namunalar kodlari yana bir kamchilikni o'z ichiga oladi: agar raqam kichik bo'lsa, biz zarur bo'lganidan ko'ra ko'proq nolga ega bo'lamiz ... Bu sonning mutlaqo katta bo'lishi mumkinligini tasavvur qila olmaymiz. Bu Python-da har bir son uchun ishlaydi, chunki men hozir Python uchun ko'proq ishlatilgandim, chunki uni C ga aylantirish oson.

N = 1230533007
digitToFind = 3
digitCount = 0

while N > 0:
    d = N % 10
    if d == digitToFind:
        digitCount += 1
    N //= 10

print digitCount
0
qo'shib qo'ydi

Ba'zi namunalar kodlari yana bir kamchilikni o'z ichiga oladi: agar raqam kichik bo'lsa, biz zarur bo'lganidan ko'ra ko'proq nolga ega bo'lamiz ... Bu sonning mutlaqo katta bo'lishi mumkinligini tasavvur qila olmaymiz. Bu Python-da har bir son uchun ishlaydi, chunki men hozir Python uchun ko'proq ishlatilgandim, chunki uni C ga aylantirish oson.

N = 1230533007
digitToFind = 3
digitCount = 0

while N > 0:
    d = N % 10
    if d == digitToFind:
        digitCount += 1
    N //= 10

print digitCount
0
qo'shib qo'ydi

Ba'zi namunalar kodlari yana bir kamchilikni o'z ichiga oladi: agar raqam kichik bo'lsa, biz zarur bo'lganidan ko'ra ko'proq nolga ega bo'lamiz ... Bu sonning mutlaqo katta bo'lishi mumkinligini tasavvur qila olmaymiz. Bu Python-da har bir son uchun ishlaydi, chunki men hozir Python uchun ko'proq ishlatilgandim, chunki uni C ga aylantirish oson.

N = 1230533007
digitToFind = 3
digitCount = 0

while N > 0:
    d = N % 10
    if d == digitToFind:
        digitCount += 1
    N //= 10

print digitCount
0
qo'shib qo'ydi

Agar boshqa bo'lsa, agar bo'lmasa. Endi esa, siz faqat bitta qaror bayonotidan o'tasiz. Biror o'yinni topsangiz, siz tashqariga chiqasiz.

#include 
using namespace std;

int main()
{
    int number;
    int n1;
    int n2;
    int n3;
    int n4;
    int n5;
    int n6;
    int n7;
    int n8;
    int n9;
    int n10;
    int digit;
    int digitCounter = 0;


    cout << "Please enter a number between 100 to 2000000000." << endl;
    cin >> number;

    cout << "Please enter a digit that you want to find the number of occurances." << endl;
    cin >> digit;

    if (number > 0)
    {
        n1 = number % 10;
        n2 = (number/10) % 10;
        n3 = (number/100) % 10;
        n4 = (number/1000) % 10;
        n5 = (number/10000) % 10;
        n6 = (number/100000) % 10;
        n7 = (number/1000000) % 10;
        n8 = (number/10000000) % 10;
        n9 = (number/100000000) % 10;
        n10 = (number/100000000) % 10;

        if (n1 == digit)
        {
            digitCounter++;
        }

        if (n2 == digit)
        {
            digitCounter++;
        }

        if (n3 == digit)
        {
            digitCounter++;
        }

        if (n4 == digit)
        {
            digitCounter++;
        }

        if (n5 == digit)
        {
            digitCounter++;
        }

        if (n6 == digit)
        {
            digitCounter++;
        }

        if (n7 == digit)
        {
            digitCounter++;
        }

        if (n8 == digit)
        {
            digitCounter++;
        }

        if (n9 == digit)
        {
            digitCounter++;
        }

        if (n10 == digit)
        {
            digitCounter++;
        }

        cout << "The total number of occurances of " << digit << " in " << number << " is " << digitCounter << endl;

    }
    else

        cout << "You have entered an invalid number." << endl;



    system("pause");
    return 0;

}
0
qo'shib qo'ydi
Agar bittagina va bitta tugma bo'lishi kerak
qo'shib qo'ydi muallif Slava, manba
Men bunga qo'shilaman, bu muammoni yaxshiroq hal qiladi. Men faqat berilgan echim bilan nima noto'g'ri bo'lgan savolga javob berishga urindim.
qo'shib qo'ydi muallif BenG, manba

Agar boshqa bo'lsa, agar bo'lmasa. Endi esa, siz faqat bitta qaror bayonotidan o'tasiz. Biror o'yinni topsangiz, siz tashqariga chiqasiz.

#include 
using namespace std;

int main()
{
    int number;
    int n1;
    int n2;
    int n3;
    int n4;
    int n5;
    int n6;
    int n7;
    int n8;
    int n9;
    int n10;
    int digit;
    int digitCounter = 0;


    cout << "Please enter a number between 100 to 2000000000." << endl;
    cin >> number;

    cout << "Please enter a digit that you want to find the number of occurances." << endl;
    cin >> digit;

    if (number > 0)
    {
        n1 = number % 10;
        n2 = (number/10) % 10;
        n3 = (number/100) % 10;
        n4 = (number/1000) % 10;
        n5 = (number/10000) % 10;
        n6 = (number/100000) % 10;
        n7 = (number/1000000) % 10;
        n8 = (number/10000000) % 10;
        n9 = (number/100000000) % 10;
        n10 = (number/100000000) % 10;

        if (n1 == digit)
        {
            digitCounter++;
        }

        if (n2 == digit)
        {
            digitCounter++;
        }

        if (n3 == digit)
        {
            digitCounter++;
        }

        if (n4 == digit)
        {
            digitCounter++;
        }

        if (n5 == digit)
        {
            digitCounter++;
        }

        if (n6 == digit)
        {
            digitCounter++;
        }

        if (n7 == digit)
        {
            digitCounter++;
        }

        if (n8 == digit)
        {
            digitCounter++;
        }

        if (n9 == digit)
        {
            digitCounter++;
        }

        if (n10 == digit)
        {
            digitCounter++;
        }

        cout << "The total number of occurances of " << digit << " in " << number << " is " << digitCounter << endl;

    }
    else

        cout << "You have entered an invalid number." << endl;



    system("pause");
    return 0;

}
0
qo'shib qo'ydi
Agar bittagina va bitta tugma bo'lishi kerak
qo'shib qo'ydi muallif Slava, manba
Men bunga qo'shilaman, bu muammoni yaxshiroq hal qiladi. Men faqat berilgan echim bilan nima noto'g'ri bo'lgan savolga javob berishga urindim.
qo'shib qo'ydi muallif BenG, manba

Agar boshqa bo'lsa, agar bo'lmasa. Endi esa, siz faqat bitta qaror bayonotidan o'tasiz. Biror o'yinni topsangiz, siz tashqariga chiqasiz.

#include 
using namespace std;

int main()
{
    int number;
    int n1;
    int n2;
    int n3;
    int n4;
    int n5;
    int n6;
    int n7;
    int n8;
    int n9;
    int n10;
    int digit;
    int digitCounter = 0;


    cout << "Please enter a number between 100 to 2000000000." << endl;
    cin >> number;

    cout << "Please enter a digit that you want to find the number of occurances." << endl;
    cin >> digit;

    if (number > 0)
    {
        n1 = number % 10;
        n2 = (number/10) % 10;
        n3 = (number/100) % 10;
        n4 = (number/1000) % 10;
        n5 = (number/10000) % 10;
        n6 = (number/100000) % 10;
        n7 = (number/1000000) % 10;
        n8 = (number/10000000) % 10;
        n9 = (number/100000000) % 10;
        n10 = (number/100000000) % 10;

        if (n1 == digit)
        {
            digitCounter++;
        }

        if (n2 == digit)
        {
            digitCounter++;
        }

        if (n3 == digit)
        {
            digitCounter++;
        }

        if (n4 == digit)
        {
            digitCounter++;
        }

        if (n5 == digit)
        {
            digitCounter++;
        }

        if (n6 == digit)
        {
            digitCounter++;
        }

        if (n7 == digit)
        {
            digitCounter++;
        }

        if (n8 == digit)
        {
            digitCounter++;
        }

        if (n9 == digit)
        {
            digitCounter++;
        }

        if (n10 == digit)
        {
            digitCounter++;
        }

        cout << "The total number of occurances of " << digit << " in " << number << " is " << digitCounter << endl;

    }
    else

        cout << "You have entered an invalid number." << endl;



    system("pause");
    return 0;

}
0
qo'shib qo'ydi
Agar bittagina va bitta tugma bo'lishi kerak
qo'shib qo'ydi muallif Slava, manba
Men bunga qo'shilaman, bu muammoni yaxshiroq hal qiladi. Men faqat berilgan echim bilan nima noto'g'ri bo'lgan savolga javob berishga urindim.
qo'shib qo'ydi muallif BenG, manba

Raqamni simga ko'chirishingiz mumkin, shundan keyingina simvolda raqamni topasiz. Yoki biror belgi satrini (raqamini) va belgini (raqamni) o'qiysiz va shunday qilasiz:

char number[20], digit;
int count = 0, i;
printf("\nEnter a string : ");
scanf("%s", &number);

printf("\nEnter the character to be searched : ");
scanf("%c", &digit);

for (i = 0; number[i] != '\0'; i++) {
  if (number[i] == digit)
     count++;
}

if (count == 0)
  printf("\nCharacter '%c'is not present", digit);
else
  printf("\nOccurence of character '%c' : %d", digit, count);`
0
qo'shib qo'ydi
Menimcha, bu juda sodda
qo'shib qo'ydi muallif Slava, manba

Raqamni simga ko'chirishingiz mumkin, shundan keyingina simvolda raqamni topasiz. Yoki biror belgi satrini (raqamini) va belgini (raqamni) o'qiysiz va shunday qilasiz:

char number[20], digit;
int count = 0, i;
printf("\nEnter a string : ");
scanf("%s", &number);

printf("\nEnter the character to be searched : ");
scanf("%c", &digit);

for (i = 0; number[i] != '\0'; i++) {
  if (number[i] == digit)
     count++;
}

if (count == 0)
  printf("\nCharacter '%c'is not present", digit);
else
  printf("\nOccurence of character '%c' : %d", digit, count);`
0
qo'shib qo'ydi
Menimcha, bu juda sodda
qo'shib qo'ydi muallif Slava, manba

Raqamni simga ko'chirishingiz mumkin, shundan keyingina simvolda raqamni topasiz. Yoki biror belgi satrini (raqamini) va belgini (raqamni) o'qiysiz va shunday qilasiz:

char number[20], digit;
int count = 0, i;
printf("\nEnter a string : ");
scanf("%s", &number);

printf("\nEnter the character to be searched : ");
scanf("%c", &digit);

for (i = 0; number[i] != '\0'; i++) {
  if (number[i] == digit)
     count++;
}

if (count == 0)
  printf("\nCharacter '%c'is not present", digit);
else
  printf("\nOccurence of character '%c' : %d", digit, count);`
0
qo'shib qo'ydi
Menimcha, bu juda sodda
qo'shib qo'ydi muallif Slava, manba