We introduce an integer. Count the number of times the software in its decimal notation includes some figures, let us say 3. That's all the condition. For example: 123 -> 1; 54321345 -> 2; 3333 -> 4 etc.
The task is very simple … at the high school level. But in order, to do the task is not quite so trivial – complicate:
– offer several (as much as possible) different ways to implement;
– for each realization reduce code writing so, that it, more concise as possible.
Solutions (It is where to turn because of the simplicity of the task, solutions can be very, Fill your):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <iostream> using namespace std; int main() { setlocale(LC_ALL, "rus"); unsigned long e; unsigned n = 0; cout << "Ввод числа: "; cin >> e; do if (3 == e % 10) n++; while ((e /= 10) > 0); cout << "Цифр 3 в числе " << n << " штук" << endl; } |
It all fits into the solution one operator do … while. But, pay attention to, what “injected integer” – This input is always a string, representing a number (Here the trick in the formulation of the problem). Then:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <iostream> using namespace std; int main() { setlocale(LC_ALL, "rus"); char e[80], *p = e; unsigned n = 0; cout << "Ввод числа: "; cin >> e; while ((p = strchr(p, '3')) != NULL) p++, n++; cout << "Цифр 3 в числе " << n << " штук" << endl; } |
Or so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <iostream> //#include <string> using namespace std; int main() { setlocale(LC_ALL, "rus"); char e[80]; unsigned n = 0, i; cout << "Ввод числа: "; cin >> e; for (i = 0; i < strlen(e); i++) if (e[i] == '3') n++; cout << "Цифр 3 в числе " << n << " штук" << endl; } |
Or even so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <iostream> #include <string> using namespace std; int main() { setlocale(LC_ALL, "rus"); string e; unsigned n = 0; cout << "Ввод числа: "; cin >> e; for (string::iterator i = e.begin();; n++, i++) if ((i = find(i, e.end(), '3')) == e.end()) break; cout << "Цифр 3 в числе " << n << " штук" << endl; } |
And that's how it looks:
1 2 3 4 5 6 7 8 9 10 11 12 | $ ./mul3_1 Ввод числа: 1234531 Цифр 3 в числе 2 штук $ ./mul3_2 Ввод числа: 12341231 Цифр 3 в числе 2 штук $ ./mul3_3 Ввод числа: 12341231 Цифр 3 в числе 2 штук $ ./mul3_4 Ввод числа: 333 Цифр 3 в числе 3 штук |
P.S. This problem is a good illustration of the fundamental programming principles, what any the problem can be solved many and very various ways.
I decided to like this
You are welcome, кто-нибудь может человеческим языком объяснить, зачем в решении использовать “unsigned”? Я искал информацию в гугле, но мой мозг оказался не готов к наплыву слишком страшных слов.
потому что возможно тестирующая система набирает много цифр. Поэтому для таких цифр длиною как 10 стпенью 16 или больше используется ключевое слово ‘unsigned’.
#include
using namespace std;
int main() {
unsigned long long int a, c;
int k=0;
cin>>a;
while(a>0) {
c=a%10;
a/=10;
if(c==3) k ;
}
cout<<k;
return 0;
}
1016
alert(‘js’);
#include
#include
using namespace std;
int main()
{
string file;
getline(cin, file);
int sum = 0;
for (int z = 0; from < file.size(); with ++)
{
if (file[from] == '3')
{
sum++;
}
}
cout << sum;
return 0;
}
Решение с проверками на ошибки:
#include
#include
using namespace std;
int main()
{
string aboba = “”;
char bebra = 0;
int jopa = 0;
cout << "Input nubmer: ";
getline(cin, aboba);
cin.clear();
for (int i = 0; i < aboba.size(); i )
{
if (!(isdigit(aboba[i])))
{
cout << "Error: You did not enter a number, or there were other characters in the number." << endl;
return 1;
}
}
cout <> bebra;
if (!(isdigit(bebra)))
{
cout << "Error: You did not enter a number, or there were other characters in the number." << endl;
return 1;
}
for (int i = 0; i < aboba.size(); i )
{
if (aboba[i] == bebra)
{
jopa++;
}
}
cout << "Numbers found: " << jopa << endl;
return 0;
}