задача На вход программе в одну строку подается последовательность символов, содержащая только заглавные и строчные латинские буквы, цифры и пробелы. Точка – признак конца ввода. Программа должна найти в этой последовательности количество слов, начинающихся с заглавной буквы и имеющих максимальную длину. Словом называется последовательность латинских букв и цифр. Между собой слова разделяются одним пробелом. Длиной слова называется количество символов, из которых оно состоит. Например, для ввода «Aabc h5dfh Al Zr67.» ответом на задачу будет являться число 2, так как всего два слова «Aabc» и «Zr67» начинаются с заглавной буквы и имеют среди таких слов максимальную длину #include <iostream> #include <vector> #include <string> #include<cctype> using namespace::std; vector<string> del(string words) { vector<string>ans; string word; for (int i = 0; i < words.size(); ++i) { if (words[i] == ' ') { ans.push_back(word); cout << word; word.clear(); } else { word = word + words[i]; } return ans; } } int main() { string wwod; int maxsize = 0, count = 0; vector<string>mas; getline(cin, wwod); /* for (int i = 0; i < wwod.size(); ++i) { if (wwod[i] == '.') {; wwod.resize(i); } } */ mas = del(wwod); for (int h = 0; h < mas.size(); ++h) { string k = mas[h]; { if (isupper(k[0]) > 0) { if (k.size() > maxsize) { maxsize = k.size(); } } } for (int j = 0; j < mas.size(); ++j) { string k = mas[j]; if (k.size() == maxsize && isupper(k[0]) > 0) { ++count; } } cout << count; } } C #include <iostream> #include <vector> #include <string> #include<cctype> using namespace::std; vector<string> del(string words) { vector<string>ans; string word; for (int i = 0; i < words.size(); ++i) { if (words[i] == ' ') { ans.push_back(word); cout << word; word.clear(); } else { word = word + words[i]; } return ans; } } int main() { string wwod; int maxsize = 0, count = 0; vector<string>mas; getline(cin, wwod); /* for (int i = 0; i < wwod.size(); ++i) { if (wwod[i] == '.') {; wwod.resize(i); } } */ mas = del(wwod); for (int h = 0; h < mas.size(); ++h) { string k = mas[h]; { if (isupper(k[0]) > 0) { if (k.size() > maxsize) { maxsize = k.size(); } } } for (int j = 0; j < mas.size(); ++j) { string k = mas[j]; if (k.size() == maxsize && isupper(k[0]) > 0) { ++count; } } cout << count; } } почему то всегда ноль выводит. Если не сложно то закомментируйте некоторые моменты в коде.
impelix, Ты не следишь за тем где у тебя блоки закрываются - метод del всегда будет возвращать пустой вектор, потому что ты не там return поставил - 20-ая строчка должна быть перед 19-ой, чтобы return был ПОСЛЕ цикла, а не в конце первой итерации. И такая же проблема в конце программы - считать сколько раз встречается подходящие слова с максимальной длиной надо ПОСЛЕ того как ты нашёл maxsize а не на каждой итерации - 51-ая строчка должна быть перед 43-ей. Ну и как мелочь - всё это можно решить в один проход без использования дополнительной памяти для хранения списка слов.
#include <iostream> using namespace std; int main() { string input_str; getline(cin, input_str); int ind = 0, cnt = 0, now_length = 0, max_length = 0; bool begins_big = (input_str[ind] >= 'A' && input_str[ind] <= 'Z'); while (input_str[ind] != '.' && ind < input_str.size()) { // system("pause"); // system("cls"); if (input_str[ind] != ' ') { ++now_length; } if (input_str[ind] == ' ' || ind == input_str.size() - 1 || (ind + 1 < input_str.size() && input_str[ind + 1] == '.')) { // cout << "end of the word\n"; if (now_length > max_length && begins_big) { // cout << "changing max_length to: " << now_length << '\n'; max_length = now_length; cnt = 1; } else if (now_length == max_length && begins_big) { // cout << "increasing count: " << cnt + 1 << '\n'; ++cnt; } if (ind + 1 < input_str.size()) { if (input_str[ind + 1] >= 'A' && input_str[ind + 1] <= 'Z') { begins_big = true; } else { // cout << "next word begins small\n"; begins_big = false; } } now_length = 0; } // cout << input_str << '\n'; // cout << "ind: " << ind << '\n' << "input_str[ind]: " << input_str[ind] << '\n' << "now_length: " << now_length << '\n' << "max_length: " << max_length << '\n' << "begins big: " << (begins_big ? "true" : "false") << '\n' << "count: " << cnt << '\n'; ++ind; } cout << cnt << '\n'; return 0; } C #include <iostream> using namespace std; int main() { string input_str; getline(cin, input_str); int ind = 0, cnt = 0, now_length = 0, max_length = 0; bool begins_big = (input_str[ind] >= 'A' && input_str[ind] <= 'Z'); while (input_str[ind] != '.' && ind < input_str.size()) { // system("pause"); // system("cls"); if (input_str[ind] != ' ') { ++now_length; } if (input_str[ind] == ' ' || ind == input_str.size() - 1 || (ind + 1 < input_str.size() && input_str[ind + 1] == '.')) { // cout << "end of the word\n"; if (now_length > max_length && begins_big) { // cout << "changing max_length to: " << now_length << '\n'; max_length = now_length; cnt = 1; } else if (now_length == max_length && begins_big) { // cout << "increasing count: " << cnt + 1 << '\n'; ++cnt; } if (ind + 1 < input_str.size()) { if (input_str[ind + 1] >= 'A' && input_str[ind + 1] <= 'Z') { begins_big = true; } else { // cout << "next word begins small\n"; begins_big = false; } } now_length = 0; } // cout << input_str << '\n'; // cout << "ind: " << ind << '\n' << "input_str[ind]: " << input_str[ind] << '\n' << "now_length: " << now_length << '\n' << "max_length: " << max_length << '\n' << "begins big: " << (begins_big ? "true" : "false") << '\n' << "count: " << cnt << '\n'; ++ind; } cout << cnt << '\n'; return 0; }
Whales_Nik, #include <iostream> using namespace std; int main() { int maxsize = 0, count = 0, size = 0, isFirstCharCapitalLetter = 0, ch; do { ch = getchar(); if (ch == ' ' || ch == '.') { if (isFirstCharCapitalLetter) { if (size == maxsize) count++; if (size > maxsize) { maxsize = size; count = 1; } } size = 0; } else if (size++ == 0) isFirstCharCapitalLetter = isupper(ch); } while (ch != '.'); cout << count << endl; return 0; } C #include <iostream> using namespace std; int main() { int maxsize = 0, count = 0, size = 0, isFirstCharCapitalLetter = 0, ch; do { ch = getchar(); if (ch == ' ' || ch == '.') { if (isFirstCharCapitalLetter) { if (size == maxsize) count++; if (size > maxsize) { maxsize = size; count = 1; } } size = 0; } else if (size++ == 0) isFirstCharCapitalLetter = isupper(ch); } while (ch != '.'); cout << count << endl; return 0; }