
Экзаменационные задачи по Информатике. Решение.
Задача 1:
#include <iostream>
using namespace std;
void main ()
{
int beginHour, beginMinute, beginSec, beginSum,
endHour, endMinute, endSec, endSum,
intervalHour, intervalMinute, intervalSec, intervalSum;
cout<<
"Enter initial point of time (Vvedite nachalnij moment vremeni) Hour Minute Sec"
<<endl<<endl;
// ввод начального момента: Часы Минуты Секунды
cin>>beginHour>>beginMinute>>beginSec;
cout<<
"Enter final point of time (Vvedite konecnij moment vremeni) Hour Minute Sec"
<<endl<<endl;
// ввод конечного момента: Часы Минуты Секунды
cin>>endHour>>endMinute>>endSec;
/* рассчет промежутка состоит из:
1. перевод начального момента времени в общее кол-во сек (beginSum)
2. перевод конечного момента времени в общее кол-во сек (endSum)
3. вычисление промежутка в секундах (intervalSum)
4. перевод промежутка в: часы, минуты, секунды.
*/
beginSum=beginHour*3600+beginMinute*60+beginSec;
endSum=endHour*3600+endMinute*60+endSec;
intervalSum=endSum-beginSum;
intervalHour=intervalSum/3600;
intervalMinute=(intervalSum-intervalHour*3600)/60;
intervalSec=intervalSum-intervalHour*3600-intervalMinute*60;
// вывод результата
cout<<"Hour: "<<intervalHour<<
"\tMinute: "<<intervalMinute<<
"\tSec: "<<intervalSec<<endl;
}
Задача 2:
#include <iostream>
using namespace std;
Void main()
{
float x,y;
cout<<
"Enter value of argument (Vvedite znachenie argumanta) :"
<<endl;
cin>>x;
if (x<=-2) y=0;
if ((-2<x)&&(x<-1)) y=-1*x-2;
if ((-1<=x)&&(x<=1)) y=x;
if ((1<x)&&(x<2)) y=-1*x+2;
if (x>=2) y=0;
cout<<" X: "<<x<<" Y: "<<y<<endl;
}
Задача 3:
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
Void main()
{
int mass[100];
for (int i=0; i<100; i++)
{
mass[i]=-100+rand()%200;
cout<<mass[i]<<"\t";
}
int kolMin=0, kolMax=0, min=0, max=0;
for( int i=0; i<100; i++)
{
if (mass[i]<mass[min])
min=i;
if (mass[i]>mass[max])
max=i;
}
cout<<"1: min = "<<mass[min]<<" "<<min<<" max = "<<mass[max]<<" "<<max<<endl;
if (min>max)
{
int trash=min;
min=max;
max=trash;
}
for (int i=min+1; i<max; i++)
{
if (mass[i]<0)
kolMin++;
if (mass[i]>0)
kolMax++;
}
cout<<"2: kolMin = "<<kolMin<<" kolMax = "<<kolMax<<endl;
}
Задача 4:
#include <iostream>
#include <stdlib.h>
using namespace std;
Void main()
{
int n;
cout<<"Enter amount of elements (Kol-vo elementov): "<<endl;
cin>>n;
cout<<"Amount of elements (Kol-vo elementov): "<<n<<endl;
float *mass= new float[n];
for (int i=0; i<n; i++)
{
mass[i]=-150+rand()%200;
cout<<" "<<mass[i];
}
cout<<endl;
int negative;
for (int i=0; i<n; i++)
{
if (mass[i]<0)
negative=i;
}
cout<<"Last negative element: "<<mass[negative]<<
" With number: "<<negative+1<<endl;
float result=0;
for (int i=negative+1; i<n; i++)
result+=mass[i];
cout<<"Result of work: "<<result<<endl;
}
Задача 5:
#include <iostream>
#include <stdlib.h>
using namespace std;
Void main()
{
Int mass[10][20];
for (int i=0; i<10; i++)
{
for (int j=0; j<20; j++)
{
mass[i][j]=-100+rand()%200;
cout<<" "<<mass[i][j];
}
cout<<endl;
}
int sum=0,positive[10]={};
for (int i=0; i<10; i++)
{
for (int j=0; j<20; j++)
{
sum+=mass[i][j];
if (mass[i][j]>0)
positive[i]++;
}
}
float average=(float)sum/(10*20);
cout<<"Summa : "<<sum<<endl;
cout<<"Average (Srednee arifmeticheskoe): "<<average<<endl;
cout<<"Positive elements: ";
for (int i=0; i<10; i++)
cout<<" "<<positive[i];
cout<<endl;
}
Задача 6:
#include <iostream>
#include <string.h>
using namespace std;
Void main()
{
const char* stroka="zdes 2 bukvi z";
char slovo[25];
cout<<"Stroka: "<<stroka<<"\nEnter text: ";
cin>>slovo;
cout<<endl;
for (int i=0; i<strlen(slovo); i++)
{
int letter=0;
for (int j=0; j<strlen(stroka); j++)
{
if (slovo[i]==stroka[j])
letter++;
}
cout<<slovo[i]<<" - "<<letter<<endl;
}
}
Задача 7:
#include <iostream>
#include <stdlib.h>
using namespace std;
int WhereMore(int* mass, int n)
{
int result=0;
for (int i=0; i<n; i++)
{
if (mass[i]>0)
result++;
}
return result;
}
Void main()
{
int n;
cout<<"Enter N(amount elements): ";
cin>>n;
int *mass1= new int[n];
cout<<"mass1: ";
for (int i=0; i<n; i++)
{
mass1[i]=-100+rand()%200;
cout<<" "<<mass1[i];
}
cout<<"\n";
int *mass2= new int[n];
cout<<"mass2: ";
for (int i=0; i<n; i++)
{
mass2[i]=-50+rand()%200;
cout<<" "<<mass2[i];
}
cout<<"\n\n";
cout<<"Mass1: "<<WhereMore(mass1,n)<<" positive elements"<<endl;
cout<<"Mass2: "<<WhereMore(mass2,n)<<" positive elements"<<endl;
if (WhereMore(mass1,n)>WhereMore(mass2,n))
cout<<"\nPositive elements more in mass1!"<<endl;
else
cout<<"\nPositive elements more in mass2!"<<endl;
cout<<endl;
}
Задача 8:
#include <iostream>
#include <stdlib.h>
using namespace std;
template <class T> T Max (T* mass, int n)
{
int max=0;
for (int i=0; i<n; i++)
if (mass[i]>mass[max])
max=i;
return mass[max];
}