#include <iostream>
#include <string>
#include <fstream>
/* The member function cin.getline() works with C strings (i.e. arrays of char)
whereas the free function std::getline() works with C++ strings (i.e. std::string.)
You should not be using C strings at all when learning C++, which means
you should not be using cin.getline().*/
using namespace std;
void FindExt(std::string paths[], int nPath, std::string extName, std::string results[], int &nRes)
{
nRes = 0;
for(int i = 0; i < nPath; i++)
{
string tmp = paths[i].substr(paths[i].find('.') + 1);
if(tmp == extName)
{
++nRes;
for(int j = 0; j < nRes; j++)
{
results[j] = paths[i];
}
}
}
}
void Find(string paths[], int nPath, string curDir, string results[], int &nRes)
{
}
int main()
{
string paths[] = {"D:/Others/us/b.pdf", "C:/Users/c.txt", "C:/Users/adm/a.txt", "E:/temp/d.exe"};
string extName = "txt";
string results[1000];
int nRes;
FindExt(paths, 4, extName, results, nRes);
for(int i = 0; i < nRes; i++)
{
cout << results[i] << " ";
}
cout << endl;
cout << nRes;
return 0;
}
#include <iostream>
using namespace std;
void doMin(int a[][3], int m, int n)
{
int res[100][100] = {0};
int toaDoX[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int toaDoY[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
for(int i = 0; i < m; ++i)
{
for(int j = 0; j < n; ++j)
{
if(a[i][j] == 1)
{
for(int k = 0; k < 8; ++k)
{
int newX = i + toaDoX[k];
int newY = j + toaDoY[k];
if(newX >= 0 && newY >= 0 && newX < m && newY < n)
{
++res[newX][newY];
}
}
}
}
}
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
a[i][j] = res[i][j];
}
}
}
int main()
{
int a[3][3] = {
1, 0, 0,
0, 1, 0,
0, 0, 0};
int m = 3;
int n = 3;
doMin(a, m, n);
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
void ReadMap(int map[][100], int &m, int &n)
{
ifstream fin;
fin.open("map.txt");
if(!fin.is_open())
{
cout << "failed to open file!";
return;
}
fin >> m >> n;
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
fin >> map[i][j];
}
}
fin.close();
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
cout << map[i][j] << " ";
}
cout << endl;
}
}
int main()
{
int map[100][100];
int m, n;
ReadMap(map, m, n);
return 0;
}
//các hàm
c_str: string to char
char array to string
// Demonstrates conversion
// from character array to string
#include <bits/stdc++.h>
using namespace std;
// converts character array
// to string and returns it
string convertToString(char* a, int size)
{
int i;
string s = "";
for (i = 0; i < size; i++) {
s = s + a[i];
}
return s;
}
// Driver code
int main()
{
char a[] = { 'C', 'O', 'D', 'E' };
char b[] = "geeksforgeeks";
int a_size = sizeof(a) / sizeof(char);
int b_size = sizeof(b) / sizeof(char);
string s_a = convertToString(a, a_size);
string s_b = convertToString(b, b_size);
cout << s_a << endl;
cout << s_b << endl;
return 0;
}
// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
#define N 4
// Function to rotate the matrix 90 degree clockwise
void rotate90Clockwise(int a[N][N])
{
// Traverse each cycle
for (int i = 0; i < N / 2; i++) {
for (int j = i; j < N - i - 1; j++) {
// Swap elements of each cycle
// in clockwise direction
int temp = a[i][j];
a[i][j] = a[N - 1 - j][i];
a[N - 1 - j][i] = a[N - 1 - i][N - 1 - j];
a[N - 1 - i][N - 1 - j] = a[j][N - 1 - i];
a[j][N - 1 - i] = temp;
}
}
}
// Function for print matrix
void printMatrix(int arr[N][N])
{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
cout << arr[i][j] << " ";
cout << '\n';
}
}
// Driver code
int main()
{
int arr[N][N] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
rotate90Clockwise(arr);
printMatrix(arr);
return 0;
}
//anticlockwise
void rotateMatrix(int mat[][N])
{
// Consider all squares one by one
for (int x = 0; x < N / 2; x++) {
// Consider elements in group
// of 4 in current square
for (int y = x; y < N - x - 1; y++) {
// Store current cell in
// temp variable
int temp = mat[x][y];
// Move values from right to top
mat[x][y] = mat[y][N - 1 - x];
// Move values from bottom to right
mat[y][N - 1 - x] = mat[N - 1 - x][N - 1 - y];
// Move values from left to bottom
mat[N - 1 - x][N - 1 - y] = mat[N - 1 - y][x];
// Assign temp to left
mat[N - 1 - y][x] = temp;
}
}
}
// Function to print the matrix
void displayMatrix(int mat[N][N])
{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << mat[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
/ Driver code /
int main()
{
// Test Case 1
int mat[N][N] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
// Function call
rotateMatrix(mat);
// Print rotated matrix
displayMatrix(mat);
return 0;
}
int binaryToDecimal(int n)
{
int num = n;
int dec_value = 0;
// Initializing base value to 1, i.e 2^0
int base = 1;
int temp = num;
while (temp) {
int last_digit = temp % 10;
temp = temp / 10;
dec_value += last_digit * base;
base = base * 2;
}
return dec_value;
}
void decToBinary(int n)
{
// array to store binary number
int binaryNum[32];
// counter for binary array
int i = 0;
while (n > 0) {
// storing remainder in binary array
binaryNum[i] = n % 2;
n = n / 2;
i++;
}
// printing binary array in reverse order
for (int j = i - 1; j >= 0; j--)
cout << binaryNum[j];
}
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets