语法learning笔记

写在前面:

这些都是我听完AcWing在线活动视频讲解后整理的,目前已经更新完
为方便大家收藏,现在把笔记链接汇总在这
如果大家发现问题或有更好的建议,欢迎在下方留言或私信,谢谢~

语法内容包含
【1】变量、输入输出、表达式与顺序语句
【2】判断语句
【3】循环语句
【4】数组
【5】字符串
【6】函数
【7】结构体、类、指针、引用
【8】STL容器、位运算与常用库函数等

文章目录

1. 变量、输入输出、表达式与顺序语句

AcWing 1. A + B

思路
cin自动过滤掉空格
链接
https://www.acwing.com/problem/content/1/

#include <iostream>

using namespace std;

int main()
{
    int a, b;
    cin >> a >> b;
    cout << a + b;
    return 0;
}

AcWing 608. 差

思路
scanf也能自动过滤掉空格与换行
链接
https://www.acwing.com/problem/content/610/

#include <cstdio>

using namespace std;//如果只用到scanf与printf函数,而没用用到其他函数,可以不用加

int main()
{
    int a, b, c, d;
    scanf("%d%d%d%d",&a,&b,&c,&d);//只要是空格,换行的意思就是有好多个空格,无所谓都能写进去,scanf能把空格与换行过滤掉
    printf("DIFERENCA = %d\n",a*b-c*d);//回车
    
    //评测器会自动过滤掉最后一个回车和每一行结尾的空格
    //展示错误的空格不会忽略
    
    return 0;
    
}

AcWing 604. 圆的面积

思路
当保留几位有效数字时,用scanf与printf
链接
https://www.acwing.com/problem/content/606/

#include <cstdio>

using namespace std;


//float, double   在代码中选double
//float 6-7位
//double 15-16位

int main()
{
    double pi = 3.14159, r;
    scanf("%lf",&r);
    printf("A=%.4lf\n",pi*r*r);//保留4位有效数字
    
    return 0;

}

AcWing 606. 平均数1

思路
题干中要求保留几位小数,统统用printf 与scanf
链接
https://www.acwing.com/problem/content/608/

#include <cstdio> //题干中要求保留几位小数,统统用printf 与scanf

int main()
{
    double a,b;
    scanf("%lf%lf",&a,&b);
    printf("MEDIA = %.5lf\n",(a*3.5+b*7.5)/11);//换行
    
    return 0;
}

AcWing 609. 工资

链接
https://www.acwing.com/problem/content/611/

#include <cstdio>

int main()
{
    int a, b;
    double c;
    scanf("%d%d%lf", &a, &b, &c);
    printf("NUMBER = %d\nSALARY = U$ %.2lf", a, b * c);
    
    return 0;
}

AcWing 615. 油耗

思路
小学解方程的问题

另外需要注意:注意
链接
https://www.acwing.com/problem/content/617/


#include <cstdio>

int main()
{
    int a;//也可以用double定义int
    double b;
    scanf("%d%lf", &a, &b);
    printf("%.3lf km/l",a / b);
    
    return 0;
}

AcWing 616. 两点间的距离

思路
两点距离公式
调用cmath库中的sqrt函数开根号
链接
https://www.acwing.com/problem/content/618/

#include <cstdio>
#include <cmath>


int main()
{
    double a, b, c, d;
    scanf("%lf%lf%lf%lf", &a, &b, &c, &d);
    printf("%.4lf\n", sqrt((a - c) * (a - c) + (b - d) * (b - d)));
    
    return 0;
}

AcWing 653. 钞票

思路
求所用的钞票数量尽可能少,面值从大往小循环
链接
https://www.acwing.com/problem/content/655/

#include <cstdio>

int main()
{
    int n;
    scanf("%d", &n);
    printf("%d\n", n);
    
    int a[7] = {100, 50, 20, 10, 5, 2, 1};
    
    for (int i = 0; i < 7; i ++ )
    {
        printf("%d nota(s) de R$ %d,00\n",n / a[i], a[i]);
        n = n % a[i];
    }
    
    return 0;
    
}

AcWing 654. 时间转换

思路
同上一题AcWing 653. 钞票
链接
https://www.acwing.com/problem/content/656

#include <cstdio>

int main()
{
    int n;
    scanf("%d", &n);
    
    int a[3] ={60*60, 60, 1};
    
    for (int i = 0; i < 3; i ++)
    {
        printf("%d",n / a[i]);
        if(i !=2) printf(":");
        
        n = n % a[i];
    }
    
    return 0;
}

2. 判断语句

AcWing 665. 倍数

思路
条件判断
链接
https://www.acwing.com/problem/content/667/

#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. 零食

思路
小学计算题
链接
https://www.acwing.com/problem/content/662/

//法1
#include <cstdio>

int main()
{
    int x, y;
    scanf("%d%d", &x, &y);
    
    double price[5] = {4.00, 4.50, 5.00, 2.00, 1.50};
    
    printf("Total: R$ %.2lf",price[x - 1] * y);
    
    return 0;
}
//法2
#include <cstdio>

int main()
{
    int x, y;
    scanf("%d%d", &x, &y);

    double price;
    if (x == 1) price = 4;
    else if (x == 2) price = 4.5;
    else if (x == 3) price = 5;
    else if (x == 4) price = 2;
    else price = 1.5;

    printf("Total: R$ %.2lf\n", price * y);

    return 0;
}

AcWing 659. 区间

思路
选择判断
链接
https://www.acwing.com/problem/content/661/

#include  <iostream>

using namespace std;

int main()
{
    double x;
    cin >> x;

    if (x >= 0 && x <= 25) cout << "Intervalo [0,25]" << endl;
    else if (x > 25 && x <= 50) cout << "Intervalo (25,50]" << endl;
    else if (x > 50 && x <= 75) cout << "Intervalo (50,75]" << endl;
    else if (x > 75 && x <= 100) cout << "Intervalo (75,100]" << endl;
    else cout << "Fora de intervalo" << endl;

    return 0;
}

AcWing 664. 三角形

思路
小学计算问题
链接
https://www.acwing.com/problem/content/666/

#include <cstdio>

int main()
{
    double a, b, c;
    scanf("%lf%lf%lf", &a, &b, &c);

    if (a + b > c && a + c > b && b + c > a) printf("Perimetro = %.1lf\n", a + b + c);
    else printf("Area = %.1lf\n", (a + b) * c / 2);

    return 0;
}

AcWing 667. 游戏时间

思路
学会将现实问题通过代码表示
链接
https://www.acwing.com/problem/content/669/

#include <cstdio>

int main()
{
    int a, b;
    scanf("%d%d", &a, &b);

    int res;
    if (a < b) res = b - a;
    else  res = b - a + 24;

    printf("O JOGO DUROU %d HORA(S)\n", res);

    return 0;
}

AcWing 669. 加薪

思路
小学计算问题
链接
https://www.acwing.com/problem/content/671/

#include <cstdio>

int main()
{
    double salary;
    scanf("%lf%lf", &salary);
    
    double No_sa,Re_gan;
    int Em_per;
    
    if(salary >= 0 && salary <= 400)
    {
        No_sa = salary *(1 + 0.15);
        Re_gan = salary * 0.15; 
        Em_per = 15;
    }
    else if(salary > 400 && salary <= 800)
    {
        No_sa = salary *(1 + 0.12);
        Re_gan = salary * 0.12; 
        Em_per = 12;
    }
    else if(salary > 800 && salary <= 1200)
    {
        No_sa = salary *(1 + 0.10);
        Re_gan = salary * 0.10; 
        Em_per = 10;
    }    
    else if(salary > 1200 && salary <= 2000)
    {
        No_sa = salary *(1 + 0.07);
        Re_gan = salary * 0.07; 
        Em_per = 7;
    }    
    else
    {
        No_sa = salary *(1 + 0.04);
        Re_gan = salary * 0.04; 
        Em_per = 4;
    }
    printf("Novo salario: %.2lf\n",No_sa);
    printf("Reajuste ganho: %.2lf\n",Re_gan);
    printf("Em percentual: %d \%\n",Em_per);
    
    return 0;

}

AcWing 670. 动物

思路
层级判断
链接
https://www.acwing.com/problem/content/672/

#include <iostream>
#include <cstdio>
#include <string>

using namespace std;

int main()
{
    string name1, name2, name3;
    cin >> name1 >> name2 >> name3;
    if(name1 == "vertebrado")
    {
        if(name2 == "ave")
        {
            if(name3 == "carnivoro") printf("aguia\n");
            else printf("pomba\n");
        }
        else
        {
            if(name3 == "onivoro") printf("homem\n");
            else printf("vaca\n");
        }
    }
    else
    {   
        if(name2 == "inseto")
        {
            if(name3 == "hematofago") printf("pulga\n");
            else printf("lagarta\n");
        }
        else
        {
            if(name3 == "hematofago") printf("sanguessuga\n");
            else printf("minhoca\n");  
        }
        
    }
    
    return 0;
}

3. 循环语句

AcWing 708. 偶数

思路
循环1-100,2的倍数就输出结果
链接
https://www.acwing.com/problem/content/710/

#include <iostream>

using namespace std;

int main()
{
    for(int i = 2; i <= 100; i += 2)
    {
        cout << i << endl ;
    }
    return 0;
}

AcWing 709. 奇数

思路
同上
链接
https://www.acwing.com/problem/content/711/

#include <iostream>

using namespace std;

int main()
{
    int x;
    cin >> x;
    for(int i = 1; i <= x; i += 2) cout << i << endl;
    
    return 0;
}

AcWing 712. 正数

思路
循环语句与条件语句结合,每输入一个数值就进行判断
链接
https://www.acwing.com/problem/content/714/

#include <iostream>

using namespace std;

int main()
{
    int cnt = 0 ; 
    double x ;
    for (int i = 0; i < 6; i ++ )
    {
        double x ;
        cin >> x;
        if(x > 0 ) cnt ++ ;
    }
    cout << cnt << " positive numbers";
    return 0;
}

AcWing 714. 连续奇数的和 1

思路
起始i取两个数的最小值,swap函数减少代码量
链接
https://www.acwing.com/problem/content/716/

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int a, b;
    cin >> a >> b;
    int sum = 0;
    if(a < b) swap(a, b);

    for(int i = b + 1; i < a; i ++)
    {
        if(i % 2 != 0) sum += i;
    }

    cout << sum << endl;
    
    return 0;
}

AcWing 716. 最大数和它的位置

思路
每次循环,如果新值大于原来最大值,就更新最大值
链接
https://www.acwing.com/problem/content/718/

#include <iostream>

using namespace std;

int main()
{
    int max = 0;
    int pos = 0;
    for(int i = 0; i < 100; i ++)
    {
        int x;
        cin >> x;
        if(x > max) 
        {
            max = x;
            pos = i + 1;
        }
    }
    cout << max << endl << pos << endl;
    
    return 0;
}

AcWing 721. 递增序列

思路
不符合循环就break
链接
https://www.acwing.com/problem/content/description/723/
版本1

#include <iostream>

using namespace std;

int main()
{
    for(int i = 0; ; i ++ )//死循环,等价于while(true)
    {
        int x;
        cin >> x;
        if(x != 0)
        {
            for(int j = 0; j < x; j ++ ) cout << j + 1 << " ";
            cout << endl;
        }
        else 
        {
            break;
        }
    }
    
    return 0;
    
}

版本2

#include <iostream>

using namespace std;

int main()
{
    int x;
    
    while(cin >> x && x) //当遇到EOF时,cin返回0,0 && 0  = false
                         //另外,while(cin >> n, n)逗号语法也可以,取逗号后面的值
    {
        for (int i = 1; i <= x; i ++ )
        {
            cout << i << ' ';
        }
        cout << endl;
    }
        
    return 0;
}

AcWing 720. 连续整数相加

思路
直到读入一个正数为止
链接
https://www.acwing.com/problem/content/722/
版本1

#include <iostream>

using namespace std;

int main()
{
    int a;
    cin >> a;
    int sum = 0;
    while(true)
    {
        int n;
        cin >> n;
        if(n <= 0) continue;
        for(int i = a; i < a + n; i ++)
        {
            sum += i;
        }
        break;
    }
    cout << sum << endl;
    
    return 0;
}

版本2

#include <iostream>

using namespace std;

int main()
{
    int a, n;
    cin >> a;
    while(cin >> n, n <= 0);
    
    int s = 0;
    for(int i = 0; i < n; i ++) s += a + i;
    
    cout << s << endl;
    
    return 0;
}

AcWing 724. 约数

思路
枚举一遍,能够整除的数就是约数
链接
https://www.acwing.com/problem/content/description/726/

#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

思路
每行的最后一个替换成“PUM”
链接
https://www.acwing.com/problem/content/description/725/
版本1

#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    int M, N;
    cin >> M >> N;
    
    for(int i = 0; i < M; i ++)
    {
        for(int j = 1; j <= N; j ++)
        {
            if(j % N == 0) cout << "PUM";
            else cout << N * i + j << " ";
        }
        cout << endl;
    }
    return 0;
}

版本2

#include <iostream>

using namespace std;

int main()
{
    int n, m;
    cin >> n >> m;
    
    for(int i = 0, k = 1; i < n; i ++)
    {
        for(int j = 0; j < m - 1; j ++)
        {
            cout << k << " ";
            k ++;
        }
        
        cout << "PUM" << endl;
        k ++;
    }
    
    return 0;
}

4. 数组

AcWing 737. 数组替换

思路
判断是否为非正整数,如果是则替换成1
链接
https://www.acwing.com/problem/content/739/

#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;
        cout << "X[" << i << "] = " << x[i] << endl; 
    }

    return 0;
}

AcWing 738. 数组填充

思路
每次循环,将上一个元素取2倍得到下一个的元素
链接
https://www.acwing.com/problem/content/740/

#include <iostream>

using namespace std;

int main()
{
    int v;
    cin >> v;
    
    for(int i = 0; i < 10; i ++)
    {
        cout << "N["<< i <<"] = " << v << endl;
        v = 2 * v;
    }
}

AcWing 739. 数组选择

思路
循环读入数组,循环读出数组
链接
https://www.acwing.com/problem/content/description/741/

#include <cstdio>

int main()
{
    double a[100];
    for(int i = 0; i < 100; i ++) scanf("%lf", &a[i]);
    
    for(int i = 0; i < 100; i ++)
        if(a[i] <= 10)
            printf("A[%d] = %.1lf\n", i, a[i]);
            
    return 0;
}

AcWing 743. 数组中的行

思路
scanf不会自动忽略空格与换行
链接
https://www.acwing.com/problem/content/description/745/

#include <cstdio>

int main()
{
    double a[12][12];
    
    int l;
    char t;
    scanf("%d\n%c",&l, &t);
    
    for(int i = 0; i < 12; i ++)
        for(int j = 0; j < 12; j ++)
            scanf("%lf", &a[i][j]);
            
    double s = 0;
    
    for(int i = 0; i < 12; i ++) s +=a[l][i];
    
    if(t == 'S') printf("%.1lf\n", s);
    else printf("%.1lf\n", s / 12);
    
    return 0;
}

AcWing 745. 数组的右上半部分

思路
j在i的基础上加1
链接
https://www.acwing.com/problem/content/description/747/

#include <cstdio>

int main()
{
    char t;
    scanf("%c", &t);
    double a[12][12];
    
    for(int i = 0; i < 12; i ++)
        for(int j = 0; j <12; j ++)
            scanf("%lf", &a[i][j]);
            
    int c = 0;
    double s = 0;
    
    for(int i = 0; i <12; i ++)
        for(int j = i + 1;j < 12; j ++)
        {
            c ++;
            s +=a[i][j];
        }
    if(t == 'S') printf("%.1lf\n", s);
    else printf("%.1lf\n", s / c);
    
    return 0;
}

AcWing 747. 数组的左上半部分

思路
寻找i 与 j 的潜在关系
链接
https://www.acwing.com/problem/content/description/749/

#include <cstdio>

int main()
{
    double a[12][12];
    char t;
    
    scanf("%c", &t);
    for(int i = 0; i < 12; i ++)
        for(int j = 0; j < 12; j ++)
            scanf("%lf", &a[i][j]);
            
    int c = 0;
    double s = 0;
    for(int i = 0; i < 12; i ++)
        for(int j = 0; j <= 10 - i; j ++)
        {
            c ++;
            s += a[i][j];
        }
    if(t == 'S') printf("%.1lf\n", s);
    else printf("%.1lf\n", s / c);
    
    return 0;
}

AcWing 749. 数组的上方区域

思路
同上
链接
https://www.acwing.com/problem/content/description/751/

#include <cstdio>

int main()
{
    char t;
    double a[12][12];
    
    scanf("%c", &t);
    for(int i = 0; i < 12; i ++)
        for (int j = 0; j < 12; j ++)
            scanf("%lf", &a[i][j]);
            
    double s = 0, c = 0;
    for(int i = 0; i <5; i ++)
        for(int j = i + 1; j <= 10 - i; j ++)
        {
            c += 1;
            s += a[i][j];
        }
    if(t == 'S') printf("%.1lf\n", s);
    else printf("%.1lf\n", s / c);
    
    return 0;
}

AcWing 753. 平方矩阵 I

思路
取到上下左右最小的值
链接
https://www.acwing.com/problem/content/755/

#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;
}

5. 字符串

AcWing 760. 字符串长度

思路
fgets函数会把回车也读进来
链接
https://www.acwing.com/problem/content/762/

#include <cstdio>

int main()
{
    char str[101];
    
    fgets(str, 101, stdin);
    
    int len = 0;
    for(int i = 0; str[i] && str[i] !='\n'; i ++) len ++;
    
    printf("%d\n", len);
    
    return 0;
}

AcWing 761. 字符串中的数字个数

思路
str[i] >= ‘0’ && str[i] <= ‘9’ 或者用ASCII码表示
链接
https://www.acwing.com/problem/content/763/

#include <cstdio>

int main()
{
    char str[101];

    fgets(str, 101, stdin);

    int cnt = 0;
    for (int i = 0; str[i]; i ++ )
        if (str[i] >= '0' && str[i] <= '9')
            cnt ++ ;
    printf("%d\n", cnt);

    return 0;
}

AcWing 763. 循环相克令

思路
将所有字符串用数字替代
链接
https://www.acwing.com/problem/content/description/765/

#include <cstdio>
#include <iostream>

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. 字符串加空格

思路
string支持动态加字符,新建字符串加一个字符放一个空格
链接
https://www.acwing.com/problem/content/767/

#include <iostream>

using namespace std;

int main()
{
    string a;
    getline(cin, a);

    string b;
    for (auto c : a) b = b + c + ' ';

    b.pop_back();  // 把最后一个字符删掉

    cout << b << endl;

    return 0;
}

AcWing 769. 替换字符

思路
puts函数在cstdio库中
链接
https://www.acwing.com/problem/content/771/

#include <cstdio>
#include <iostream>

using namespace std;

int main()
{
    char str[31];
    scanf("%s", str);
    
    char c;
    scanf("\n%c", &c);
    for(int i = 0; str[i]; i ++)
    {
        if(str[i] == c)
            str[i] = '#';
    }
    
    puts(str);
    
    return 0;
}

AcWing 773. 字符串插入

思路
利用substr函数 :第一个参数为子字符串开始字符,第二个参数为所选子字符串长度
链接
https://www.acwing.com/activity/content/problem/content/1955/

#include <iostream>
using namespace std;

int main()
{
    string a, b;
    
    while(cin >> a >> b)
    {
        int p = 0;
        for(int i = 1; i < a.size(); i ++)
        {
            if(a[i] > a[p])
                p = i;
        }
        
        cout << a.substr(0, p + 1) + b + a.substr(p + 1) <<endl;
    }
    
    return 0;
}

AcWing 772. 只出现一次的字符

思路
创建一个新数组记录字符出现的次数
链接
https://www.acwing.com/problem/content/description/774/

#include <iostream>
#include <cstring>

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, len = strlen(str); i < len; i ++) cnt[str[i] - 'a'] ++;
    
    
    
    for(int i = 0; str[i]; i ++)
    {
        if(cnt[str[i] - 'a'] == 1)
        {
            cout << str[i] <<endl;
            return 0;
        }
    }
    
    puts("no");
    
    return 0;
}

AcWing 766. 去掉多余的空格

思路
字符串去掉多余的空格
法1:利用cin读取的特性,每读入一个字符串,输入一个空格(另外结尾多了空格是不影响答案的)
法2:通过getline函数将字符串全部读入,重新遍历,借助双指针手动去掉多余的空格
法3:通过getline函数将字符串全部读入,重新遍历,每次只判断相邻两个字符且只加入第一个空格,手动去掉多余的空格
链接
https://www.acwing.com/problem/content/description/768/

//法1
#include <iostream>

using namespace std;

int main()
{
	string s;
	while (cin >> s) cout << s << ' ';
	
	return 0;
}
//法2
#include <iostream>

using namespace std;

int main()
{
	string s;
	getline(cin, s);
	string r;
	for (int i = 0; i < s.size(); i ++ )
	{
		if (s[i] != ' ') r += s[i];
		else
		{
			r += ' ';
			int j = i;
			while (j < s.size() && s[j] == ' ') j ++;//双指针算法
			i = j - 1;//在循环体,执行完后i还要加1	
		}	
	}
	cout << r << endl;

	return 0;
}

//法3
#include <iostream>

using namespace std;

int main()
{
	string s;
	getline(cin, s);
	string r;
	for (int i = 0; i < s.size(); i ++ )
	{
		if (s[i] != ' ') r += s[i];
		else
		{
			if (!i || s[i - 1] != ' ') r += ' ';//要么是第一个字符,要么是前一个字符不是空格,如果前一个字符是空格就不需要在加空格了
		}
	}
	cout << r << endl;

	return 0;
}

6. 函数

AcWing 804. n的阶乘

思路
5!= 1 * 2 * 3 *4 * 5
链接
https://www.acwing.com/problem/content/description/806/

#include <iostream>

using namespace std;

int fact(int n)
{
    int res = 1;
    for(int i = 1; i <= n; i ++)
    {
        res *= i;
    }
    return res;
}

int main()
{
    int n;
    cin >> n;
    
    cout << fact(n) << endl;
    
    return 0;
}

AcWing 805. x和y的最大值

思路
谁大返回谁
链接
https://www.acwing.com/problem/content/807/

#include <iostream>

using namespace std;

int max(int x, int y)
{
    if(x > y) return x;
    return y;
}

int main()
{
    int x, y;
    cin >> x >> y;
    
    cout << max(x, y) << endl;
    
    return 0;
}

AcWing 808. 最大公约数

思路
两个数的最大公约数:能被两个数整除
链接
https://www.acwing.com/problem/content/description/810/

#include <iostream>

using namespace std;

int gcd(int a, int b)
{
    for(int i = 1000; i; i --)
    {
        if(a % i == 0 && b % i == 0)
            return i;
    }
    return -1;
}

int main()
{
    int a, b;
    cin >> a >> b;
    cout << gcd(a, b) << endl;
    
    return 0;
}

AcWing 811. 交换数值

思路
传入的函数要交换,本身也要交换,要传入实参
链接
https://www.acwing.com/problem/content/813/

#include <iostream>

using namespace std;

void swap(int& x, int& y)
{
    if(x == y) return;
    
    int t = x;
    x = y;
    y = t;
}

int main()
{
    int x, y;
    cin >> x >> y;
    swap(x, y);
    
    cout << x << ' ' << y << endl;
    
    return 0;
}

AcWing 812. 打印数字

思路
size不能在全局定义,可能与其他库函数相冲突
链接
https://www.acwing.com/problem/content/814/

#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] << ' ';
    }
    cout << endl;
}

int main()
{
    int n, size;
    int a[N];
    
    cin >> n >> size;
    for(int i = 0; i < n; i ++) cin >> a[i];
    
    print(a, size);
    
    return 0;
}

AcWing 813. 打印矩阵

思路
二维矩阵如何输出
链接
https://www.acwing.com/problem/content/815/

#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 a[100][100];
    
    int row, col;
    cin >> row >> col;
    for(int i = 0; i < row; i ++)
    {
        for(int j = 0; j < col; j ++)
        {
            cin >> a[i][j];
        }
    }
    
    print2D(a, row, col);
    
    return 0;
}

AcWing 819. 递归求阶乘

思路
找递归出口
链接
https://www.acwing.com/problem/content/description/821/

#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;
}

AcWing 820. 递归求斐波那契数列

思路
递归会反复调用函数,所有参数n不会太大。但用递归写要比用迭代更方便理解
链接
https://www.acwing.com/problem/content/description/822/

#include <iostream>

using namespace std;

int f(int n)
{
    if(n <= 2) return 1;
    return f(n - 2) + f(n -1);
}

int main()
{
    int n;
    cin >> n;
    
    cout << f(n) << endl;
    
    return 0;
}

7. 结构体、类、指针、引用

AcWing 21. 斐波那契数列

思路
学会在类中写代码
链接
https://www.acwing.com/problem/content/19/

class Solution {
public:
    int Fibonacci(int n) {
        if(n <= 1) return n;
        return Fibonacci(n - 1) + Fibonacci(n - 2);
        
    }
};

AcWing 16. 替换空格

思路
遍历一遍字符串
链接
https://www.acwing.com/problem/content/17/

class Solution {
public:
    string replaceSpaces(string &str) {
        string res;
        for(auto c : str)
        {
            if(c == ' ') res += "%20";
            else res += c;
        }
        
        return res;
        
    }
};

AcWing 84. 求1+2+…+n

思路
利用&&(逻辑与)短路运算
指如果在进行前面的表达式的运算过程,通过判断已经明确的知道整个表达式的结果,那么就不会进行后面表达式的运算判断。
链接
https://www.acwing.com/problem/content/80/

class Solution {
public:
    int getSum(int n) {
        int res = n;
        n > 0 && (res += getSum(n - 1));
        
        return res;
        
    }
};

AcWing 28. 在O(1)时间删除链表结点

思路
伪删除
链接
https://www.acwing.com/problem/content/85/

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    void deleteNode(ListNode* node) {
        node -> val = node -> next -> val;
        node -> next = node ->next -> next;
        
        //*(node) = *(node -> next);
        
    }
};

AcWing 36. 合并两个排序的链表

思路
类似归并
链接
https://www.acwing.com/problem/content/34/

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* merge(ListNode* l1, ListNode* l2) {
        auto dummy = new ListNode(-1), tail = dummy;
        while(l1 && l2)
        {
            if(l1 -> val < l2 -> val)
            {
                tail = tail -> next = l1;
                l1 = l1 -> next;
            }
            else
            {
                tail = tail -> next = l2;
                l2 = l2 ->next;
            }
        }
        if(l1) tail -> next = l1;
        if(l2) tail -> next = l2;
        
        return dummy -> next;
        
    }
};

8. STL容器、位运算与常用库函数

AcWing 67. 数字在排序数组中出现的次数

思路
遍历一遍数组
链接
https://www.acwing.com/problem/content/63/

class Solution {
public:
    int getNumberOfK(vector<int>& nums , int k) {
        int cnt = 0;
        for(auto x : nums)
        {
            if(x == k) cnt ++;
        }
        
        return cnt;
        
    }
};
class Solution {
public:
    int getNumberOfK(vector<int>& nums , int k) {
        int cnt = 0;
        for(int i = 0; i < nums.size(); i ++)
        {
            if(nums[i] == k) cnt ++;
        }
        
        return cnt;
        
    }
};

AcWing 68. 0到n-1中缺失的数字

思路
类似归并
链接
https://www.acwing.com/problem/content/64/

class Solution {
public:
    int getMissingNumber(vector<int>& nums) {
        unordered_set<int> S;
        for(int i = 0; i <= nums.size(); i++) S.insert(i);
        
        for(auto x : nums) S.erase(x);
        
        return *S.begin();
        
    }
};

AcWing 32. 调整数组顺序使奇数位于偶数前面

思路
利用双指针算法
链接
https://www.acwing.com/problem/content/30/

class Solution {
public:
    void reOrderArray(vector<int> &array) {
        int i = 0, j = array.size() - 1;
        while(i < j)
        {
            while(i < j && array[i] % 2) i ++;
            while(i < j && array[j] % 2 == 0) j --;
            if(i < j) swap(array[i], array[j]);
        }
         
    }
};

AcWing 17. 从尾到头打印链表

思路
利用reverse翻转函数
链接
https://www.acwing.com/problem/content/18/

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
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. 用两个栈实现队列

思路
满足栈先进后出,队列先进先出
链接
https://www.acwing.com/problem/content/36/

class MyQueue {
public:
    /** Initialize your data structure here. */
    stack<int> s1, s2;
    
    MyQueue() {
        
    }
    
    /** Push element x to the back of queue. */
    void push(int x) {
        s1.push(x);
        
    }
    
    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        while (s1.size() > 1) s2.push(s1.top()), s1.pop();
        int t = s1.top();
        s1.pop();
        while(s2.size()) s1.push(s2.top()), s2.pop();
        return t;
        
    }
    
    /** Get the front element. */
    int peek() {
        while (s1.size() > 1) s2.push(s1.top()), s1.pop();
        int t = s1.top();
        while(s2.size()) s1.push(s2.top()), s2.pop();
        return t;
        
    }
    
    /** Returns whether the queue is empty. */
    bool empty() {
        return s1.empty();
        
    }
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * bool param_4 = obj.empty();
 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nighty_k

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值