• Предмет: Информатика
  • Автор: maximkravchenko0
  • Вопрос задан 4 месяца назад

Знайти добуток числа, що міститься у тестовому рядку, який складається тільки з цифр, на задану цифру. Впорядкувати слова текстового рядка в алфавітному порядку.
С++

Ответы

Ответ дал: mazamazacccp6ct75
1

#include <iostream>

#include <string>

#include <algorithm>

int main() {

std::string input;

int targetNum;

// Read the input string

std::cout << "Enter the input string: ";

std::getline(std::cin, input);

// Read the target number

std::cout << "Enter the target number: ";

std::cin >> targetNum;

// Вычислить произведение числа, содержащегося во входной строке, и заданного числа

int product = 1;

for (char c : input) {

if (isdigit(c)) {

product *= (c - '0');

}

}

product *= targetNum;

// Сортировка слов во входной строке в алфавитном порядке

std::string words[100];

int wordCount = 0;

std::string word = "";

for (char c : input) {

if (c == ' ') {

words[wordCount++] = word;

word = "";

} else {

word += c;

}

}

words[wordCount++] = word;

std::sort(words, words + wordCount);

// Вывести результат

std::cout << "Product: " << product << std::endl;

std::cout << "Sorted words: ";

for (int i = 0; i < wordCount; i++) {

std::cout << words[i] << " ";

}

return 0;

}

Вас заинтересует