【AcWing】语法基础课听课笔记

第一讲变量,输入输出,表达式与顺序语句

AcWing606.平均数

#include<cstdio>

using namespace std;

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

单精度(float)在计算机中存储占用4字节,32位,有效位数为7位(6位小数+小数点)。
双精度(double)在计算机中存储占用8字节,64位,有效位数为16位(15位小数+小数点)。

在这里插入图片描述
在这里插入图片描述
AcWing653.钞票

#include <iostream>

using namespace std;

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

c++ 控制输出格式的方法

    cout.setf(ios_base::fixed); // 若不加这一句,则输出为科学计数法表示的数
    cout.precision(2);

AcWing 656.钞票和硬币

// 放大100倍,避免小数的问题。
#include <iostream>
#include <cstdio>

using namespace std;

int a[12] = {
   10000, 5000, 2000, 1000, 500, 200, 100, 50, 25, 10, 5, 1};
int ans[12];

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

    int m = (int)(n * 100);
    
    for (int i = 0; i < 12; i++)
    {
   
        ans[i] = m / a[i];
        m %= a[i];
    }

    puts("NOTAS:");
    for (int i = 0; i < 6; i++)
        printf("%d nota(s) de R$ %.2lf\n", ans[i], (double)a[i] / 100);
    puts("MOEDAS:");
    for (int i = 6; i < 12; i++)
        printf("%d moeda(s) de R$ %.2lf\n", ans[i], (double)a[i] / 100);

    return 0;
}

第二讲 判断语句

AcWing660.零食

#include <cstdio>

double price[6] = {
   0, 4, 4.5, 5, 2, 1.5};
int x, y;

int main() {
   
    scanf("%d %d", &x, &y);
    printf("Total: R$ %.2f", price[x] * y);
    return 0;
}

第五讲 字符串

AcWing760.字符串长度

#include <iostream>
#include <cstring>

using namespace std;

int main()
{
   
    string a;
    getline(cin,a);
    cout<<a.size()<<endl;
    return 0;
}

AcWing761.字符串中的数字个数

#include <iostream>

using namespace std;

string s;
int cnt;

int main()
{
   
    getline(cin,s);
    for (int i = 0; i < s.size(); i ++)
        if(s[i]>='0'&&s[i]<='9') cnt++;
    cout << cnt;
}

AcWing763.循环相克令

#include<iostream>
using namespace std;
int get(string s)
{
   
    if(s == "Hunter") return 0;
    if(s == "Bear") return 1;
    return 2;
}
int main()
{
   
    int n;
    cin >> n;
    while(n --)
    {
   
        string a,b;
        cin >> a >> b;
        int x = get(a), y = get(b);
        if(x == y) cout << "Tie" <<endl;
        else if((x + 1) % 3 == y) cout << "Player2"<< endl;
        else cout << "Player1" << endl;
    }
}

AcWing765.字符串加空格

#include<iostream>

using namespace std;

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

    string b;
    for (auto c : a) b = b + c + ' '; // for (auto c : a) b += c + ' ';如果这样写的话输出会乱码。

    b.pop_back();
    
    cout << b << endl;

    return 0;
}

AcWing769.替换字符

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
   
    string a;
    char s;
    getline(cin,a);
    cin>>s;
    for(int i = 0;i < a.size();i ++)
    {
   
        if(a[i] == s) cout << "#";
        else cout<<a[i];
    }
    return 0;
}

AcWing773.字符串插入

#include<iostream>

using namespace std;

int 
  • 4
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值