Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // У скольких целых чисел, лежащих в диапазоне от 1 до n, есть цифра k?
- #include <iostream>
- using namespace std;
- int main()
- {
- int n, k, counter = 0;
- cout << "Enter number from 1 to 9999: ";
- cin >> n;
- cout << "Enter searching number (from 1 to 9): ";
- cin >> k;
- if (n > 9999 || n < 1) {
- cout << "Entered number is invalid\n";
- system("pause");
- main();
- }
- if (k<=0||k>9) {
- cout << "Entered searching number is invalid\n";
- system("pause");
- main();
- }
- for (int i = 1; i <= n; i++) {
- if (i == k) {
- counter++;
- continue;
- }
- if (i > 10 && i < 100) {
- if (i / 10 == k) {
- counter++;
- continue;
- }
- if (i % 10 == k) {
- counter++;
- continue;
- }
- }
- if (i > 100 && i<1000) {
- if (i / 100 == k) {
- counter++;
- continue;
- }
- if ((i % 100) / 10 == k) {
- counter++;
- continue;
- }
- if ((i % 100) % 10 == k) {
- counter++;
- continue;
- }
- }
- if (i > 1000) {
- if (i / 1000 == k) {
- counter++;
- continue;
- }
- if ((i % 1000) / 100 == k) {
- counter++;
- continue;
- }
- if (((i % 1000) % 100) / 10 == k) {
- counter++;
- continue;
- }
- if (((i % 1000) % 100) % 10 == k) {
- counter++;
- continue;
- }
- }
- }
- cout << "There are " << counter << " numbers between 1 and " << n << " that have " << k << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement