using System;
internal class Program {
private static void Main() {
Console.WriteLine("Введите минимум для счетчика");
var min = int.Parse(Console.ReadLine() ?? throw new ());
Console.WriteLine("Введите максимум для счетчика");
var max = int.Parse(Console.ReadLine() ?? throw new ());
Console.WriteLine("Введите значение для счетчика");
var v = int.Parse(Console.ReadLine() ?? throw new ());
var counter = new Counter(max, min, v);
Console.WriteLine("Введите + для увеличение и - для уменьшения, иное для выхода");
do {
var c = Console.ReadKey();
if (c.KeyChar == '+') counter.Increase();
else if (c.KeyChar == '-') counter.Decrease();
else break;
Console.WriteLine($" => {counter.Value}");
} while (true);
Console.ReadKey();
}
}
public class Counter {
public readonly int Maximum;
public readonly int Minimum;
public int Value { private set; get; }
public Counter(int maximum, int minimum, int counter) {
this.Maximum = maximum;
this.Minimum = minimum;
counter = Math.Min(this.Maximum, counter);
counter = Math.Max(this.Minimum, counter);
this.Value = counter;
}
private Counter() {
this.Maximum = 10;
this.Minimum = 0;
this.Value = 5;
}
public void Increase() {
var value = this.Value + 1;
if (value > this.Maximum || value < this.Minimum)
return;
this.Value++;
}
public void Decrease() {
var value = this.Value - 1;
if (value > this.Maximum || value < this.Minimum)
return;
this.Value--;
}
}
a = input()
print(a[-1:] if int(a) % 2 == 0 else a[-2:-1])
Объяснение:
1) Введем число как строку.
2) Конструкция "действие1 if условие1 else действие2" (она еще называется тернарным оператором) выполняет действие1, если условие1 возвращает True, иначе выполняется действие2. Это уменьшает количество строк кода с четырех до одной.
3) Выражение s[a:b] вернет все символы в строке s в диапазоне [a; b). Выражение s[a:] вернет все символы в строке s от a до конца строки. Если a или b отрицательные, то отсчет будет вестись с конца строки. Выражение s[-1:] вернет все символы в строке, начиная с последнего (т.е., последний символ). Ну а s[-2:-1] вернет все символы с предпоследнего до последнего, исключая последний (т.е., предпоследний символ).
#include "stdafx.h"
#include "conio.h"
#include "iostream"
using namespace std;
int main()
{
setlocale( LC_ALL,"Russian");// включаем русскую раскладку
cout << "Введите количество элементов массива" << endl;
unsigned int lenthOfArray=0;//переменная длины массива
cin >> lenthOfArray;//считываем длину массива
//создаем Массив
int* Array = new int [lenthOfArray];
int M=1;
cout << "Вводите элементы массива:" << endl;
for(int i=0;i<lenthOfArray;i++)
{
cin >> Array[i];//считываем массив
current=up;
if(i>1)
if((Array[i]>=Array[i-1])&&(Array[i-1]<Array[i-2]))
{
M++;
}
else
{
if((Array[i]<=Array[i-1])&&(Array[i-1]>Array[i-2]))
M++;
}
}
cout << "Количество промежутков монотонности: " << M << endl;
getch();
delete [] Array;
return 0;
}
Добавлено через 7 минут
C++Выделить код
1
current=up;
только это удали
Добавлено через 10 минут
C++Выделить код
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "stdafx.h"
#include "conio.h"
#include "iostream"
using namespace std;
int main()
{
setlocale( LC_ALL,"Russian");// включаем русскую раскладку
cout << "Введите количество элементов массива" << endl;
unsigned int lenthOfArray=0;//переменная длины массива
cin >> lenthOfArray;//считываем длину массива
//создаем Массив
int* Array = new int [lenthOfArray];
int M=1;
cout << "Вводите элементы массива:" << endl;
for(int i=0;i<lenthOfArray;i++)
{
cin >> Array[i];//считываем массив
if(i>1)
if((Array[i]>Array[i-1])&&(Array[i-1]<Array[i-2]))
{
M++;
}
else
{
if((Array[i]<Array[i-1])&&(Array[i-1]>Array[i-2]))
M++;
else
if((Array[i]==Array[i-1])&&(Array[i-1]!=Array[i-2]))
M++;
}
}
cout << "Количество промежутков монотонности: " << M << endl;
getch();
delete [] Array;
return 0;
}
Объяснение: