Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
экзамен прога.docx
Скачиваний:
0
Добавлен:
01.07.2025
Размер:
46.27 Кб
Скачать

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

#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;

}

2)Даны четыре числа. Напечатать “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;

}

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

#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;

}

5)Повернуть матрицу на 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;}

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

#include "stdafx.h"

#include <iostream>

#include <ctime>

#include <cstdlib>

#include <cstring>

using namespace std;