C++考试重点梳理
2022/1/3 17:11:28
本文主要是介绍C++考试重点梳理,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
结构体
#include <bits/stdc++.h> using namespace std; struct Book{//定义书的结构体 char title[200];//title是长度200以内的字符串 float price; }; int main(){ Book b[101],t;//定义了一个book类和一个101的book数组 int n; cin>>n; int i,j; for(i=1;i<=n;i++){//注意是从1开始的 cin.get();//吃掉空格 cin.get(b[i].title,200);//输入当前的书名 cin>>b[i].price;//输入当前的书的价格 } for(j=1;j<=n-1;j++){//冒泡排序, 承接上文的设定(从1开始) for(i=1;i<=n-j;i++){ if(b[i].price<b[i+1].price){ t=b[i]; b[i]=b[i+1]; b[i+1]=t; } } } for(i=1;i<=n;i++){//输出 cout<<b[i].title<<endl; } return 0; }
大数相加
#include <bits/stdc++.h> using namespace std; int num(char x){ return (int)x-48;; } char str(int x){ return (char)(x+48); } int main(){ string s1,s2,s;//定义三个字符串 cin>>s1;//用cin输入两个字符串 cin>>s2; bool flag = false;//用来标记是否有进位 string s0(""); if(s1.length() < s2.length()){ int cha = s2.length()-s1.length(); for(int i=0;i<cha;i++){ s0.append("0"); //append是从后append, 结束后会得到一个长度为cha的0串 } s1.insert(0,s0);//将上文得到的0串插入到短的字符串前面 }else{ int cha = s1.length()-s2.length(); for(int i=0;i<cha;i++){ s0.append("0"); } s2.insert(0,s0); } int n = s1.length(); string result = ""; for(int i=n-1;i>=0;i--){ int temp = 0; if(flag){//如果有进位 temp = num(s1[i]) + num(s2[i])+1; }else{//如果没有进位 temp = num(s1[i]) + num(s2[i]); } if(temp>=10){//判断是不是有进位 flag = true; temp = temp%10; }else flag=false; // cout<<temp; string a= ""; a+=str(temp);//把结果变成字符串 result.insert(0,a);//拼接起来 } cout<<result; return 0; }
十进制转十七进制
#include<iostream> using namespace std; char t(int x){//将数字和对应的应该转化为什么字符依次对应 if(x==10){ return 'a'; }else if(x==11){ return 'b'; }else if(x==12){ return 'c'; }else if(x==13){ return 'd'; }else if(x==14){ return 'e'; }else if(x==1){ return '1'; }else if(x==2){ return '2'; }else if(x==3){ return '3'; }else if(x==4){ return '4'; }else if(x==5){ return '5'; }else if(x==6){ return '6'; }else if(x==7){ return '7'; }else if(x==8){ return '8'; }else if(x==9){ return '9'; }else if(x==0){ return '0'; }else if(x==15){ return 'f'; }else if(x==16){ return 'g'; } return 0; } int num[100];//对与十七进制每一位进行储存 int main(){ int n; cin>>n; int j=0; while(n>=17){ //进行十位转十七位,参考二进制 num[j]=n%17; n=n/17; j++; } num[j]=n; for(int i=j;i>=0;i--){ //由低到高,倒序输出 cout<<t(num[i]); } return 0; }
梳理难记的代码
头文件
#include <bits/stdc++.h> using namespace std;
输入char数组型的字符
cin.get(a, 1007);
输入string类型的数组
cin>>s1;
deque的常用方法
deque<int> d; d.push_back(elem);//在容器尾部添加一个数据 d.push_front(elem);//在容器头部插入一个数据 d.erase(beg,end);//删除[beg,end)区间的数据,返回下一个数据的位置。 d.erase(pos);//删除pos位置的数据,返回下一个数据的位置。 d.at(idx);//返回索引idx所指的数据,如果idx越界,抛出out_of_range。 d.front();//返回第一个数据。 d.back();//返回最后一个数据 deque.size();//返回容器中元素的个数 deque.empty();//判断容器是否为空 d.sort(); //从大到小排序
这篇关于C++考试重点梳理的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-13PingCAP 戴涛:构建面向未来的金融核心系统
- 2024-05-09flutter3.x_macos桌面os实战
- 2024-05-09Rust中的并发性:Sync 和 Send Traits
- 2024-05-08使用Ollama和OpenWebUI在CPU上玩转Meta Llama3-8B
- 2024-05-08完工标准(DoD)与验收条件(AC)究竟有什么不同?
- 2024-05-084万 star 的 NocoDB 在 sealos 上一键起,轻松把数据库编程智能表格
- 2024-05-08Mac 版Stable Diffusion WebUI的安装
- 2024-05-08解锁CodeGeeX智能问答中3项独有的隐藏技能
- 2024-05-08RAG算法优化+新增代码仓库支持,CodeGeeX的@repo功能效果提升
- 2024-05-08代码报错不用愁,CodeGeeX一键完成代码修复、错误解释的功能上线了!