文章目录
例题
1. 804.n的阶乘
#include<iostream>
using namespace std;
int fact(int n)
{
if(n == 1) return 1;
return n * fact(n - 1);
}
int main()
{
int n;
cin >> n;
cout << fact(n) <<endl;
return 0;
}
2. 805.x和y的最大值
#include<iostream>
using namespace std;
int max(int x,int y)
{
return x>y?x:y;
}
int main()
{
int n, m;
cin >> n >> m;
cout << max(n,m) << endl;
return 0;
}
3. 808.最大公约数
#include<iostream>
using namespace std;
int gcd(int a, int b)
{
for(int i = 1000; i > 0; i--)// 直接进行计算
if(a % i == 0 && b % i == 0)
return i;
return 0;
}
int main()
{
int n, m;
cin >> n >> m;
cout << gcd(n, m) << endl;
return 0;
}
4. 811.交换数值
#include<iostream>
using namespace std;
void swap(int &x, int &y)
{
int t = x;
x = y;
y = t;
}
int main()
{
int n, m;
cin >> n >> m;
swap(n, m);
cout << n << ' '<< m << endl;
return 0;
}
5. 812.打印数字
#include<iostream>
using namespace std;
const int N = 1010;
int a[N];
void print(int a[], int size)
{
for(int i = 0; i < size;i ++)
cout << a[i] << ' ';
cout << endl;
}
int main()
{
int n, size;
cin >> n >> size;
for(int i = 0; i < n; i++) cin >> a[i];
print(a,size);// 这里直接写数组名就可以
return 0;
}
6. 813.打印矩阵
#include<iostream>
using namespace std;
void print2D(int a[][100], int row, int col)
{
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
cout << a[i][j] << ' ';
}
cout << endl;
}
}
int main()
{
int row, col;
cin >> row >> col;
int a[100][100];
for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++)
cin >> a[i][j];
print2D(a, row, col);
return 0;
}
7. 819.递归求阶乘
#include<iostream>
using namespace std;
int fact(int n)
{
if(n == 1) return 1;
else return n * fact(n - 1);
}
int main()
{
int n;
cin >> n;
cout << fact(n) << endl;
return 0;
}
8. 820.递归求斐波那契数列
- 树的深度优先遍历
#include<iostream>
using namespace std;
int f(int n)
{
if(n <= 2) return 1;
return f(n - 1) + f(n - 2);
}
int main()
{
int n;
cin >> n;
cout << f(n) << endl;
return 0;
}
习题
1. 810.绝对值
#include<iostream>
using namespace std;
int abs(int x)
{
if(x >= 0) return x;
else return -x;
}
int main()
{
int x;
cin >> x;
cout << abs(x) << endl;
return 0;
}
2. 806.两个数的和
#include<iostream>
using namespace std;
double add(double x, double y)
{
return x + y;
}
int main()
{
double a, b;
cin >> a >> b;
printf("%.2lf", add(a,b));
return 0;
}
3. 807.区间求和
#include<iostream>
using namespace std;
int sum(int l, int r)
{
int s = 0;// 需要先赋值为0
for(int i = l; i <= r; i++)
{
s += i;
}
return s;
}
int main()
{
int a, b;
cin >> a >> b;
cout << sum(a,b) << endl;
return 0;
}
4. 809.最小公倍数
#include<iostream>
using namespace std;
int lcm(int a, int b)
{
for(int i = 1;i <= a * b; i++)
{
if(i % a == 0 && i % b == 0)
return i;
}
return -1;
}
int main()
{
int a, b;
cin >> a >> b;
cout << lcm(a,b) << endl;
return 0;
}
5. 814.复制数组
#include<iostream>
#include<cstring>
using namespace std;
const int N = 110;
int a[N], b[N];
void copy(int a[], int b[], int size)
{
for(int i = 0; i < size; i++) b[i] = a[i];
// memcpy(b, a, size * 4);第三个参数是多少个字节,int类型每个是4个字节,加头文件(cstring)
}
int main()
{
int n, m, size;
cin >> n >> m >> size;
for(int i = 0; i < n; i++) cin >> a[i];
for(int j = 0; j < m; j++) cin >> b[j];
copy(a, b, size);
for(int i = 0; i < m; i++) cout << b[i] << ' ';
cout << endl;
return 0;
}
6. 815.打印字符串(重难题)
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
void print(char str[])
{
// puts(str);// 调用函数直接打印,会输出回车
printf("%s",str);
}
int main()
{
char str[110];
// 多输入回车,中间的是101
// fgets(str,101,stdin);// 在字符串最后加回车"\n",使用puts函数会输出回车,printf不会输出回车
// 或者直接用getline
cin.getline(str, 101);
print(str);
return 0;
}
7. 816.数组翻转
#include<iostream>
using namespace std;
const int N = 1010;
int a[N];
void reverse(int a[], int size)
{
for(int i = 0; i < size / 2; i++)
{
int t;
t = a[size - 1 - i];
a[size - 1 - i] = a[i];
a[i] = t;
}
// 或者用现在的数据
// for(int i = 0, j = size - 1; i < j; i++, j--)
// swap(a[i], a[j]);
}
}
int main()
{
int n, size;
cin >> n >> size;
for(int i = 0; i < n; i++) cin >> a[i];
reverse(a, size);
for(int i = 0; i < n; i++) cout << a[i] << ' ';
return 0;
}
8. 817.数组去重(难题)
Acwing 817.数组去重
使用STL中的sort和unique函数进行去重
#include<iostream>
using namespace std;
const int N = 1010;
int a[N];
int get_unique_count(int a[], int n)// 返回数组前n个数中的不同数的个数
{
int cnt = 0;// 不同数字的个数
for(int i = 0; i < n; i++)// 枚举每个数字
{
bool is_exist = false;// 判断前面是否出现与当前数相同的数字,出现的话用true,没出现用false
for(int j = 0; j < i; j++)
{
if(a[j] == a[i])
{
is_exist = true;// 这里是直接判断true
break;
}
}
if(!is_exist) cnt ++;
}
return cnt;
}
int main()
{
int m;
cin >> m;
for(int i = 0; i < m; i++) cin >> a[i];
cout << get_unique_count(a, m) << endl;
return 0;
}