std::vector<std::vector<int>> matrix = {{1,2,3,42},{5,6,7,8},{9,10,11,42}};
const size_t limit = matrix.size() * matrix.front().size();
const size_t cols = matrix.front().size();
size_t i = 0;
size_t j = 1;
while(i < limit - 1 && matrix[i / cols][i % cols] != matrix[j / cols][j % cols])
{
if(++j == limit) j = ++i + 1;
}
if(i < limit - 1)
{
std::cout << "Indices of elements: [" << i / cols << "," << i % cols << "]";
std::cout << "[" << j / cols << "," << j % cols << "]" << std::endl;
}
else std::cout << "Not found" << std::endl;
#include <algorithm>
std::vector<std::vector<int>> arr = {{0,1,2,3},{0,1,2,3},{0,1,2,3}};
std::for_each(arr.begin(), arr.end(), [](auto& row){ std::reverse(row.begin(), row.end()); });
#include <vector>
#include <iostream>
#include <iterator>
#include <fstream>
struct Node
{
int x;
int y;
Node() : x(0), y(0) {}
Node(int x, int y) : x(x), y(y) {}
friend std::ostream& operator<<(std::ostream& os, const Node& node)
{
os << node.x << " " << node.y << " ";
return os;
}
friend std::istream& operator>>(std::istream& is, Node& node)
{
is >> node.x >> node.y;
return is;
}
};
class Way
{
private:
std::vector<Node> path;
public:
Way(){}
Way(const std::vector<Node>& path) : path(path) {}
Way(const Way& otherWay) : path(otherWay.path) {}
// в файл пишем количество элементов в path и сам path
friend std::fstream& operator<<(std::fstream& os, const Way& way)
{
os << way.path.size() << " ";
std::copy(way.path.begin(), way.path.end(), std::ostream_iterator<Node>(os, " "));
os << std::endl;
return os;
}
friend std::fstream& operator>>(std::fstream& is, Way& way)
{
size_t size = 0;
is >> size;
way.path.resize(size);
std::copy_n(std::istream_iterator<Node>(is), size, way.path.begin());
return is;
}
// на экран выводим только содержимое path
friend std::ostream& operator<<(std::ostream& os, const Way& way)
{
std::copy(way.path.begin(), way.path.end(), std::ostream_iterator<Node>(os, " "));
os << std::endl;
return os;
}
};
int main()
{
std::vector<Node> path0{ Node(3,2), Node(2,4), Node(4,5) };
std::vector<Node> path1{ Node(1,4), Node(4,3), Node(3,2) };
Way way0(path0);
Way way1(path1);
std::fstream ways;
ways.open("1.txt", std::ios_base::out | std::ios_base::trunc);
ways << way0 << way1;
ways.close();
Way way0r;
Way way1r;
ways.open("1.txt", std::ios_base::in);
ways >> way0r >> way1r;
ways.close();
std::cout << way0r << way1r;
return 0;
}
while(g>0);
имеет пустое тело и уходит в бесконечный повтор, если ранее введённый g>0.do
{
printf("Введите год:\n");
scanf("%d", &g);
}
while(!(g>0));
int g = 0;
while(!(g>0))
{
printf("Введите год:\n");
scanf("%d", &g);
}
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
int main(void)
{
std::vector<std::string> array_data{ "13.11.1999", "09.10.1997", "22.05.1995" };
auto as_yyyymmdd = [](const std::string& src){
return src.substr(6,4) + src.substr(3,2) + src.substr(0,2);
};
auto min_date_it = std::min_element(array_data.begin(), array_data.end(),
[as_yyyymmdd](const std::string& a, const std::string& b){
return as_yyyymmdd(a) < as_yyyymmdd(b);
});
std::string min_date = *min_date_it;
std::cout << min_date << std::endl;
return 0;
}
std::string hexString = "0xBAADF00D";
char octString[MAX_BUF];
itoa(std::stoi(hexString, 0, 16 ), octString, 8);
#include <cstring>
#include <iostream>
unsigned getHexDigit(char hexChar)
{
if (hexChar >= '0' && hexChar <= '9')
{
return hexChar - '0';
}
if (hexChar >= 'A' && hexChar <= 'F')
{
return 10 + hexChar - 'A';
}
if (hexChar >= 'a' && hexChar <= 'f')
{
return 10 + hexChar - 'a';
}
return 0;
}
void getOctQuartet(const char* hex, char* oct)
{
unsigned number = getHexDigit(hex[0]) << 8 | getHexDigit(hex[1]) << 4 | getHexDigit(hex[2]);
oct[0] = '0' + (number >> 9 & 07);
oct[1] = '0' + (number >> 6 & 07);
oct[2] = '0' + (number >> 3 & 07);
oct[3] = '0' + (number & 07);
}
char* getOctString(const char* hexString)
{
const size_t hexStrLen = strlen(hexString);
const size_t prefixLen = hexStrLen % 3;
char* octString = new char[(hexStrLen - prefixLen + 3) / 3 * 4 + 1];
char* pOct = octString;
if (prefixLen > 0)
{
char hexPrefix[3] = { '0'
, prefixLen == 2 ? hexString[0] : '0'
, prefixLen == 2 ? hexString[1] : hexString[0]
};
getOctQuartet(hexPrefix, octString);
pOct += 4;
}
const char* pHex = hexString + prefixLen;
while(pHex < hexString + hexStrLen)
{
getOctQuartet(pHex, pOct);
pHex += 3;
pOct += 4;
}
*pOct = '\0';
return octString;
}
int main()
{
char hexString[] = "123456789ABCDEF";
char* octString = getOctString(hexString);
std::cout << octString << std::endl;
delete[] octString;
return 0;
}
std::vector<int> heights{18, 10, 15, 20, 20, 10, 3} ;
size_t bestLen = 0;
size_t bestIndex = 0;
// путь не может быть проложен из последних двух вершин
for(size_t i = 0; i < heights.size() - 2; ++i)
{
// начало пути должно быть в гору
if(heights[i + 1] <= heights[i])continue;
// прокладываем путь до ближайшего спуска
for(size_t j = i + 2; j < heights.size(); ++j)
{
if(heights[j] > heights[j-1])break; // подъём встретился до спуска, решение заведомо неоптимально
if(heights[j] < heights[j-1])
{
size_t len = j - i;
if(len < bestLen || bestLen == 0)
{
bestLen = len;
bestIndex = i;
}
break;
}
}
}
if(bestLen == 0)
{
std::cout << 0 << std::endl;
}
else
{
std::cout << bestIndex + 1 << " " << bestIndex + bestLen + 1 << std::endl;
}