笔记
LUjh_
这个作者很懒,什么都没留下…
展开
-
C++简单学生类
编写一个程序,输入N个学生数据,包括学号、姓名、成绩,要求输出这些学生数据并计算平均分。 输入第一行为学生个数,接下来输入N个学生的数据。输出时,先输出N个学生,再输出平均分数(保留小数点后两位)。样例输入:3test1 101 90test2 102 87tets3 103 83样例输出:test1 101 90test2 102 87tets3 103 8386.67#include<iostream>#include<iomanip>原创 2021-03-14 22:24:32 · 1118 阅读 · 0 评论 -
C++使用const定义常变量指针
定义指向字符的常变量指针,并让其指向字符串"abc",然后将'a'修改为'z',并输出字符串提示:1、语句char * name =”abc”定义了常量字符串”abc”,类型是string literal,直接赋给char*类型的指针变量name有类型转换问题,C++11版本不进行隐含类型转换,并会告警。可以改为char * name =(char*)”abc”。2、这样定义的字符串是常量,不允许修改。所以可改为:char* const name=new char[4]{'a','b','c'};原创 2021-03-14 21:38:50 · 289 阅读 · 0 评论 -
C++引用传递
定义一个函数,参数为三个int类型变量的引用。在函数中交换三个参数的值,使其按升序排序。然后在主函数中输入三个整型数,调用该函数,按升序输出这三个数 例如:输入: 3 4 -1输出: -1 3 4#include<iostream>using namespace std;void add(int& a, int& b, int& c) { if (a > b) { int temp = a; a = b; b = temp; }原创 2021-03-14 21:29:30 · 1722 阅读 · 0 评论 -
C++定义和使用动态数组
创建并释放一维数组#include<iostream>using namespace std;int main(){ int n; cin>>n; //分配动态一维数组 int *arr=new int[n]; for(int i=0;i<n;i++) cin>>arr[i]; for(int i=0;i<n;i++) cout<<arr[i]<原创 2021-03-14 21:17:14 · 217 阅读 · 0 评论 -
数据结构专题实验记录1-简单背包问题
有n个物品,每个物品都有体积,可用二进制表示物品的有无:1为有,0为无。例如有四个物品,二进制数 0000表示四个物品皆无。所以从0到pow(2,n)-1为所有情况,对每种情况进行位运算temp>>1表示下一个物品,共有n次,每次与1进行&运算判断有无,有则相加,无则跳过,最后与T相比较进行判断。#include<iostream>#include<math.h>using namespace std;int main(){ int t; cin .原创 2020-12-19 23:05:30 · 136 阅读 · 0 评论 -
数据结构树的创建
下面展示一些 内联代码片。// An highlighted block#include<iostream>#include<stdlib.h>using namespace std;typedef struct tree { int data; struct tree* left; struct tree* right;}bitree,*btree;void creat(btree& t){ int input; cout << "in原创 2020-12-19 11:24:30 · 401 阅读 · 1 评论 -
条件收敛计算pi
条件收敛计算pi通过计算前n项估算Π/4,进而计算Π的值// An highlighted block#include<bits/stdc++.h>using namespace std;int main(){ int n; double pi=0;//后续进行除法运算用double cin>>n; for(int i=1;i<=n;i++){ if(i&1)pi+=1.0/(2*i-1); else pi-=1.0/(2*i-1); }原创 2020-08-26 11:41:42 · 323 阅读 · 0 评论