
C++
亚尔蒂兰
前途似海,来日方长
展开
-
C++实现合并排序
Merge与Mergesort代码:#include<iostream>using namespace std;//合并排序int Mergesort(int a[],int left,int right);void Merge(int a[],int left,int mid,int right); int main(){ int n; cin>>n; int *a=new int[n]; for(int i=0;i<n;i原创 2022-04-04 22:30:45 · 1301 阅读 · 0 评论 -
C++汉诺塔问题
将n个从大到小的盘子从A移到B,输出移动步骤代码:#include<iostream>using namespace std;//汉诺塔 void f(int n,char a,char b,char c);int main(){ int n; cin>>n; char a,b,c; a='A'; b='B'; c='C'; int k=1; f(n,a,b,c); return 0;}void原创 2022-04-04 22:30:29 · 1531 阅读 · 0 评论 -
C++全排列
正在学习算法,用C++实现全排列:输入数字n,求1-n所有数字的全排列#include<iostream>using namespace std;//全排列 int f(int *arr,int k,int n);int main(){ int n; cin>>n; int *arr=new int[n](); for(int i=0;i<n;i++) { arr[i]=i+1; } f(ar原创 2022-04-04 22:29:54 · 1254 阅读 · 0 评论 -
利用指针数组和函数实现冒泡排序
#include<iostream>using namespace std;void bubblesort(int*arr,int length)//先声明函数int main(){ //定义数组 int n; cout << "请输入数组长度:"; cin >> n; int arr[n]; cout << "请输入数组元素:"; for (int i = 0; i < n; i++) { cin >> arr[原创 2021-01-28 09:53:30 · 968 阅读 · 0 评论 -
随机数小游戏
srand((unsigned int)time(NULL)); cout << "let's do a game,guess the number that I give." << endl; int num = rand() % 100 + 1; int val=0; while (1) { cin >> val; if (val > num) { cout << "猜测过大" << endl; } .原创 2021-01-28 09:55:01 · 354 阅读 · 0 评论 -
利用嵌套循环写乘法口诀表
int i, j; for (i = 1; i < 10; i++) { for (j = 1; j <= i; j++) { cout << j << "*" << i << "=" << i * j << " "; } cout << endl; }原创 2021-01-28 09:55:38 · 266 阅读 · 0 评论 -
do while语句求三位水仙花数
int num = 100; do { int a = 0, b = 0, c = 0; a = num % 10; b = num / 10 % 10; c = num / 100; if (a*a*a+b*b*b+c*c*c==num) { cout << num << endl; } num++; } while (num<1000);如何求数字个位十位百位:个位 a = num % 10;十位b = num原创 2021-01-28 09:56:05 · 1165 阅读 · 0 评论 -
结构体数组
刚学习到结构体,感觉有点难,像套娃一样,不过还算有一点点理解,结构体数组好像重新定义数据类型#include<iostream>#include<string> //输出字符型数据都要记得包含string头文件using namespace std;//结构体定义struct student{ string name; int age; int score; } ;int main(){//结构体数组 struct student arr[3]= {原创 2021-01-28 11:04:23 · 219 阅读 · 4 评论 -
嵌套结构体案例
这个案例还是有很多地方可以学习的,决定记录一下代码参考:黑马程序员匠心之作|C++教程从0到1入门编程#include<iostream>#include<string>#include<ctime> #include<stdlib.h> using namespace std;//定义结构体struct student { string sname; int score;};struct teacher{ string tnam原创 2021-01-31 17:50:01 · 848 阅读 · 2 评论 -
C++
20210508记#include <iostream>using namespace std;// 结构体的声明struct time{ int day; int month; int year;};// 使用typedef为结构体声明别名typedef struct student1{ char *name; int number; int age; time t; struct student1 *pnext;}St原创 2021-05-08 20:28:22 · 320 阅读 · 0 评论