804
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> int fact(int n) { if (n == 0 || n == 1) { return 1; } else { return n*fact(n-1); } } int main() { int n = 0; std::cin >> n; std::cout << fact(n) << std::endl; return 0; } |
805
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> int max(int lhs, int rhs) { return (lhs > rhs) ? lhs : rhs; } int main() { int x = 0, y = 0; std::cin >> x >> y; std::cout << max(x, y) << std::endl; return 0; } |
808
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> int gcd(int a, int b) { while (a != b) { if (a > b) { a -= b; } else { b -= a; } } return a; } int main() { int a = 0, b = 0; std::cin >> a >> b; std::cout << gcd(a, b) << std::endl; return 0; } |
811
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> void swap(int& x, int& y) { x = x + y; y = x - y; x = x - y; } int main() { int x = 0, y = 0; std::cin >> x >> y; swap(x, y); std::cout << x << " " << y << std::endl; return 0; } |
812
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> void print(int a[], int size) { for (size_t i = 0; i < size; ++i) { std::cout << a[i] << " "; } } int main() { int n = 0, size = 0; std::cin >> n >> size; int arr[1000] = {0}; for(size_t i = 0; i < n; ++i) { std::cin >> arr[i]; } print(arr, size); return 0; } |
813
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 |
#include <iostream> void print2D(int a[][100], int row, int col) { for (int i = 0; i < row; ++i) { for (int j = 0; j < col; ++j) { std::cout << a[i][j] << " "; } std::cout << std::endl; } } int main() { int row = 0, col = 0; std::cin >> row >> col; int arr[100][100] = {0}; for (int i = 0; i < row; ++i) { for (int j = 0; j < col; ++j) { std::cin >> arr[i][j]; } } print2D(arr, row, col); return 0; } |
819
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> int fact(int n) { if (n == 0 || n == 1) { return 1; } else { return n*fact(n-1); } } int main() { int n = 0; std::cin >> n; std::cout << fact(n) << std::endl; return 0; } |
820
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> int fibnaci(int i) { if (i == 0) { return 0; } else if (i == 1) { return 1; } else { return fibnaci(i-1)+fibnaci(i-2); } } int main() { int n = 0; std::cin >> n; std::cout << fibnaci(n) << std::endl; return 0; } |
810
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> int abs(int x) { return (x >= 0) ? x : -x; } int main() { int x = 0; std::cin >> x; std::cout << abs(x) << std::endl; return 0; } |
806
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <cstdio> double add(double x, double y) { return x + y; } int main() { double x = 0.0, y = 0.0; std::cin >> x >> y; printf("%.2lf\n", add(x, y)); return 0; } |
807
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> int sum(int l, int r) { int sum = 0; for (int i = l; i <= r; ++i) { sum += i; } return sum; } int main() { int x = 0, y = 0; std::cin >> x >> y; std::cout << sum(x, y) << std::endl; return 0; } |
809
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 |
#include <iostream> int lcd(int a, int b) { while (a != b) { if (a > b) { a -= b; } else { b -= a; } } return a; } int lcm(int a, int b) { return a*b/lcd(a, b); } int main() { int x = 0, y = 0; std::cin >> x >> y; std::cout << lcm(x, y) << std::endl; return 0; } |
814
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 |
#include <iostream> int main() { int x = 0, y = 0, size = 0; std::cin >> x >> y >> size; int n[100] = {0}, m[100] = {0}; int temp = x; while(temp--) { std::cin >> n[x-temp-1]; } temp = y; while(temp--) { std::cin >> m[y-temp-1]; } for (int i = 0; i < size; ++i) { m[i] = n[i]; } for (int i = 0;i < y; ++i) { std::cout << m[i] << " "; } return 0; } |
815
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <cstdio> void print(char str[]) { printf("%s", str); } int main() { char ch[100] = {0}; fgets(ch, 101, stdin); print(ch); return 0; } |
816
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 |
#include <iostream> void reverse(int arr[], int size) { for (int i = 0; i < size; ++i) { if (i <= size-1-i && arr[i] != arr[size-1-i]) { arr[i]= arr[i] + arr[size-1-i]; arr[size-1-i] = arr[i] - arr[size-1-i]; arr[i] = arr[i] - arr[size-1-i]; } } } int main() { int x = 0, y = 0; std::cin >> x >> y; int arr[1000] = {0}; int temp = x; while(temp--) { std::cin >> arr[x-temp-1]; } reverse(arr, y); for (int i = 0; i < x; ++i) { std::cout << arr[i] << " "; } return 0; } |
817
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 |
#include <iostream> int get_unique_count(int a[], int n) { int arr[1000] = {0}; for (int i = 0; i < n; ++i) { arr[a[i]-1]++; } int res = 0; for (int i = 0; i < 1000; ++i) { if (arr[i] > 0) { res++; } } return res; } int main() { int x = 0; std::cin >> x; int arr[1000] = {0}; int temp = x; while(temp--) { std::cin >> arr[x-temp-1]; } std::cout << get_unique_count(arr, x) << std::endl; return 0; } |
818
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 |
#include <iostream> void sort(int a[], int x, int l, int r) { for (int i = l; i < r; ++i) { for (int j = l; j < r; ++j) { if (a[j] > a[j+1]) { a[j] = a[j] + a[j+1]; a[j+1] = a[j] - a[j+1]; a[j] = a[j] - a[j+1]; } } } for (int i = 0; i < x; ++i) { std::cout << a[i] << " "; } } int main() { int x = 0, l = 0, r = 0; std::cin >> x >> l >> r; int arr[1000] = {0}; int temp = x; while(temp--) { std::cin >> arr[x-temp -1]; } sort(arr, x, l, r); return 0; } |
821
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> int get_count(int i) { if (i == 0) { return 0; } else if (i == 1) { return 1; } else if (i == 2) { return 2; } else { return get_count(i-1) + get_count(i-2); } } int main() { int x = 0; std::cin >> x; std::cout << get_count(x) << std::endl; return 0; } |
822
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 |
#include <iostream> int x = 0, y = 0; int res = 0; void dfs(int n, int m) { if (n == x && m == y) { res++; return; } else { if (n < x) { dfs(n+1, m); } if (m < y) { dfs(n, m+1); } } } int main() { std::cin >> x >> y; dfs(0, 0); std::cout << res << std::endl; return 0; } |
823
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 |
#include <iostream> int x = 0; void dfs(int r, int nums[], bool flags[]) { if (r > x) { for (int i = 1; i <= x; ++i) { std::cout << nums[i] << " "; } std::cout << std::endl; } else { for (int i = 1; i <= x; ++i) { if (flags[i] == false) { flags[i] = true; nums[r] = i; dfs(r+1, nums, flags); flags[i] = false; } } } } int main() { std::cin >> x; int nums[10] = {0}; bool flags[10] = {false}; dfs(1, nums, flags); return 0; } |
本文为原创文章,版权归Aet所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ 【LeetCode-30 天 JavaScript 挑战】07/23
- ♥ 【LeetCode-Apr-链表二】04/02
- ♥ 【AcWing 语法基础课 第二讲】02/17
- ♥ 【AcWing 语法基础课 第三讲】02/22
- ♥ 【LeetCode-April】04/02
- ♥ 【AcWing 语法基础课 第一讲】02/14