1 变量、输入输出、表达式与顺序语句
AcWing 1. A + B
#include<iostream>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
cout << a + b << endl;
return 0;
}
AcWing 608. 差
#include<iostream>
using namespace std;
int main()
{
int a, b, c, d;
cin >> a >> b >> c >> d;
int x;
x = (a * b - c * d);
cout << "DIFERENCA = " << x << endl;
return 0;
}
AcWing 604. 圆的面积
#include<iostream>
using namespace std;
#define pi 3.14159
int main()
{
double r;
cin >> r;
double a;
a = pi * r * r;
printf("A=%.4lf\n", a);
return 0;
}
AcWing 606. 平均数
#include <iostream>
using namespace std;
int main()
{
double a, b;
cin >> a >> b;
double x;
x = a * (3.5/11) + b * (7.5/11);
printf("MEDIA = %.5f\n", x);
return 0;
}
AcWing 609. 工资
#include<iostream>
using namespace std;
int main()
{
int num, hour;
double hourMoney;
cin >> num >> hour >> hourMoney;
double money;
money = hourMoney * hour;
printf("NUMBER = %d\nSALARY = U$ %.2lf", num, money);
return 0;
}
AcWing 615. 油耗
#include <iostream>
using namespace std;
int main()
{
int length;
double oil;
cin >> length >> oil;
double h;
h = length / oil;
printf("%.3f km/l\n", h);
return 0;
}
AcWing 616. 两点间的距离
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
double length;
length = sqrt(pow(x2-x1, 2) + pow(y2-y1, 2));
//cout << length << endl;
printf("%.4f", length);
return 0;
}
AcWing 653. 钞票
#include<iostream>
using namespace std;
int main()
{
int money;
cin >> money;
int n = money;
int bai, wushi, ershi, shi, wu, er, yi;
bai = money / 100;
money -= bai * 100;
wushi = money / 50;
money -= wushi * 50;
ershi = money / 20;
money -= ershi * 20;
shi = money / 10;
money -= shi * 10;
wu = money / 5;
money -= wu * 5;
er = money / 2;
money -= er * 2;
yi = money;
printf("%d\n%d nota(s) de R$ 100,00\n%d nota(s) de R$ 50,00\n%d nota(s) de R$ 20,00\n%d nota(s) de R$ 10,00\n%d nota(s) de R$ 5,00\n%d nota(s) de R$ 2,00\n%d nota(s) de R$ 1,00", n, bai, wushi, ershi, shi, wu, er, yi);
return 0;
}
AcWing 654. 时间转换
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int hours, minutes, seconds;
hours = n / (60 * 60);
n -= hours * 60 * 60;
minutes = n / 60;
n -= minutes * 60;
seconds = n;
printf("%d:%d:%d\n", hours, minutes, seconds);
return 0;
}
AcWing 605. 简单乘积
#include <iostream>
using namespace std;
int main()
{
int a, b;
long long c;
cin >> a >> b;
c = a * b;
cout << "PROD = " << c << endl;
return 0;
}
AcWing 611. 简单计算
#include<iostream>
using namespace std;
int main()
{
int num1, num2, a, b;
double m1, m2;
cin >> num1 >> a >> m1 >> num2 >> b >> m2;
double money;
money = a * m1 + b * m2;
printf("VALOR A PAGAR: R$ %.2f", money);
return 0;
}
AcWing 612. 球的体积
#include<iostream>
#include<cmath>
using namespace std;
const double pi = 3.14159;
int main()
{
double r;
cin >> r;
double v;
v = (4/3.0) * pi * pow(r, 3);
printf("VOLUME = %.3f\n", v);
return 0;
}
AcWing 613. 面积
#include<iostream>
#include<cmath>
using namespace std;
const double pi = 3.14159;
int main()
{
double a, b, c;
cin >> a >> b >> c;
double san, circle, ti, zheng, chang;
san = 0.5 * a * c;
circle = pi * pow(c, 2);
ti = 0.5 * (a + b) * c;
zheng = b * b;
chang = a * b;
printf("TRIANGULO: %.3f\nCIRCULO: %.3f\nTRAPEZIO: %.3f\nQUADRADO: %.3f\nRETANGULO: %.3f\n", san, circle, ti, zheng, chang);
return 0;
}
AcWing 607. 平均数
#include<iostream>
using namespace std;
int main()
{
double a, b, c;
cin >> a >> b >> c;
double x;
x = a * 0.2 + b * 0.3 + c * 0.5;
printf("MEDIA = %.1f\n", x);
return 0;
}
AcWing 610. 工资和奖金
#include<iostream>
#include<string>
using namespace std;
int main()
{
string name;
cin >> name;
double a, b;
cin >> a >> b;
printf("TOTAL = R$ %.2f", a + b * 0.15);
return 0;
}
AcWing 614. 最大值
#include<iostream>
#include<cstdlib>
//#include<cmath>
using namespace std;
int main()
{
int a, b, c;
cin >> a >> b >> c;
int max;
max = (a + b + abs(a - b)) / 2;
max = (max + c + abs(max - c)) / 2;
cout << max << " eh o maior" << endl;
return 0;
}
AcWing 617. 距离
#include <iostream>
using namespace std;
int main()
{
int l;
cin >> l;
cout << 2 * l << " minutos" << endl;
return 0;
}
AcWing 618. 燃料消耗
#include<iostream>
using namespace std;
int main()
{
double t, s;
cin >> t >> s;
double oil;
oil = s * t /12;
printf("%.3f\n", oil);
return 0;
}
AcWing 656. 钞票和硬币
#include<iostream>
using namespace std;
int main()
{
double m;
scanf("%lf", &m);
int n = m * 100;
printf("NOTAS:\n");
printf("%d nota(s) de R$ 100.00\n", n / 10000); n %= 10000;
printf("%d nota(s) de R$ 50.00\n", n / 5000);n %= 5000;
printf("%d nota(s) de R$ 20.00\n", n / 2000);n %= 2000;
printf("%d nota(s) de R$ 10.00\n", n /1000);n %= 1000;
printf("%d nota(s) de R$ 5.00\n", n / 500);n %= 500;
printf("%d nota(s) de R$ 2.00\n", n / 200);n %= 200;
printf("MOEDAS:\n");
printf("%d moeda(s) de R$ 1.00\n", n / 100);n %= 100;
printf("%d moeda(s) de R$ 0.50\n", n / 50);n %= 50;
printf("%d moeda(s) de R$ 0.25\n", n / 25);n %= 25;
printf("%d moeda(s) de R$ 0.10\n", n / 10);n %= 10;
printf("%d moeda(s) de R$ 0.05\n", n / 5);n %= 5;
printf("%d moeda(s) de R$ 0.01\n", n);
return 0;
}
AcWing 655. 天数转换
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int year, month, day;
year = n /365;
n %= 365;
month = n / 30;
n %= 30;
day = n;
printf("%d ano(s)\n%d mes(es)\n%d dia(s)\n", year, month, day);
return 0;
}
2 判断语句
AcWing 665. 倍数
#include<iostream>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
if (a % b == 0 || b % a == 0)
{
cout << "Sao Multiplos" << endl;
}
else
{
cout << "Nao sao Multiplos" << endl;
}
return 0;
}
AcWing 660. 零食
#include<iostream>
using namespace std;
int main()
{
int x;
double y;
cin >> x >> y;
switch(x)
{
case 1:printf("Total: R$ %.2f",4.00 * y);break;
case 2:printf("Total: R$ %.2f",4.50 * y);break;
case 3:printf("Total: R$ %.2f",5.0 * y);break;
case 4:printf("Total: R$ %.2f",2.0 * y);break;
case 5:printf("Total: R$ %.2f",1.5 * y);break;
}
return 0;
}
AcWing 659. 区间
#include<iostream>
using namespace std;
int main()
{
double n;
cin >> n;
if (n < 0 || n > 100)
{
cout << "Fora de intervalo" << endl;
}
else if (n <= 25 && n >= 0)
{
cout << "Intervalo [0,25]" << endl;
}
else if (n <= 50 && n > 25)
{
cout << "Intervalo (25,50]" << endl;
}
else if (n <= 75 && n > 50)
{
cout << "Intervalo (50,75]" << endl;
}
else if (n <= 100 && n > 75)
{
cout << "Intervalo (75,100]" << endl;
}
return 0;
}
AcWing 664. 三角形
#include<iostream>
using namespace std;
int main()
{
double a, b, c;
cin >> a >> b >> c;
if (a + b > c && a + c > b && b + c > a)
{
printf("Perimetro = %.1f\n", a + b + c);
}
else
{
printf("Area = %.1f\n", 0.5 * (a + b) * c);
}
return 0;
}
AcWing 667. 游戏时间
#include<iostream>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
int c;
if (a >= b)
{
c = b + 24 - a;
}
else
{
c = b - a;
}
printf("O JOGO DUROU %d HORA(S)", c);
return 0;
}
AcWing 669. 加薪
#include<iostream>
using namespace std;
int main()
{
double n, t, rate;
cin >> n;
if (n >= 0 && n <= 400)
{
rate = 15;
t = n * 0.15;
n += t;
}
else if (n > 400 && n <= 800)
{
rate = 12;
t = n * 0.12;
n += t;
}
else if (n > 800 && n <= 1200)
{
rate = 10;
t = n * 0.1;
n += t;
}
else if (n > 1200 && n <= 2000)
{
rate = 7;
t = n * 0.07;
n += t;
}
else if(n > 2000)
{
rate = 4;
t = n * 0.04;
n += t;
}
printf("Novo salario: %.2f\n", n);
printf("Reajuste ganho: %.2f\n", t);
printf("Em percentual: %.0f %\n", rate);
return 0;
}
AcWing 670. 动物
#include<iostream>
#include<string>
using namespace std;
int main()
{
string a, b, c;
cin >> a >> b >> c;
if (a == "vertebrado")
{
if (b == "ave")
{
if (c == "carnivoro")
{
cout << "aguia" << endl;
}
else
{
cout << "pomba" << endl;
}
}
else
{
if (c == "onivoro")
{
cout << "homem" << endl;
}
else
{
cout << "vaca" << endl;
}
}
}
else
{
if (b == "inseto")
{
if (c == "herbivoro")
{
cout << "lagarta" << endl;
}
else
{
cout << "pulga" << endl;
}
}
else
{
if (c == "onivoro")
{
cout << "minhoca" << endl;
}
else
{
cout << "sanguessuga" << endl;
}
}
}
return 0;
}
AcWing 657. 选择练习
#include<iostream>
using namespace std;
int main()
{
int a, b, c, d, t = 0;
cin >> a >> b >> c >> d;
if (b <= c)
{
t = 1;
}
if (d <= a)
{
t = 1;
}
if (c + d <= a + b)
{
t = 1;
}
if (c < 0 || d < 0)
{
t = 1;
}
if (a % 2 == 1)
{
t = 1;
}
if (t == 0)
{
cout << "Valores aceitos" << endl;
}
else
{
cout << "Valores nao aceitos" << endl;
}
return 0;
}
AcWing 671. DDD
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
switch(n)
{
case 61: cout << "Brasilia" << endl;break;
case 11: cout << "Sao Paulo" << endl;break;
case 19: cout << "Campinas" << endl;break;
case 21: cout << "Rio de Janeiro" << endl; break;
case 27: cout << "Vitoria" << endl; break;
case 31: cout << "Belo Horizonte" << endl; break;
case 32: cout << "Juiz de Fora" << endl; break;
case 71: cout << "Salvador" << endl; break;
default:
cout << "DDD nao cadastrado" << endl;
}
return 0;
}
AcWing 662. 点的坐标
#include<iostream>
using namespace std;
int main()
{
double x, y;
cin >> x >> y;
if (x > 0 && y >0) cout << "Q1" << endl;
else if (x < 0 && y > 0) cout << "Q2" << endl;
else if (x < 0 && y < 0) cout << "Q3" << endl;
else if (x > 0 && y <0) cout << "Q4" << endl;
else
{
if (!x && !y) cout << "Origem" << endl;
else if (!y) cout << "Eixo X" << endl;
else cout << "Eixo Y" << endl;
}
return 0;
}
AcWing 666. 三角形类型
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double a, b, c;
cin >> a >> b >> c;
if (a < b)
{
a = a + b;
b = a - b;
a = a - b;
}
if (a < c)
{
a = a + c;
c = a - c;
a = a - c;
}
if (b < c)
{
b = b + c;
c = b - c;
b = b - c;
}
if (a >= b + c)
{
cout << "NAO FORMA TRIANGULO" << endl;
}
else
{
if (pow(a, 2) == pow(b, 2) + pow(c, 2))
{
cout << "TRIANGULO RETANGULO" << endl;
}
else if (pow(a, 2) > pow(b, 2) + pow(c, 2))
{
cout << "TRIANGULO OBTUSANGULO" << endl;
}
else if (pow(a, 2) < pow(b, 2) + pow(c, 2))
{
cout << "TRIANGULO ACUTANGULO" << endl;
}
if (a == b && b == c && a == c)
{
cout << "TRIANGULO EQUILATERO" << endl;
}
else if (a == b || b == c || a == c)
{
cout << "TRIANGULO ISOSCELES" << endl;
}
}
return 0;
}
AcWing 668. 游戏时间2
#include<iostream>
using namespace std;
int main()
{
int a, b, c, d;
cin >> a >> b >> c >> d;
int start = a * 60 + b;
int end = c * 60 + d;
int spare_time = end - start;
if (spare_time <= 0) spare_time += 24 * 60; // 注意等号,两者时间相同时,是经过了24小时。
printf("O JOGO DUROU %d HORA(S) E %d MINUTO(S)", spare_time / 60, spare_time % 60);
return 0;
}
AcWing 672. 税
#include<iostream>
using namespace std;
int main()
{
double n;
cin >> n;
double sum = 0;
if (n > 2000)
{
double y = 3000;
if (n < y) y = n;
sum += (y - 2000) * 0.08;
}
if (n > 3000)
{
double y = 4500;
if (n < y) y = n;
sum += (y - 3000) * 0.18;
}
if (n > 4500)
{
sum += (n - 4500) * 0.28;
}
if (sum == 0) cout << "Isento" << endl;
else printf("R$ %.2f", sum);
return 0;
}
AcWing 663. 简单排序
#include<iostream>
using namespace std;
int main()
{
int a, b, c;
cin >> a >> b >> c;
int x = a, y = b, z = c;
if (a > b)
{
b = a + b;
a = b - a;
b = b - a;
}
if (a > c)
{
a = a + c;
c = a - c;
a = a - c;
}
if (b > c)
{
b = b + c;
c = b - c;
b = b - c;
}
cout << a << endl << b << endl << c << endl << endl;
cout << x << endl << y << endl << z << endl;
return 0;
}
AcWing 658. 一元二次方程公式
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double a, b, c;
cin >> a >> b >> c;
if (pow(b,2) < 4 * a * c || a == 0)
{
cout << "Impossivel calcular" << endl;
}
else
{
double x1, x2;
x1 = (-b - sqrt(b * b - 4 * a * c))/(2 *a);
x2 = (-b + sqrt(b * b - 4 * a * c))/(2 *a);
printf("R1 = %.5f\n", x2);
printf("R2 = %.5f\n", x1);
}
return 0;
}
AcWing 661. 平均数3
#include<iostream>
using namespace std;
int main()
{
double a, b, c, d, t;
cin >> a >> b >> c >> d;
double average;
average = a * 0.2 + b * 0.3 + c * 0.4 + d * 0.1;
if (average >= 7)
{
printf("Media: %.1f\n", average);
cout << "Aluno aprovado." << endl;
}
else if (average < 7 && average >= 5)
{
printf("Media: %.1f\n", average);
cout << "Aluno em exame." << endl;
cin >> t;
cout << "Nota do exame: " << t << endl;
average = (average + t) / 2;
if (average >= 5)
{
cout << "Aluno aprovado." << endl;
}
else
{
cout << "Aluno reprovado." << endl;
}
printf("Media final: %.1f", average);
}
else if (average < 5)
{
printf("Media: %.1f\n", average);
cout << "Aluno reprovado." << endl;
}
return 0;
}
3 循环语句
AcWing 708. 偶数
#include<iostream>
using namespace std;
int main()
{
for (int i = 1;i <= 100; i++)
{
if ( i % 2 == 0)
{
cout << i << endl;
}
}
return 0;
}
AcWing 709. 奇数
#include<iostream>
using namespace std;
int main()
{
int x;
cin >> x;
for (int i = 1;i <= x; i++)
{
if ( i % 2 != 0)
{
cout << i << endl;
}
}
return 0;
}
AcWing 712. 正数
#include<iostream>
using namespace std;
int main()
{
double x;
int t = 0;
for (int i = 0;i < 6; i++)
{
cin >> x;
if (x > 0)
{
t ++;
}
}
cout << t << " positive numbers" << endl;
return 0;
}
AcWing 714. 连续奇数的和
#include<iostream>
using namespace std;
int main()
{
int x, y, sum = 0;
cin >> x >> y;
if (x > y)
{
x = x + y;
y = x - y;
x = x - y;
}
for (int i = x + 1;i < y; i++)
{
if (i % 2 != 0)
{
sum += i;
}
}
cout << sum << endl;
return 0;
}
AcWing 716. 最大数和它的位置
#include<iostream>
using namespace std;
int main()
{
int index, max = 0, x;
for (int i = 1;i <= 100; i++)
{
cin >> x;
if (max < x)
{
max = x;
index = i;
}
}
cout << max << endl << index << endl;
return 0;
}
AcWing 721. 递增序列
#include<iostream>
using namespace std;
int main()
{
int x;
while (1)
{
cin >> x;
if (!x) break;
for (int i = 1;i <= x; i++)
{
cout << i << " ";
}
cout << endl;
}
return 0;
}
AcWing 720. 连续整数相加
#include<iostream>
using namespace std;
int main()
{
int a, n;
cin >> a;
while (1)
{
cin >> n;
if (n > 0) break;
}
int sum;
for (int i = a;i < a + n; i++)
{
sum += i;
}
cout << sum << endl;
return 0;
}
AcWing 724. 约数
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
for (int i = 1;i <= n; i++)
{
if (n % i == 0)
cout << i << endl;
}
return 0;
}
AcWing 723. PUM
#include<iostream>
using namespace std;
int main()
{
int n, m, t = 0;
cin >> n >> m;
for (int i = 1;i <= n; i++)
{
for (int j = 1;j < m; j++)
{
cout << ++t << " ";
}
t++;
cout << "PUM" << endl;
}
return 0;
}
AcWing 715. 余数
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
for (int i = 2; i < 10000; i++ )
{
if (i % n == 2)
{
cout << i << endl;
}
}
return 0;
}
AcWing 710. 六个奇数
#include<iostream>
using namespace std;
int main()
{
int n, t = 0;
cin >> n;
// 我的思路
// while (1)
// {
// if (n % 2)
// {
// cout << n << endl;
// t ++;
// }
// n ++;
// if (t == 6) break;
// }
// 大佬的思路
if (n % 2 == 0) n ++ ;
for (int i = 0; i < 6; i ++ ) cout << n + i * 2 << endl;
return 0;
}
AcWing 711. 乘法表
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
for (int i = 1; i <= 10; i++ )
{
printf("%d x %d = %d\n", i, n, i * n);
}
return 0;
}
AcWing 718. 实验
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
double coneys = 0, rats = 0, frogs = 0;
int sum = 0;
for ( int i = 0;i < n; i++)
{
int num;
char animal;
cin >> num >> animal;
if (animal == 'C')
{
coneys += num;
}
if (animal == 'R')
{
rats += num;
}
if (animal == 'F')
{
frogs += num;
}
sum += num;
}
double rates1, rates2, rates3;
rates1 = coneys / sum;
rates2 = rats / sum;
rates3 = frogs / sum;
printf("Total: %d animals\n", sum);
printf("Total coneys: %.0f\n", coneys);
printf("Total rats: %.0f\n", rats);
printf("Total frogs: %.0f\n", frogs);
printf("Percentage of coneys: %.2f %\n", rates1 * 100);
printf("Percentage of rats: %.2f %\n", rates2 * 100);
printf("Percentage of frogs: %.2f %\n", rates3 * 100);
return 0;
}
AcWing 713. 区间2
#include<iostream>
using namespace std;
int main()
{
int n, x;
cin >> n;
int in = 0;
for (int i = 0; i < n; i++)
{
cin >> x;
if ( x <= 20 && x >= 10)
{
in ++;
}
}
cout << in << " in" << endl;
cout << n - in << " out" << endl;
return 0;
}
AcWing 719. 连续奇数的和
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
int x, y, sum = 0;
cin >> x >> y;
if (x > y)
{
x = x + y;
y = x - y;
x = x - y;
}
for (int j = x + 1; j < y; j++)
{
if (j % 2)
sum += j;
}
cout << sum << endl;
}
return 0;
}
AcWing 717. 简单斐波那契
#include<iostream>
using namespace std;
int main()
{
int x1 = 0, x2 = 1, t;
int n;
cin >> n;
if (n < 3)
{
for (int i = 0; i < n; i++)
{
cout << i << " ";
}
}
else
{
cout << "0 1";
for (int i = 2; i < n; i++)
{
t = x1 + x2;
cout << " " << t;
x1 = x2;
x2 = t;
}
}
return 0;
}
AcWing 722. 数字序列和它的和
#include<iostream>
using namespace std;
int main()
{
while (1)
{
int m, n, sum = 0;
cin >> m >> n;
if (m <=0 || n <= 0) break;
if (m > n)
{
m = m + n;
n = m - n;
m = m - n;
}
for (int i = m; i <= n; i++)
{
sum += i;
cout << i << " ";
}
cout << "Sum=" << sum << endl;
}
return 0;
}
AcWing 725. 完全数
#include<iostream>
using namespace std;
int main()
{
int n, x;
cin >> n;
for (int i = 0; i < n; i++)
{
int sum = 0, x;
cin >> x;
for (int j = 1; j * j <= x; j++)
{
if (x % j == 0)
{
if (j < x) sum += j;
if (j != x / j && x / j < x) sum += x / j; // 这个怎么理解,,我抄,
}
}
if (sum == x && x != 1) cout << x << " is perfect" << endl;
else cout << x << " is not perfect" << endl;
}
return 0;
}
AcWing 726. 质数
#include<iostream>
using namespace std;
int main()
{
int n, x;
cin >> n;
for (int i = 0; i < n; i++)
{
int sum = 0, x, t = 0;
cin >> x;
for (int j = 2; j * j <= x; j++)
{
if (x % j == 0) t = 1;
}
if (t == 0) cout << x << " is prime" << endl;
else cout << x << " is not prime" << endl;
}
return 0;
}
AcWing 727. 菱形
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int cx = n / 2, cy = n / 2;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (abs(i - cx) + abs(j - cy) <= n / 2) cout << '*';
else cout << ' ';
}
cout << endl;
}
return 0;
}
4 数组
AcWing 737. 数组替换
#include<iostream>
using namespace std;
int main()
{
int x[10];
for ( int i = 0; i < 10; i++)
{
cin >> x[i];
if (x[i] <= 0)
{
x[i] = 1;
}
printf("X[%d] = %d\n", i, x[i]);
}
return 0;
}
AcWing 738. 数组填充
#include<iostream>
using namespace std;
int main()
{
int v, x[10];
cin >> v;
for (int i = 0; i < 10; i++)
{
x[i] = v;
v *= 2;
printf("N[%d] = %d\n", i, x[i]);
}
return 0;
}
AcWing 739. 数组选择
#include<iostream>
using namespace std;
int main()
{
double x[100];
for (int i = 0; i< 100; i++)
{
cin >> x[i];
if ( x[i] <= 10)
printf("A[%d] = %.1f\n", i, x[i]);
}
return 0;
}
AcWing 743. 数组中的行
#include<iostream>
using namespace std;
int main()
{
double x[12][12];
int l;
char p;
cin >> l >> p;
for (int i = 0; i < 12; i++)
{
for (int j = 0; j < 12; j++)
{
cin >> x[i][j];
}
}
double sum = 0;
for (int i = 0; i < 12; i++)
{
sum += x[l][i];
}
if (p == 'S')
{
printf("%.1f\n", sum);
}
if (p == 'M')
{
printf("%.1f\n", sum / 12);
}
return 0;
}
AcWing 745. 数组的右上半部分
#include<iostream>
using namespace std;
int main()
{
char p;
cin >> p;
double m[12][12], sum = 0;
for (int i = 0; i < 12; i++)
{
for (int j = 0; j < 12; j++)
{
cin >> m[i][j];
}
}
int c = 0;
for (int i = 0; i < 12; i ++ )
for (int j = i + 1; j < 12; j ++ )
{
c ++ ;
sum += m[i][j];
}
if (p == 'S')
printf("%.1f\n", sum);
if (p == 'M')
printf("%.1f\n", sum / c);
return 0;
}
AcWing 747. 数组的左上半部分
#include<iostream>
using namespace std;
int main()
{
char p;
cin >> p;
double m[12][12], sum = 0;
int c = 0;
for (int i = 0; i < 12; i++)
{
for (int j = 0; j < 12; j++)
{
cin >> m[i][j];
if (i + j < 11)
{
c ++ ;
sum += m[i][j];
}
}
}
if (p == 'S')
printf("%.1f\n", sum);
if (p == 'M')
printf("%.1f\n", sum / c);
return 0;
}
AcWing 749. 数组的上方区域
#include<iostream>
using namespace std;
int main()
{
char p;
cin >> p;
double m[12][12], sum = 0;
for (int i = 0; i < 12; i++)
{
for (int j = 0; j < 12; j++)
{
cin >> m[i][j];
}
}
int c = 0;
for (int i = 0; i < 12; i ++ )
for (int j = i + 1; j < 12; j ++ )
{
if (i + j < 11)
{
c ++ ;
sum += m[i][j];
}
}
if (p == 'S')
printf("%.1f\n", sum);
if (p == 'M')
printf("%.1f\n", sum / c);
return 0;
}
AcWing 751. 数组的左方区域
#include<iostream>
using namespace std;
int main()
{
char p;
cin >> p;
double m[12][12], sum = 0;
int c = 0;
for (int i = 0; i < 12; i++)
{
for (int j = 0; j < 12; j++)
{
cin >> m[i][j];
}
}
for (int i = 0; i <= 5; i++)
{
for (int j = 0; j <= i - 1; j ++)
{
c ++;
sum += m[i][j];
}
}
for (int i = 6; i <= 10; i++)
{
for (int j = 0; j <= 10 - i; j++)
{
c ++;
sum += m[i][j];
}
}
if (p == 'S')
printf("%.1f\n", sum);
if (p == 'M')
printf("%.1f\n", sum / c);
return 0;
}
AcWing 753. 平方矩阵
#include<iostream>
using namespace std;
int main()
{
int n;
while (cin >> n, n)
{
for (int i= 1;i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
int up = i, down = n - i + 1, left = j, right = n - j + 1;
cout << min( min(up, down), min(left, right)) << " ";
}
cout << endl;
}
cout << endl;
}
return 0;
}
AcWing 740. 数组变换
#include<iostream>
using namespace std;
int main()
{
int n[20];
for (int i = 0; i < 20; i ++) cin >> n[i];
for (int i = 0; i < 20; i ++) printf("N[%d] = %d\n", i, n[19 - i]);
return 0;
}
AcWing 741. 斐波那契数列
#include<iostream>
using namespace std;
int main()
{
long long n, f[61];
f[0] = 0, f[1] = 1;
cin >> n;
for ( int i = 2; i < 61; i++) f[i] = f[i - 1] + f[i - 2];
for (int i = 0; i < n; i ++)
{
int x;
cin >> x;
printf("Fib(%d) = %lld\n", x, f[x]);
}
return 0;
}
AcWing 742. 最小数和它的位置
#include<iostream>
using namespace std;
#define N 1010
int value = 1010;
int main()
{
int n, x[N], index;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> x[i];
if ( value > x[i] )
{
value = x[i];
index = i;
}
}
printf("Minimum value: %d\nPosition: %d\n", value, index);
return 0;
}
AcWing 744. 数组中的列
#include<iostream>
using namespace std;
int main()
{
int c;
char p;
cin >> c >> p;
double M[12][12], sum = 0, t = 0;
for (int i = 0; i < 12; i++)
{
for (int j = 0; j < 12; j++)
{
cin >> M[12][12];
if (c == j)
{
t += 1;
sum += M[12][12];
}
}
}
if (p == 'S') printf("%.1f", sum);
else printf("%.1f", sum / t);
return 0;
}
AcWing 748. 数组的右下半部分
#include<iostream>
using namespace std;
int main()
{
char p;
cin >> p;
double m[12][12], sum = 0, c = 0;
for (int i = 0; i < 12; i++)
{
for (int j = 0; j < 12; j++)
{
cin >> m[i][j];
if (i + j > 11)
{
c += 1;
sum += m[i][j];
}
}
}
if (p == 'S') printf("%.1f\n", sum);
else printf("%.1f\n", sum / c);
return 0;
}
AcWing 746. 数组的左下半部分
#include<iostream>
using namespace std;
int main()
{
char p;
cin >> p;
double m[12][12], sum = 0, c = 0;
for (int i = 0; i < 12; i++)
{
for (int j = 0; j < 12; j++)
{
cin >> m[i][j];
}
}
for (int i = 0; i < 12; i++)
{
for (int j = 0; j < i; j++)
{
c += 1;
sum += m[i][j];
}
}
if (p == 'S') printf("%.1f\n", sum);
else printf("%.1f\n", sum / c);
return 0;
}
AcWing 750. 数组的下方区域
#include<iostream>
using namespace std;
int main()
{
char p;
cin >> p;
double m[12][12], sum = 0, c = 0;
for (int i = 0; i < 12; i++)
{
for (int j = 0; j < 12; j++)
{
cin >> m[i][j];
}
}
for (int i = 0; i < 12; i++)
{
for (int j = 0; j < i; j++)
{
if (i + j > 11)
{
c += 1;
sum += m[i][j];
}
}
}
if (p == 'S') printf("%.1f\n", sum);
else printf("%.1f\n", sum / c);
return 0;
}
AcWing 752. 数组的右方区域
#include<iostream>
using namespace std;
int main()
{
char p;
cin >> p;
double m[12][12], sum = 0;
for (int i = 0; i < 12; i++)
{
for (int j = 0; j < 12; j++)
{
cin >> m[i][j];
}
}
int c = 0;
for (int i = 0; i < 12; i ++ )
for (int j = i + 1; j < 12; j ++ )
{
if (i + j > 11)
{
c ++ ;
sum += m[i][j];
}
}
if (p == 'S')
printf("%.1f\n", sum);
if (p == 'M')
printf("%.1f\n", sum / c);
return 0;
}
AcWing 754. 平方矩阵
#include<iostream>
using namespace std;
int q[100][100];
int main()
{
int n;
while (cin >> n, n)
{
for (int i = 0; i < n; i++)
{
q[i][i] = 1;
for (int j = i + 1, k = 2; j < n; j++, k++) q[i][j] = k;
for (int j = i + 1, k = 2; j < n; j++, k++) q[j][i] = k;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << q[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
return 0;
}
AcWing 755. 平方矩阵
#include<iostream>
#include<cmath>
using namespace std;
int q[100][100];
int main()
{
int n;
// while (cin >> n, n)
// {
// for (int i = 0; i < n; i++)
// {
// q[0][i] = pow(2, i);
// }
// for (int i = 1; i < n; i++)
// {
// for (int j = 0; j < n; j++)
// {
// q[i][j] = q[i - 1][j] * 2;
// }
// }
// for (int i = 0; i < n; i++)
// {
// for (int j = 0; j < n; j++)
// {
// cout << q[i][j] << " ";
// }
// cout << endl;
// }
// cout << endl;
// }
// 大佬思路
while (cin >> n, n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
int v = 1;
for (int k = 0; k < i + j; k ++ ) v *= 2;
cout << v << ' ';
}
cout << endl;
}
cout << endl;
}
return 0;
}
AcWing 756. 蛇形矩阵
#include<iostream>
using namespace std;
int m, n;
int res[100][100];
int main()
{
cin >> n >> m;
int dx[] = {0,1,0,-1}, dy[] = {1,0,-1,0};
for (int x = 0, y = 0, d = 0, k = 1; k <= m * n; k++)
{
res[x][y] = k;
int a = x + dx[d], b = y + dy[d];
if (a < 0 || a >= n || b < 0 || b >= m || res[a][b])
{
d = (d + 1) % 4;
a = x + dx[d], b = y + dy[d];
}
x = a, y = b;
}
for (int i = 0; i < n; i ++ )
{
for (int j = 0; j < m; j ++ ) cout << res[i][j] << ' ';
cout << endl;
}
return 0;
}
4 字符串
AcWing 760. 字符串长度
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char a[101];
fgets(a, 101, stdin);
int c = strlen(a);
cout << c << endl;
return 0;
}
AcWing 761. 字符串中的数字个数
#include<iostream>
using namespace std;
int main()
{
char a[101];
fgets(a, 101, stdin);
int ret = 0;
for (char c: a)
if (c >= 48 && c <= 57)
ret ++;
cout << ret << endl;
return 0;
}
AcWing 763. 循环相克令
#include<iostream>
#include<string.h>
#include<string>
using namespace std;
int main()
{
int n;
cin >> n;
while (n --)
{
string a, b;
cin >> a >> b;
int x, y;
if (a == "Hunter") x = 0;
else if (a == "Bear") x = 1;
else x = 2;
if (b == "Hunter") y = 0;
else if (b == "Bear") y = 1;
else y = 2;
if (x == y) puts("Tie");
else if (x == (y + 1) % 3) puts("Player1");
else puts("Player2");
}
return 0;
}
AcWing 765. 字符串加空格
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
int main()
{
char a[101];
fgets(a, 101, stdin);
int len = strlen(a);
for (int i = 0; i < len; i++)
{
cout << a[i] << " ";
}
return 0;
}
AcWing 769. 替换字符
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char a[31], c;
fgets(a, 31, stdin);
cin >> c;
int len = strlen(a);
for (int i = 0; i < len; i++)
{
if (a[i] == c)
a[i] = '#';
cout << a[i];
}
return 0;
}
AcWing 773. 字符串插入
#include<iostream>
#include<string>
using namespace std;
int main()
{
string a, b;
while (cin >> a >> b)
{
int p = 0;
for (int i = 1; i < a.size(); i++) // 从 1 开始 少比较一次
{
if (a[i] > a[p])
p = i;
}
cout << a.substr(0, p + 1) + b + a.substr(p + 1) << endl;
}
return 0;
}
AcWing 772. 只出现一次的字符
#include<iostream>
using namespace std;
int cnt[26];
char str[100010];
int main()
{
cin >> str;
for (int i = 0; str[i]; i++) cnt[str[i] - 'a'] ++;
for (int i = 0; str[i]; i++)
if (cnt[str[i] - 'a'] == 1)
{
cout << str[i] << endl;
return 0;
}
printf("no");
return 0;
}
AcWing 762. 字符串匹配
#include<iostream>
#include<string>
using namespace std;
int main()
{
double k, ret = 0;
string a, b;
cin >> k;
cin >> a >> b;
for (int i = 0; i < a.size(); i++)
{
if (a[i] == b[i])
ret += 1;
}
double rare;
rare = ret / a.size();
if (rare >= k) cout << "yes" << endl;
else cout << "no" << endl;
return 0;
}
AcWing 768. 忽略大小写比较字符串大小
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int main()
{
char a[100], b[100];
fgets(a, 100, stdin);
fgets(b, 100, stdin);
if (a[strlen(a) - 1] == '\n') a[strlen(a) - 1] = 0; // 去掉末尾回车
if (b[strlen(b) - 1] == '\n') b[strlen(b) - 1] = 0; // 去掉末尾回车
for (int i = 0; a[i]; i ++ )
if (a[i] >= 'A' && a[i] <= 'Z')
a[i] += 32;
for (int i = 0; b[i]; i ++ )
if (b[i] >= 'A' && b[i] <= 'Z')
b[i] += 32;
int t = strcmp(a, b);
if (t == 0) puts("=");
else if (t < 0) puts("<");
else puts(">");
return 0;
}
AcWing 766. 去掉多余的空格
#include <iostream>
using namespace std;
int main()
{
string s;
while (cin >> s) cout << s << ' ' ;
return 0;
}
AcWing 767. 信息加密
#include<iostream>
using namespace std;
int main()
{
char a[110];
fgets(a, 110, stdin);
for (char& c: a)
if (c >= 'a' && c <= 'z') c = (c - 'a' + 1) % 26 + 'a';
else if (c >= 'A' && c <= 'Z') c = (c - 'A' + 1) % 26 + 'A';
cout << a;
return 0;
}
AcWing 764. 输出字符串
#include<iostream>
#include<string>
using namespace std;
int main()
{
string a, b;
getline(cin, a);
for (int i = 0; i < a.size(); i++) b += a[i] + a[(i + 1) % a.size()];
cout << b << endl;
return 0;
}
AcWing 770. 单词替换
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
string s, a, b;
getline(cin, s);
cin >> a >> b;
stringstream ssin(s);
string sin;
while (ssin >> sin)
if (sin == a) cout << b << ' ';
else cout << sin << ' ';
return 0;
}
AcWing 771. 字符串中最长的连续出现的字符
#include<iostream>
#include<string>
using namespace std;
int main()
{
int n;
cin >> n;
while (n --)
{
string str;
cin >> str;
char c;
int cnt = 0;
for (int i = 0; i < str.size (); i++)
{
int j = i + 1; // j = i + 1 也可以
while (j < str.size() && str[j] == str[i]) j ++;
if (j - i > cnt) cnt = j - i, c = str[i];
i = j - 1;
}
cout << c << ' ' << cnt << endl;
}
return 0;
}
AcWing 774. 最长单词
#include<iostream>
using namespace std;
int main()
{
string str, res;
while (cin >> str)
{
if (str.back() == '.') str.pop_back();
if (str.size() > res.size()) res = str;
}
cout << res << endl;
return 0;
}
AcWing 775. 倒排单词
#include<iostream>
using namespace std;
int main()
{
string a,b;
while(cin >> a)
b = a + " " + b;
cout << b;
return 0;
}
AcWing 776. 字符串移位包含问题
#include<iostream>
#include<string>
using namespace std;
int main()
{
string a, b;
cin >> a >> b;
if (a.size() < b.size()) swap(a, b);
for (int i = 0; i < a.size(); i++)
{
a = a.substr(1) + a[0];
for (int j = 0; j + b.size() <= a.size(); j ++ )
{
int k = 0;
for (; k < b.size(); k ++ )
if (a[j + k] != b[k])
break;
if (k == b.size())
{
puts("true");
return 0;
}
}
}
puts("false");
return 0;
}
AcWing 777. 字符串乘方
#include<iostream>
using namespace std;
int main()
{
string str;
while (cin >> str, str != ".")
{
int len = str.size();
for (int n = len; n; n -- )
if (len % n == 0)
{
int m = len / n;
string s = str.substr(0, m);
string r;
for (int i = 0; i < n; i++) r += s;
if (r == str)
{
cout << n << endl;
break;
}
}
}
return 0;
}
AcWing 778. 字符串最大跨距
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s, s1, s2;
char c;
while (cin >> c, c != ',') s += c;
while (cin >> c, c != ',') s1 += c;
while (cin >> c) s2 += c;
if (s.size() < s1.size() || s.size() < s2.size()) puts("-1");
else
{
int l = 0;
while (l + s1.size() < s.size())
{
int k = 0;
while (k < s1.size())
{
if (s[l + k] != s1[k]) break;
k ++;
}
if (k == s1.size()) break;
l ++;
}
int r = s.size() - s2.size();
while (r >= 0)
{
int k = 0;
while (k < s2.size())
{
if (s[r + k] != s2[k]) break;
k ++;
}
if (k == s2.size()) break;
r --;
}
l += s1.size() - 1;
if (l >= r) puts("-1");
else printf("%d\n", r - l - 1);
}
return 0;
}
AcWing 779. 最长公共字符串后缀
#include <iostream>
using namespace std;
const int N = 200;
int n;
string str[N];
int main()
{
while (cin >> n, n)
{
int len = 201;
for (int i = 0; i < n; i ++ )
{
cin >> str[i];
if (len > str[i].size()) len =str[i].size();
}
while (len)
{
bool sucess = true;
for (int i = 1; i < n; i ++ )
{
bool is_same = true;
for (int j = 1; j <= len; j ++ )
if (str[0][str[0].size() - j] != str[i][str[i].size() - j])
{
is_same = false;
break;
}
if (!is_same)
{
sucess = false;
break;
}
}
if (sucess) break;
len --;
}
cout << str[0].substr(str[0].size() - len) << endl;
}
return 0;
}
6 函数
AcWing 804. n 的阶乘
#include<iostream>
using namespace std;
int fact(int n)
{
int mul = 1;
for (int i = 1; i <= n; i ++ )
{
mul *= i;
}
return mul;
}
int main()
{
int n, mul;
cin >> n;
mul = fact(n);
cout << mul << endl;
return 0;
}
AcWing 805. x和y的最大值
#include <iostream>
using namespace std;
int Max(int x, int y)
{
return max(x, y);
}
int main()
{
int x, y;
cin >> x >> y;
cout << Max(x, y) << endl;
return 0;
}
AcWing 808. 最大公约数
#include <iostream>
using namespace std;
int gcd(int a, int b)
{
int c = max(a, b);
while (c --)
{
if (a % c == 0 && b % c == 0)
return c;
}
}
int main()
{
int x, y;
cin >> x >> y;
int res = gcd(x, y);
cout << res << endl;
return 0;
}
AcWing 811. 交换数值
#include <iostream>
using namespace std;
void swap(int &x, int &y)
{
int t;
t = x;
x = y;
y = t;
cout << x << " " << y << endl;
}
int main()
{
int x, y;
cin >> x >> y;
swap(x, y);
return 0;
}
AcWing 812. 打印数字
#include <iostream>
using namespace std;
const int N = 1010;
void print(int a[], int size)
{
for (int i = 0; i < size; i ++ )
cout << a[i] << " ";
}
int main()
{
int n, size;
cin >> n >> size;
int a[N];
for (int i = 0; i < n; i ++ )
cin >> a[i];
print(a, size);
return 0;
}
AcWing 813. 打印矩阵
#include <iostream>
using namespace std;
const int N = 101;
int mat[N][N];
void print2D(int a[][N], int row, int col)
{
for (int i = 0; i < row; i ++ )
{
for(int j = 0; j < col; j ++)
cout << mat[i][j] << " ";
cout << endl;
}
}
int main()
{
int row, col;
cin >> row >> col;
for (int i = 0; i < row; i ++ )
for(int j = 0; j < col; j ++)
cin >> mat[i][j];
print2D(mat, row, col);
return 0;
}
AcWing 819. 递归求阶乘
#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;
int mul = fact(n);
cout << mul << endl;
return 0;
}
AcWing 820. 递归求斐波那契数列
#include <iostream>
using namespace std;
int fun(int n)
{
if (n == 1 || n == 2) return 1;
return fun(n - 1) + fun(n - 2);
}
int main()
{
int n;
cin >> n;
int res = fun(n);
cout << res << endl;
return 0;
}
AcWing 810. 绝对值
#include <iostream>
using namespace std;
int abs(int x)
{
if (x < 0) x = -x;
return x;
}
int main()
{
int n;
cin >> n;
int c = abs(n);
cout << c << endl;
return 0;
}
AcWing 806. 两个数的和
#include <iostream>
using namespace std;
double Sum(double a, double b)
{
return a + b;
}
int main()
{
double a, b;
cin >> a >> b;
double sum;
sum = Sum(a, b);
printf("%.2f\n", sum);
return 0;
}
AcWing 807. 区间求和
#include <iostream>
using namespace std;
int sum(int l, int r)
{
int sum = 0;
for (int i = l; i <= r; i ++ )
{
sum += i;
}
return sum;
}
int main()
{
int l, r;
cin >> l >> r;
cout << sum(l, r) << endl;
return 0;
}
AcWing 809. 最小公倍数
#include <iostream>
using namespace std;
const int N = 101;
int a[N], b[N];
void copy(int a[], int b[], int size)
{
for (int i = 0; i < size; i ++ )
b[i] = a[i];
}
int main()
{
int n, m, size;
cin >> n >> m >> size;
for (int i = 0; i < n; i ++ )
cin >> a[i];
for (int i = 0; i < m; i ++ )
cin >> b[i];
copy(a, b, size);
for (int i = 0; i < m; i ++ )
cout << b[i] << " ";
return 0;
}
AcWing 814. 复制数组
#include <iostream>
using namespace std;
const int N = 101;
int a[N], b[N];
void copy(int a[], int b[], int size)
{
for (int i = 0; i < size; i ++ )
b[i] = a[i];
}
int main()
{
int n, m, size;
cin >> n >> m >> size;
for (int i = 0; i < n; i ++ )
cin >> a[i];
for (int i = 0; i < m; i ++ )
cin >> b[i];
copy(a, b, size);
for (int i = 0; i < m; i ++ )
cout << b[i] << " ";
return 0;
}
AcWing 815. 打印字符串
#include <iostream>
#include <string.h>
using namespace std;
const int N = 101;
void print(char str[])
{
for (int i = 0; i < strlen(str); i ++ )
cout << str[i];
}
int main()
{
char s[N];
fgets(s, N, stdin);
print(s);
return 0;
}
AcWing 816. 数组翻转
#include <iostream>
using namespace std;
void reverse(int a[], int size)
{
for (int i = 0, j = size - 1; i < j; i ++, j -- )
swap(a[i], a[j]);
}
int main()
{
int a[1000];
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;
}
AcWing 817. 数组去重
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int a[N];
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i ++ ) cin >> a[i];
sort(a, a + n);
int k = 1;
for (int i = 1; i < n; i ++ )
if (a[i] != a[k - 1])
a[k ++] = a[i];
cout << k << endl;
return 0;
}
AcWing 818. 数组排序
#include <iostream>
#include <cstring>
using namespace std;
const int N = 1001;
int a[N];
void sort(int a[], int l, int r)
{
for (int i = l; i <= r; i ++ )
{
for (int j = i + 1; j <= r; j ++ )
{
if (a[i] > a[j])
swap(a[i], a[j]);
}
}
}
int main()
{
int n, l, r;
cin >> n >> l >> r;
for (int i = 0; i < n; i ++)
cin >> a[i];
sort(a, l, r);
for (int i = 0; i < n; i ++)
cout << a[i] << " ";
return 0;
}
AcWing 821. 跳台阶
#include <iostream>
using namespace std;
int res;
void f(int k, int n)
{
if (k == n) res ++;
else if (k < n)
{
f(k + 1, n);
f(k + 2, n);
}
}
int main()
{
int n;
cin >> n;
f(0, n);
cout << res << endl;
return 0;
}
AcWing 822. 走方格
#include <iostream>
using namespace std;
int n, m;
int res = 0;
void f(int x, int y)
{
if (x == n && y == m) res ++;
else
{
if (x < n) f(x + 1, y);
if (y < m) f(x, y + 1);
}
}
int main()
{
cin >> n >> m;
f(0,0);
cout << res << endl;
return 0;
}
AcWing 823. 排列
#include <iostream>
using namespace std;
const int N = 10;
int n;
void dfs(int u, int nums[],bool st[])
{
if (u == n)
{
for (int i = 0; i < n; i ++ ) cout << nums[i] << " ";
cout << endl;
}
else
{
for (int i = 1; i <= n; i ++ )
if (!st[i])
{
st[i] = true;
nums[u] = i;
dfs(u + 1, nums, st);
st[i] = false; // 恢复现场
}
}
}
int main()
{
cin >> n;
int nums[N];
bool st[N] = {0};
dfs(0, nums, st);
return 0;
}
7 结构体、类、指针、引用
AcWing 21. 斐波那契数列
class Solution {
public:
int Fibonacci(int n) {
if (n == 0) return 0;
if (n == 1 || n == 2) return 1;
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
};
AcWing 16. 替换空格
class Solution {
public:
string replaceSpaces(string &str) {
string res;
for (auto& x: str)
if(x == ' ')
res += "%20";
else
res += x;
return res;
}
};
AcWing 84. 求1+2+…+n
class Solution {
public:
int getSum(int n) {
if (n == 0) return 0;
return n + getSum(n - 1);
}
};
AcWing 28. 在O(1)时间删除链表结点
class Solution {
public:
void deleteNode(ListNode* node) {
node->val = node->next->val;
ListNode* n = node->next;
node->next = n->next;
delete n;
}
};
AcWing 36. 合并两个排序的链表
class Solution {
public:
ListNode* merge(ListNode* l1, ListNode* l2) {
auto dummy = new ListNode(-1);
auto cur = dummy;
while (l1 && l2)
{
if (l1->val < l2->val)
{
cur->next = l1;
cur = l1;
l1 = l1->next;
}
else
{
cur->next = l2;
cur = l2;
l2 = l2->next;
}
}
if (l1) cur->next = l1;
else cur->next = l2;
return dummy->next;
}
};
AcWing 78. 左旋转字符串
class Solution {
public:
string leftRotateString(string str, int n) {
// string r;
// for (int i = n; i < str.size(); i ++ ) r += str[i];
// for (int i = 0; i < n; i ++ ) r += str[i];
// return r;
// 大佬思路
return str.substr(n) + str.substr(0, n);
}
};
AcWing 87. 把字符串转换成整数
class Solution {
public:
int strToInt(string str) {
int k = 0;
while (k < str.size() && str[k] == ' ') k ++;
long long res = 0;
int minus = 1;
if (k < str.size())
{
if (str[k] == '-') minus = -1, k ++;
else if (str[k] == '+') k ++;
}
while (k < str.size() && str[k] >= '0' && str[k] <= '9')
{
res = res * 10 + str[k] - '0';
if (res > 1e11) break; // 防止溢出
k ++;
}
res *= minus;
if (res > INT_MAX) res = INT_MAX;
if (res < INT_MIN) res = INT_MIN;
return res;
}
};
AcWing 35. 反转链表
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if (!head || !head->next) return head;
// 迭代做法
// auto a = head, b = head->next;
// while (b)
// {
// auto c = b->next;
// b->next = a;
// a = b, b = c;
// }
// head->next = nullptr;
// return a;
// 递归做法
auto tail = reverseList(head->next);
head->next->next = head;
head->next = nullptr;
return tail;
}
};
AcWing 66. 两个链表的第一个公共结点
class Solution {
public:
ListNode *findFirstCommonNode(ListNode *headA, ListNode *headB) {
auto q = headA, p = headB;
while (q != p)
{
if (q) q = q->next;
else q = headB;
if (p) p = p->next;
else p = headA;
}
return q;
}
};
AcWing 29. 删除链表中重复的节点
class Solution {
public:
ListNode* deleteDuplication(ListNode* head) {
auto dummy = new ListNode(-1);
dummy->next = head;
auto p = dummy;
while (p->next)
{
auto q = p->next;
while (q && p->next->val == q->val) q = q->next;
if (p->next->next == q) p = p->next;
else p->next = q;
}
return dummy->next;
}
};
8 STL容器、位运算与常用库函数
AcWing 67. 数字在排序数组中出现的次数
class Solution {
public:
int getNumberOfK(vector<int>& nums , int k) {
int ret = 0;
for (auto x:nums) if (x == k) ret ++;
return ret;
}
};
AcWing 68. 0到n-1中缺失的数字
class Solution {
public:
int getMissingNumber(vector<int>& nums) {
// 我的想法
// if (nums.empty()) return 0;
// if (nums[0]) return 0;
// for (int i = 0; i < nums.size(); i ++ )
// {
// if (nums[i] + 1 != nums[i + 1])
// return nums[i] + 1;
// }
// 二分法
if (nums.empty()) return 0;
int l = 0, r = nums.size() - 1;
while (l < r)
{
int mid = l + r >> 1;
if (nums[mid] != mid) r = mid;
else l = mid + 1;
}
if (nums[l] == l) l ++;
return l;
}
};
AcWing 32. 调整数组顺序使奇数位于偶数前面
class Solution {
public:
void reOrderArray(vector<int> &array) {
int p = 0, q = array.size() - 1;
while (p < q)
{
while (p < q && array[p] % 2 != 0) p ++;
while (p < q && array[q] % 2 == 0) q --;
if (p < q) swap(array[p], array[q]);
}
}
};
AcWing 17. 从尾到头打印链表
class Solution {
public:
vector<int> printListReversingly(ListNode* head) {
vector<int> res;
for (auto p = head; p; p = p->next) res.push_back(p->val);
reverse(res.begin(), res.end());
return res;
}
};
AcWing 20. 用两个栈实现队列
class MyQueue {
public:
/** Initialize your data structure here. */
stack<int> stk, cache;
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
stk.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
while(stk.size() > 1) cache.push(stk.top()), stk.pop();
int t = stk.top();
stk.pop();
while(cache.size()) stk.push(cache.top()), cache.pop();
return t;
}
/** Get the front element. */
int peek() {
while(stk.size() > 1) cache.push(stk.top()), stk.pop();
int t = stk.top();
while(cache.size()) stk.push(cache.top()), cache.pop();
return t;
}
/** Returns whether the queue is empty. */
bool empty() {
return stk.empty();
}
};
AcWing 53. 最小的k个数
class Solution {
public:
vector<int> getLeastNumbers_Solution(vector<int> input, int k) {
sort(input.begin(), input.end());
vector<int> res;
for (int i = 0; i < k; i ++ ) res.push_back(input[i]);
return res;
}
};
AcWing 75. 和为S的两个数字
class Solution {
public:
vector<int> findNumbersWithSum(vector<int>& nums, int target) {
unordered_set<int> S;
for (auto x: nums)
{
if (S.count(target - x)) return {x, target - x};
S.insert(x);
}
}
};
AcWing 51. 数字排列
class Solution {
public:
vector<vector<int>> permutation(vector<int>& nums) {
sort(nums.begin(), nums.end());
vector<vector<int>> res;
do res.push_back(nums); while (next_permutation(nums.begin(), nums.end()));
return res;
}
};
AcWing 26. 二进制中1的个数
class Solution {
public:
int NumberOf1(int n) {
int res = 0;
unsigned int un = n;
while (un) res += un & 1, un >>= 1;
return res;
}
};
AcWing 862. 三元组排序
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 10010;
struct Data
{
int x;
double y;
string z;
bool operator< (const Data &t)const
{
return x < t.x;
}
}a[N];
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i ++ ) cin >> a[i].x >> a[i].y >> a[i].z;
sort(a, a + n);
for (int i = 0; i < n; i ++ ) printf("%d %.2lf %s\n", a[i].x, a[i].y, a[i].z.c_str());
return 0;
}