Skip to content

Instantly share code, notes, and snippets.

@nevikw39
Last active Jul 31, 2021
Embed
What would you like to do?
Basic C++ Example Code
{
"version": "2.0.0",
"tasks": [
{
"label": "Open Terminal",
"type": "shell",
"command": "osascript -e 'tell application \"Terminal\"\ndo script \"echo hello\"\nend tell'",
"problemMatcher": []
},
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
{
"version": "0.2.0",
"configurations": [
{
"name": "clang++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "clang++ build active file"
}
]
}
#include <iostream>
using namespace std;
int main()
{
cout << "hello, world";
return 0;
}
#include <iostream>
using namespace std;
int main()
{
cout << "hello, world\nhi, nevikw39\n";
return 0;
}
#include <iostream>
using namespace std;
int main()
{
cout << "87*69 = " << 87 * 69 << "\n9487*426 - 89*64 + 87*69 = " << 9487 * 426 - 89 * 64 + 87 * 69 << '\n';
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int n = 87 * 69;
cout << "87*69 = " << n << "\n9487*426 - 89*64 + 87*69 = " << 9487 * 426 - 89 * 64 + n << '\n';
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Please input an integer and press Enter: ";
cin >> n;
cout << "You inputed " << n << "!\n";
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int a, b;
cout << "Please input two integers a and b: ";
cin >> a >> b;
cout << "a/b = " << a / b << "\n"
<< "a%b = " << a % b << "\n";
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
cout << a + b << "\n";
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int a, b;
cout << "Please input two integers a and b: ";
cin >> a >> b;
if (b != 0)
cout << "a/b = " << a / b << "\n"
<< "a%b = " << a % b << "\n";
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int a, b;
cout << "Please input two integers a and b: ";
cin >> a >> b;
if (b)
cout << "a/b = " << a / b << "\n"
<< "a%b = " << a % b << "\n";
else
cout << "Divisor cannot be 0!\n";
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int day;
cin >> day;
switch (day)
{
case 2:
cout << "SUKIYA\n";
break;
case 3:
cout << "那個那個飯\n";
break;
case 4:
cout << "癡麵\n";
break;
case 0:
case 7:
cout << "Burger King\n";
break;
default:
cout << "No cram school today.\n";
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
if (a + b >= 120)
cout << "2\n";
else if (a >= 60 || b >= 60)
cout << "1\n";
else
cout << "0\n";
}
#include <iostream>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
cout << (a >= 60) + (b >= 60) << '\n';
}
#include <iostream>
using namespace std;
int main()
{
int a, b, d;
scanf("%d%d", &a, &b);
if (b > a)
d = b - a;
else
d = a - b;
if (d > 200)
cout << b * 3 << '\n';
else if (b > 20)
cout << b * 4 << '\n';
else
cout << "0\n";
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
b = abs(a - b);
cout << (max(b - 20, 0) << 2) - max(b - 200, 0) << '\n';
return 0;
}
#include <iostream>
#include <random>
using namespace std;
int main()
{
default_random_engine random(random_device{}());
uniform_int_distribution<int> dice(1, 6);
int val = dice(random), n = 0;
while (n != val)
{
cout << "Please guess the lucky number your dice is: ";
cin >> n;
}
cout << "Congratulate!! You got it. The lucky number is " << val << '\n';
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
while (n--)
cout << "hello, world\n";
return 0;
}
#include <iostream>
#include <random>
using namespace std;
int main()
{
default_random_engine random(random_device{}());
uniform_int_distribution<int> dice(1, 6);
int val = dice(random), n;
do
{
cout << "Please guess the lucky number your dice is: ";
cin >> n;
} while (n != val);
cout << "Congratulate!! You got it. The lucky number is " << val << '\n';
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
cout << i << ". hello, world\n";
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++)
{
int a;
cin >> a;
if (a == k)
{
cout << i << '\n';
break;
}
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
for (int i = 0; i < 100; i++)
{
if (i == 64)
{
cout << "敏感词汇已屏蔽\n";
continue;
}
cout << i << '\n';
}
return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
for (int i = 1; i < 10; i++)
{
for (int j = 1; j < 10; j++)
cout << i << '*' << j << '=' << setw(2) << i * j << ' ';
cout << '\n';
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int a, b, n;
cin >> a >> b >> n;
cout << a / b << '.';
a %= b;
while (n--)
{
a *= 10;
cout << a / b;
a %= b;
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int n = 0;
for (int i = 1; i < 8; i++)
{
char c;
cin >> c;
n += (c - '0') * i;
}
cout << (10 - n % 10) % 10 << '\n';
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int a, b, m, n;
cin >> a >> b;
m = a;
n = b;
while (a % b)
{
cout << a << '%' << b << '=' << a % b << '\n';
int t = b;
b = a % b;
a = t;
}
cout << a << '%' << b << '=' << a % b << '\n'
<< "GCD(" << m << ',' << n << ")=" << n << '\n';
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int n, m, k;
cin >> n >> m >> k;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{
int a;
cin >> a;
if (a == k)
{
cout << i << ' ' << j << '\n';
goto end;
}
}
end:
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int n = 87, *ptr = &n;
cout << "n = " << n << ", &n = " << &n << '\n'
<< "ptr = " << ptr << ", *ptr = " << *ptr << '\n';
n = 69;
cout << "n = " << n << ", &n = " << &n << '\n'
<< "ptr = " << ptr << ", *ptr = " << *ptr << '\n';
*ptr = 89;
cout << "n = " << n << ", &n = " << &n << '\n'
<< "ptr = " << ptr << ", *ptr = " << *ptr << '\n';
return 0;
}
#include <iostream>
using namespace std;
int main()
{
const int ca = 69, cb = 777, *ptr0 = &ca;
cout << "ptr0 = " << ptr0 << ", *ptr0 = " << *ptr0 << '\n';
ptr0 = &cb;
cout << "ptr0 = " << ptr0 << ", *ptr0 = " << *ptr0 << '\n';
// *ptr0 is read-only constant.
/*
*ptr0 = 9487;
cout << "ptr0 = " << ptr0 << ", *ptr0 = " << *ptr0 << '\n';
*/
int na = 87, nb = 666, *const ptr1 = &na;
cout << "ptr1 = " << ptr1 << ", *ptr1 = " << *ptr1 << '\n';
// ptr1 is read-only constant.
/*
ptr1 = &nb;
cout << "ptr1 = " << ptr1 << ", *ptr1 = " << *ptr1 << '\n';
*/
*ptr1 = nb;
cout << "ptr1 = " << ptr1 << ", *ptr1 = " << *ptr1 << '\n';
const int *const ptr2 = &ca;
cout << "ptr2 = " << ptr2 << ", *ptr2 = " << *ptr2 << '\n';
// ptr2 is read-only constant.
/*
ptr2 = &cb;
cout << "ptr2 = " << ptr2 << ", *ptr2 = " << *ptr2 << '\n';
*/
// *ptr2 is read-only constant.
/*
*ptr2 = 9487;
cout << "ptr2 = " << ptr2 << ", *ptr2 = " << *ptr2 << '\n';
*/
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int n, avg = 0, r = 0;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
avg += arr[i];
}
avg /= n;
for (int i = 0; i < n; i++)
r += abs(arr[i] - avg);
cout << r / 2 << '\n';
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int arr[3];
arr[0] = 9;
arr[1] = 8;
arr[2] = 7;
cout << arr + 0 << ' ' << *(arr + 0) << '\n'
<< arr + 1 << ' ' << *(arr + 1) << '\n'
<< arr + 2 << ' ' << *(arr + 2) << '\n';
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int arr[10];
for (int i = 0; i < 10; i++)
cin >> arr[i];
for (int i = 0; i < 10; i++)
cout << arr[i] << '\n';
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int arr[10];
for (int *ptr = arr; ptr < arr + 10; ptr++)
cin >> *ptr;
for (const int *ptr = arr; ptr < arr + 10; ptr++)
cout << *ptr << '\n';
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int arr[10];
for (int &i : arr)
cin >> i;
for (const int &i : arr)
cout << i << '\n';
return 0;
}
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
int arr[10];
copy_n(istream_iterator<int>(cin), 10, arr);
copy(arr, arr + 10, ostream_iterator<int>(cout, "\n"));
return 0;
}
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
int main()
{
vector<int> v0(5), v1(5, 87), v2 = {4, 3, 2, 1, 0}, v3 = v2;
cout << "v0:\n";
for (int i = 0; i < 5; i++)
cout << '\t' << v0[i] << '\n';
cout << "v1:\n";
for (auto itr = v1.begin(); itr < v1.end(); itr++) // Here, `auto` would be `vector<int>::iterator`
cout << '\t' << *itr << '\n';
cout << "v2:\n";
for (const int &i : v2)
cout << '\t' << i << '\n';
cout << "v3:\n\t";
copy(v3.begin(), v3.end(), ostream_iterator<int>(cout, "\n\t"));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment