C++练习备忘录

news2024/9/21 20:36:54

1. 保留两位小数输出格式

在这里插入图片描述

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	double S = 0;
	S = (15 + 25) * 20 / 2;
	cout << fixed << setprecision(2) << S;
	return 0;
}

2. 设置输出宽度

在这里插入图片描述

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	int a, b, c;
	cin >> a >> b >> c;
	cout << setw(8) << a << " ";
	cout << setw(8) << b << " ";
	cout << setw(8) << c;
	return 0;
}

3. ASCII码转换

在这里插入图片描述

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	char a;
	cin >> a;
	cout << int(a);
	return 0;
}

4. 高精度加法

在这里插入图片描述

#include <iostream>
using namespace std;

// 字符型转整型
void strtoint(string src, int des[]) {
	for (int i = 0;i < src.size();i++) {
		//从[1]开始倒序存整型数字,使得个位对齐
		des[src.size() - i] = src[i] - '0'; 
	}
}

int main()
{	
	string s1, s2;
	int a[201] = {0};
	int b[201] = {0};
	int ans[201] = {0};
	cin >> s1 >> s2;
	/* 字符型转整型,通过反转使得个位对齐
	   s1: 1234
	   s2: 567
	   序列号:01234
			a:  4321
			b:  765

	*/
	strtoint(s1, a);
	strtoint(s2, b);
	// 计算ans数组长度,按最长位+1
	int a_size = s1.size(), b_size = s2.size();
	int ans_size = max(a_size, b_size) + 1;
	// 对位相加得到ans数组
	for (int i = 1;i <= ans_size;i++) {
		ans[i] = a[i] + b[i] + ans[i]; // a+b+进位
		ans[i + 1] = ans[i] / 10; // 进位
		ans[i] %= 10; // 留个位数
	}
	// 去除前导0
	while (ans[ans_size] == 0 && ans_size > 1)
		ans_size--;
	// 倒序打印得最后结果
	for (int i = ans_size;i >= 1;i--)
		cout << ans[i];
	return 0;
}

5. 高精度减法

在这里插入图片描述
这里多加了一下a和b大小的判别

#include <iostream>
using namespace std;

// 字符型转整型
void strtoint(string src, int des[]) {
	for (int i = 0;i < src.size();i++) {
		//从[1]开始倒序存整型数字,使得个位对齐
		des[src.size() - i] = src[i] - '0';
	}
}

// 比较字符串输入数的大小
bool cmpstring(string str1, string str2) {
	if (str1.size() != str2.size())
		return str1.size() > str2.size();
	else
		return str1 >= str2;
}

int main()
{
	string s1, s2;
	int a[201] = { 0 };
	int b[201] = { 0 };
	int ans[201] = { 0 };
	cin >> s1 >> s2;
	// 保证大数减小数
	if (cmpstring(s1, s2) == false) {
		swap(s1, s2);
		cout << "-";
	}

	/* 字符型转整型,通过反转使得个位对齐
	   s1: 1234
	   s2: 567
	   序列号:01234
			a:  4321
			b:  765

	*/
	strtoint(s1, a);
	strtoint(s2, b);
	// 计算ans数组长度
	int a_size = s1.size(), b_size = s2.size();
	int ans_size = max(a_size, b_size);
	// 对位相减得到ans数组
	for (int i = 1;i <= ans_size;i++) {
		// 判断够不够减
		if (a[i] < b[i]) {
			a[i + 1]--;
			a[i] += 10;
		}
		ans[i] = a[i] - b[i];
	}
	// 去除前导0
	while (ans[ans_size] == 0 && ans_size > 1)
		ans_size--;
	// 倒序打印得最后结果
	for (int i = ans_size;i >= 1;i--)
		cout << ans[i];
	return 0;
}

6. 高精度乘法

在这里插入图片描述

#include <iostream>
using namespace std;

// 字符型转整型
void strtoint(string src, int des[]) {
	for (int i = 0;i < src.size();i++) {
		//从[1]开始倒序存整型数字,使得个位对齐
		des[src.size() - i] = src[i] - '0';
	}
}

int main()
{
	string s1, s2;
	int a[101] = { 0 };
	int b[101] = { 0 };
	int ans[201] = { 0 };
	cin >> s1 >> s2;
	strtoint(s1, a);
	strtoint(s2, b);
	// 计算ans数组长度
	int a_size = s1.size(), b_size = s2.size();
	int ans_size = a_size + b_size;
	/*
		序列号:5	 4	  3	   2	  1
				    a4b1 a3b1 a2b1  a1b1
			  a4b2	a3b2 a2b2 a1b2
	得:
	ans[i + j - 1] += a[i] * b[j]
	*/
	for (int i = 1;i <= a_size;i++) {
		for (int j = 1;j <= b_size;j++) {
			ans[i + j - 1] += a[i] * b[j];
			ans[i + j] += ans[i + j - 1] / 10;
			ans[i + j - 1] %= 10;
		}
	}
	// 去除前导0
	while (ans[ans_size] == 0 && ans_size > 1)
		ans_size--;
	// 倒序打印得最后结果
	for (int i = ans_size;i >= 1;i--)
		cout << ans[i];
	return 0;
}

7. 冒泡排序

  • 时间复杂度:O( n 2 n^2 n2)
  • 空间复杂度:O( 1 1 1)
  • 稳定性:稳定

在这里插入图片描述
代码:

void bubbleSort(vector<int>& nums) {
    int n = nums.size();
    for (int i = 0; i < n-1; i++) {
        bool swapped = false;
        
        for (int j = 0; j < n-i-1; j++) {
            if (nums[j] > nums[j+1]) {
                swap(nums[j], nums[j+1]);
                swapped = true;
            }
        }
 
        // 如果一轮遍历没有发生交换,说明序列已经有序,提前结束排序
        if (!swapped) 
            break;
    }

例题:
在这里插入图片描述

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

int BubbleSort(vector<int>& a,int n) {
	int swapped_num = 0;
	for (int i = 0;i < n - 1;i++) {
		// 判断是否提前结束
		bool swapped = false;
		for (int j = 0;j < n - 1 - i;j++) {
			if (a[j] > a[j + 1]) {
				swap(a[j], a[j + 1]);
				swapped_num++;
				swapped = true;
			}
		}
		// 如果内层循环没有再交换说明剩下的已经是有序的了,可提前结束
		if (swapped == false)
			break;
	}
	return swapped_num;
}

int main()
{
	int n;
	int swapped_num;
	cin >> n;
	// 长度为变量,使用动态数组
	vector<int> a(n);
	for (int i = 0;i < n;i++)
		cin >> a[i];
	swapped_num = BubbleSort(a, n);
	cout << swapped_num;
	return 0;
}

8. 选择排序

在这里插入图片描述

  • 时间复杂度:O( n 2 n^2 n2)
  • 空间复杂度:O( 1 1 1)
  • 稳定性:不稳定(对于[20,20,5]这种情况,第一个20会和5交换,从而到第二个20的后面。改变了相等值的前后顺序,故不稳定)

代码:

void SelectSort(vector<int>& a) {
	int n = a.size();
	for (int i = 0;i < n;i++) {
		int minIndex = i;
		for (int j = i + 1;j < n;j++) {
			if (a[j] < a[minIndex])
				minIndex = j;
		}
		swap(a[i], a[minIndex]);
	}
}

9. 插入排序

在这里插入图片描述

  • 时间复杂度:O( n 2 n^2 n2)
  • 空间复杂度:O( 1 1 1)
  • 稳定性:稳定

代码:

void InsertSort(vector<int>& a) {
	for (int j = 1;j < a.size();j++) { //构造无序区
		for (int i = 0;i < j;i++) { //构造有序区
			if (a[j] < a[i]) {
				// 后移插入
				int tmp = a[j];
				for (int k = j - 1;k >= i;k--) {
					a[k + 1] = a[k];
				}
				a[i] = tmp;
				break; // 跳出有序区的循环
			}
		}
	}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2039339.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

花式表演无人机技术详解

花式表演无人机作为现代科技与艺术融合的典范&#xff0c;以其独特的飞行姿态、绚烂的灯光效果及精准的控制能力&#xff0c;在各类庆典、体育赛事、音乐会等合中展现出非凡的魅力。本文将从以下几个方面对花式表演无人机技术进行详细解析。 1. 三维建模与编程 在花式表演无人…

SpringBoot整合日志功能(slf4j+logback)详解

目录 一、日志门面与日志实现 1.1 什么是日志门面和日志实现&#xff1f; 1.2 为什么需要日志门面&#xff1f; 二、简介 三、日志格式 四、记录日志 4.1 使用日志工厂 4.2 使用Lombok的Slf4j注解 五、日志级别 5.1 日志级别介绍 5.2 配置日志级别 5.3 指定某个包下…

SpringBoot配置--Profile

目录 使用profile 的原因&#xff1f; proflie 的配置方式 多profile文件方式 profile 激活方式 1 配置文件 2 命令行参数 小结 使用profile 的原因&#xff1f; 用来完成不同环境下&#xff0c;配置动态切换功能的&#xff08;具体什么意思呢&#xff1f;假设你在A电脑…

【算法速刷(8/100)】LeetCode —— 21.合并两个有序链表

使用两个指针顺序遍历两个链表&#xff0c;每次都将最小值的那个加到结果链表上&#xff0c;最后如果两个链表不一样长&#xff0c;就将剩下的接到结果后面 无头结点 无头结点的情况下&#xff0c;处处都需要进行判空&#xff0c;将初次赋值和其他时候分为两个情况&#xff0c…

cloud compare二次插件化功能开发详细步骤(一)

点云处理&#xff0c;有一个出名的处理软件&#xff0c;cloud compare&#xff0c;简称cc&#xff0c;将自己实现的功能以插件形式集成到CC里&#xff0c;方便使用 前提 环境&#xff1a;cc 2.13&#xff0c;qt 5.15&#xff0c;cmake 3.18&#xff0c;vs2019【其他组合也可&…

二、AI工作流(低代码)的趋势崛起在即。带你轻松玩转输入-文本组件

对工作流感兴趣的小伙伴可以去试一试。&#x1f525;偷偷的告诉你&#xff0c;它的GPTo4.0不要&#x1f4b0;。传送门&#xff1a;https://www.nyai.chat/chat?invitenyai_1141439 一、能用AI工作流介绍 能用AI-工作流是一个“低代码”工具、它也是个人或者中小企业的提效工…

8G显存玩转书生大模型

基础任务 使用 Cli Demo 完成 InternLM2-Chat-1.8B 模型的部署&#xff0c;并生成 300 字小故事&#xff0c;记录复现过程并截图。 尝试很多方法无解后在网页端重新输入&#xff1a; import torch from transformers import AutoTokenizer, AutoModelForCausalLM使用了Tran…

sql注入(判断字符型/数字型)

目录 字符型 数字型 字符型闭合方式 less-1 less-4 sql注入常见类型包括字符型和数字型&#xff08;json这里不介绍&#xff09; 以sql-labs靶场为例&#xff1b; 字符型 less-1&#xff1a;输入参数id&#xff1a; 这里我将sql查找语句一起输出了&#xff1b; 我们发现…

书生大模型实战营-基础关-XTuner 微调个人小助手认知

XTuner 微调个人小助手认知 环境配置模型效果预览微调数据准备微调配置微调训练权重格式转换模型合并页面对话 环境配置 # 创建虚拟环境 conda create -n xtuner0812 python3.10 -y# 激活虚拟环境&#xff08;注意&#xff1a;后续的所有操作都需要在这个虚拟环境中进行&#…

锂电池剩余寿命预测 | Matlab基于LSTM-Attention的锂电池剩余寿命预测

目录 预测效果基本介绍程序设计参考资料 预测效果 基本介绍 Matlab基于LSTM-Attention的锂电池剩余寿命预测&#xff08;单变量&#xff09;&#xff0c;长短期记忆神经网络融合注意力机制&#xff08;自注意力机制&#xff0c;多头注意力机制&#xff09;&#xff08;单变量&…

有效字的字母异位词

给定两个字符串 s 和 t &#xff0c;编写一个函数来判断 t 是否是 s 的字母异位词。 注意&#xff1a;若 s 和 t 中每个字符出现的次数都相同&#xff0c;则称 s 和 t 互为字母异位词。 示例 1: 输入: s "anagram", t "nagaram" 输出: true示例 2: 输…

8.14-LVS主从+nginx的haproxy+mysql的haproxy+读写分离

一、LVS-主从数据库 # nat # 添加规则 [rootDS ~]# ipvsadm -A -t 192.168.2.130:3306 -s rr [rootDS ~]# ipvsadm -a -t 192.168.2.130:3306 -r 192.168.2.40:3306 -m [rootDS ~]# ipvsadm -a -t 192.168.2.130:3306 -r 192.168.2.42:3310 -m [rootDS ~]# ipvsadm -Ln IP Vir…

javaweb学习笔记(8.10)

一、JS 1.1JS简介 Web标准&#xff1a;由3WC制订 三个组成部分&#xff1a; HTML---》网页的基础结构 CSS---》网页的表现效果 JavaScript---》网页的行为 简介&#xff1a;JS是一门跨平台、面向对象的脚本语言。用来控制网页行为的&#xff0c;使网页交互。 1.2JS的引入…

贷奇乐漏洞学习 --- 两个变态WAF绕过

代码分析 第一个WAF 代码 function dowith_sql($str) {$check preg_match(/select|insert|update|delete|\|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile/is, $str);if ($check) {echo "非法字符!";exit();}return $str;} 实现原理 这段PHP代码定义了一个…

Linux日常运维-主机名hosts

作者介绍&#xff1a;简历上没有一个精通的运维工程师。希望大家多多关注作者&#xff0c;下面的思维导图也是预计更新的内容和当前进度(不定时更新)。 本小章内容就是Linux进阶部分的日常运维部分&#xff0c;掌握这些日常运维技巧或者方法在我们的日常运维过程中会带来很多方…

探索消费新纪元:循环购模式的奥秘

在这个日新月异的消费时代&#xff0c;你是否听说过“消费1000送2000&#xff0c;每天领钱&#xff0c;提现无忧”的奇闻&#xff1f;或许你会疑惑&#xff0c;商家这是在慷慨解囊&#xff0c;还是在布下什么神秘的局&#xff1f;今天&#xff0c;让我作为你的私域电商向导&…

Linux应用--IO多路复用

一、I/O多路复用简介 socket通信&#xff0c;在Linux系统其是就是文件描述符&#xff0c;对应于内核中的缓冲区&#xff08;包含读缓冲区与写缓冲区&#xff09;&#xff0c;实质上是对读写缓冲区的操作&#xff1b;多路复用&#xff0c;多条路复用成一条路。 I/O多路复用使得程…

爬虫动态http代理ip:提高数据抓取的有效工具

爬虫动态HTTP代理IP的概述与应用 在网络爬虫的世界中&#xff0c;动态HTTP代理IP是一个非常重要的工具。它不仅能帮助用户提高数据抓取的效率&#xff0c;还能有效避免被目标网站封禁。本文将为您详细介绍什么是动态HTTP代理IP、其优势、使用场景及如何获取和配置。 1. 什么是…

NVDLA专题8:具体模块介绍——Convolution Accumulator

概述 卷积累加器(Convolution Accumulator&#xff0c; CACC)是CMAC之后的卷积流水线的一个阶段,CACC的定义在NV_NVDLA_cacc.v&#xff0c;module定义如下&#xff1a; module NV_NVDLA_cacc (cacc2sdp_ready //|< i,csb2cacc_req_pd //|<…

ZooKeeper服务器下载|安装|配置|启动|关闭|端口占用冲突解决

1、下载ZooKeeper ZooKeeper官网&#xff1a;https://zookeeper.apache.org/ 下载ZooKeeper二进制包 2、安装ZooKeeper 对ZooKeeper压缩包解压即可 tar -zxvf apache-zookeeper-3.9.2-bin.tar.gz -C /usr/local/3、配置ZooKeeper 来到ZooKeeper配置文件页面 cd conf复制z…