Посчитать сколько слов в тексте

Найти и напечать самое длинное слово.

#include "stdafx.h #include <iostream> #include <cmath> #include <cstring> using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{char s[1000];

cin.getline(s,1000);

int n = strlen (s);

//в цикле обработать все слова - последовательно

 

int maxlen=0, maxbegin, maxend;

for (int i=0; i<n; i++)

{

//найти начало слова - пропустить пробелы и найти букву

for (; s[i] == ' '; i++);

//если слово не нашли, то выход из цикла

if (i >= n)

break;

//если нашли - запомнить позицию первой буквы

int begin=i;

for (; i<n && s[i]!= ' '; i++)

;

int end=i;

//если длина Max, тозапомнить это слово

if (end-begin > maxlen)

maxlen = end-begin, maxbegin = begin, maxend = end;

}

//печатать самое длинное слово - от начала и до конца

for (int i = maxbegin; i < maxend; i++)

cout << s[i];

cout << endl;

system("pause");

return 0;

}

Даны четыре числа. Напечатать “Yes”, если среди них есть парные числа.

#include "stdafx.h"#include <iostream>#include <cmath>using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{

int a, b, c, d;

cin >> a >> b >> c >> d;

if (a == b || b == c || c == d || a == c || d == a || b == d)

cout << "Yes" << endl;

else

cout << "No" << endl;

system("pause");

return 0;}

 

3) Заполнить матрицу (от 1 до N^2) змейкой.

#include "stdafx.h"

#include <cmath>

#include <iostream>

#include <iomanip>

#include <cstdlib>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{

const int N=100;

int a[N][N];

int n;

cin>>n;

for (int i=0, k=1; i<n; i++)

if (i % 2 == 0)

for (int j=0; j<n; j++, k++)

a[i][j]=k;

else

for (int j=n-1; j>=0; j--, k++)

a[i][j]=k;

for (int i=0; i<n; i++)

{

for (int j=0; j< n; j++)

cout << setw(4) << a[i][j];

cout << endl;

}

system("pause");

return 0;

}

 

Все латинские буквы поменять: прописные на строчные, строчные на прописные.

#include "stdafx.h"

#include <cstring>

#include <iostream>

#include <cmath>

#include <cstdlib>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{

int a = 'a'-'A';

cout << a << endl;

char s[1000];

cin.getline(s,1000);

for (int i=0; i<strlen(s); i++)

if ((int) s[i] >= 'a' && (int) s[i] <= 'z')

s[i]-= a;

else

if ((int) s[i] >= 'A' && (int) s[i] <= 'Z')

s[i]+=a;

cout << s << endl;

system ("pause");

return 0;

}

Повернуть матрицу на 90 градусов.

#include "stdafx.h"

#include <iomanip>

#include <iostream>

#include <ctime>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{

//заполнение матрицы

const int N=10;

int a[N][N];

srand (time (0));

for (int i=0; i<N; i++)

for (int j=0; j<N; j++)

a[i][j] = rand() % 100;

//печать матрицы

for (int i=0; i<N; i++)

{

for (int j=0; j<N; j++)

cout << setw(4) << a[i][j];

cout << endl;

}

int N2 = N/2;

for (int i=0; i < N2 + N % 2; i++)

for (int j=0; j < N2; j++)

{

int t = a[i][j];

a[i][j] = a[N-j-1][i];

a[N-j-1][i] = a[N-i-1][N-j-1];

a[N-i-1][N-j-1] = a[j][N-i-1];

a[j][N-i-1] = t;

}

cout << endl << endl;

for (int i=0; i<N; i++)

{

for (int j=0; j<N; j++)

cout << setw(4) << a[i][j];

cout << endl;

}

system ("pause");

return 0;}

 

Дан массив целых чисел. Написать программу, которая упорядочит, отсортирует в порядке возрастания.

#include "stdafx.h"

#include <iostream>

#include <ctime>

#include <cstdlib>

#include <cstring>

 

using namespace std;

 

int _tmain(int argc, _TCHAR* argv[])

{

int a[10000], n;

cin >> n;

srand (time (0));

for (int i=0; i<n; i++)

{

a[i] = rand () % 100; // числа от 0 до 99

cout << a[i] << " ";

}

cout << endl;

 

for (int k=1; k<n; k++) // k-проходы, от начала до конца

for (int i=0; i < n-1; i++)

 

if (a[i] > a[i+1])

swap (a[i], a[i+1]); // меняем местами

 

for (int i=0; i<n; i++)

 

cout << a[i] << " ";

system ("pause");

return 0;

}

Дано натуральное число n. Напечатать все его делители и посчитать их.

#include "stdafx.h"

#include <iostream>

#include <cmath>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{

int n;

cin >> n; // ввод числа n

int kol = 0;

for (int d = 1; d <= n; d++)

if (n % d == 0)

kol++,

cout << d << " ";

cout << endl;

cout << "kol = " << kol << endl;

system ("pause");

return 0;

}

 

Посчитать сколько слов в тексте.

#include "stdafx.h"

#include <iostream>

#include <cmath>

#include <cstdlib>

#include <cstring>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{

char s[1000];

cin.getline (s,1000);

int n = strlen(s);

int k = 0;

if (s[0]!= ' ')

k = 1;

for (int i=1; i<n; i++)

if (s[i]!= ' ' && s[i-1] == ' ')

k++;

cout << k;

cout << endl;

system ("pause");

return 0;

}

Написать программу с 3-мя функциями: 1.Заполняет массив случайными числами (кол-во от 10 до 20) и распечатывает их. 2.Ищет самое большое число в этом массиве. Главная функция должна создать 5 случайных массивов, каждый распечатать.

#include "stdafx.h"

#include <iostream>

#include <cstdlib>

#include <time.h>

#include <cmath>

using namespace std;

int FillPrint (int*a);

int Max (int a[], int n);

int _tmain(int argc, _TCHAR* argv[])

{

int a[10000];

srand (time (0));

for (int i=0; i<5; i++) // будем строить 5 массивов

{

int n = FillPrint (a); // заполнить и напечатать массив

cout << Max(a,n) << endl; // найти и напечатать Max

}

system("pause");

return 0;

}

int FillPrint (int * a)

{

int n = 10 + rand() % 10; // сколько будет элементов в массиве

for (int i=0; i<n; i++)

{

a[i] = rand ()% 100;

cout << a[i] << " ";

}

system("pause");

return n; // возврaщаем кол-во

}

int Max (int a[], int n)

{

int max = 0;

for (int i=0; i<n; i++)

if (a[i] > max) max = a[i];

system("pause");

return max;

}

 


Понравилась статья? Добавь ее в закладку (CTRL+D) и не забудь поделиться с друзьями:  



double arrow
Сейчас читают про: