【C++】C++知识点复习

牛客cpp:牛客网在线编程

2024年4月10日:BC1--->BC8

BC4:浮点数精度保留

问题:不加入fixed输入0.359813,最后得到0.36,并不是强制保留0.360。这种写法会保留小数点后三位精度,但是最后输出会省略掉最后的0不打印。

cpp 复制代码
#include <ios>
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    float a;
    cin >> a;
    cout << fixed <<setprecision(3);
    cout << a << endl;
}

解决:在设置精度前加入sdt::fixed固定精度。

std::fixed 用于指定浮点数的定点表示法,而 std::setprecision(3) 则设置小数位数为三位。

BC8:字符菱形(for嵌套循环)

之前理解的for循环嵌套,外层循环打印行数,内层循环打印列数有点小瑕疵。没打印空格之前,#是不能占据第一个位置的(并不是一个矩阵!)
内层循环打印列数这句话并不是很准确。进入行之后只对这一行关注即可。

cpp 复制代码
include <iostream>
using namespace std;

int main() 
{
    string str ="#";
    for (int i = 0; i < 5; i++) 
    {
        if(i<3)    // 上半部分
        {
            for(int j=0;j<2-i;j++)
            {
                cout << " ";
            }
            for(int k=0;k<2*i+1;k++)
            {
                cout << str;
            }
            cout << endl;
        }
        else //下半部分
        {
            for(int j=0;j<i-2;j++)
            {
                cout << " ";
            }
            for(int k=0;k<9-2*i;k++)
            {
                cout << str;
            }
            cout <<endl;
        }
    }
}

2024年4月10日:BC9--->BC

BC9:字符转ASCII码

强制类型转换

cpp 复制代码
int ascii = static_cast<int>(ch);

BC10:四舍五入

输入:14,99;输出:15

cpp 复制代码
double a;
int b = round(a);

BC12:加入间隔的输入和控制精度输出

问题1:输入信息中有分号和逗号的情况下cin中加入char ch来控制;

问题2:变量类型声明为double,最后控制精度输出无法做到四舍五入。是因为double类型和setprecision不匹配,换位float即可。

输入:17140216;80.845,90.55,100.00

输出:The each subject score of No. 17140216 is 80.85, 90.55, 100.00.

cpp 复制代码
#include <ios>
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    int id_number;
    float score1, score2, score3;
    char ch;
    cin >> id_number >> ch >> score1 >> ch >> score2 >> ch >> score3;
    cout << "The each subject score of No. " << id_number
         << " is " << fixed << setprecision(2) << score1 << ", " << score2 << ", " <<
         score3 << "." << endl;
}

BC13:字符串截断

这种题目最好使用字符串,方便截断处理。使用substr函数,参数为开始位置和截取长度。

输入:20130225 输出: year=2013 month=02 date=25

cpp 复制代码
#include <iostream>
using namespace std;

int main() {
    string date;
    cin >> date;
    cout << "year=" << date.substr(0, 4) << endl;
    cout << "month=" << date.substr(4, 2) << endl;
    cout << "date=" << date.substr(6, 2) << endl;
}

BC14:C语言风格的输入输出

在一行内输入:a=1,b=2。用cin有点难度,但是c语言风格的输入就方便很多。头文件不需要改。

cpp 复制代码
scanf("a=%d,b=%d", &a, &b);

BC15:大小写转换和读取键入的字符

getchar函数专门用于读取键盘键入的字符,还可以用于丢弃Enter键。

cpp 复制代码
#include <iostream>
using namespace std;

int main() {
    char ch;
    char a;
    while ((ch = getchar()) != EOF) {
        getchar();
        a=tolower(ch);
        cout << a << endl;
    }
    return 0;
}

BC19 对齐

使用iomanip库中的setw()函数,来固定对齐格式,setw() 设置的字段宽度只对下一个输出项起作用。

例如使用setw(8),该函数意味着控制下一个输出的字段宽度为 8 个字符,不足8个长度则前面用空格补充。

cpp 复制代码
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    cout << a << setw(8) << b << setw(8) << c << endl;
}
相关推荐
王老师青少年编程1 小时前
信奥赛C++提高组csp-s之搜索进阶(搜索剪枝案例实践1)
c++·csp·高频考点·信奥赛·提高组·搜索剪枝·小木棍
石山代码2 小时前
ArrayList / HashMap / ConcurrentHashMap
java·开发语言
程序大视界2 小时前
【Python系列课程】Python正则表达式(下):环视、命名分组与日志实战
开发语言·python·正则表达式
枫叶v.2 小时前
Agent 分层存储架构设计:从记忆方法到中间件选型
开发语言·python
sleven fung4 小时前
MinerU与BabelDOC与KTransformers与OpenAI API库
开发语言·python·ai·langchain
萤萤七悬4 小时前
【Python笔记】AI帮实现CLI工具-使用argparse.ArgumentParser接收命令参数
开发语言·笔记·python
iCxhust4 小时前
C# 命令行指令 查看二进制文件
开发语言·单片机·嵌入式硬件·c#·proteus·微机原理·8088单板机
csdn_aspnet4 小时前
Java 霍尔分区算法(Hoare‘s Partition Algorithm)
java·开发语言·算法
王老师青少年编程4 小时前
信奥赛C++提高组csp-s之搜索进阶(搜索剪枝核心思想 )
c++·dfs·csp·信奥赛·搜索剪枝·搜索优化
一拳一个呆瓜4 小时前
【STL】使用 C++ 标准库标头
c++·stl