【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;
}
相关推荐
郝学胜-神的一滴8 分钟前
计算机图形学中的光照模型:从基础到现代技术
开发语言·c++·程序人生·图形渲染
lly20240633 分钟前
PostgreSQL 表达式
开发语言
LXMXHJ35 分钟前
php开发
开发语言·php
mjhcsp1 小时前
MATLAB 疑难问题诊疗:从常见报错到深度优化的全流程指南
开发语言·matlab
Lynnxiaowen1 小时前
今天我们开始学习python语句和模块
linux·运维·开发语言·python·学习
逐步前行2 小时前
C标准库--浮点<float.h>
c语言·开发语言
深耕AI2 小时前
MFC + OpenCV 图像预览显示不全中断问题解决:GDI行填充详解
c++·opencv·mfc
zoyation2 小时前
多线程简介和在JAVA中应用
java·开发语言
余辉zmh2 小时前
【C++篇】:ServiceBus RPC 分布式服务总线框架项目
开发语言·c++·rpc
水饺编程2 小时前
第3章,[标签 Win32] :窗口类03,窗口过程函数字段
c语言·c++·windows·visual studio