首页 > 编程语言 >c++实现简单算法【1】

c++实现简单算法【1】

时间:2024-05-25 18:34:22浏览次数:20  
标签:p2 p1 int c++ a1 算法 a2 简单 include

1.交换两数

int a=2,b=3;
int temp=a;a=b;b=a;

 函数包装

指针

#include <stdio.h>
#include<string.h> 
//#include <iostream>
//using namespace std;
void swap(int* a,int* b)
{
	int temp=*a;
	*a=*b;//修改a指针指向的地址的对应的变量的值,地址不变 
	*b=temp;
}
int main()
{
	int a1=3,a2=4;
	int* p1=&a1;int* p2=&a2;
	swap(p1,p2);
	printf("%d %d",a1,a2);

}

 输出:4 3

指针的值=地址

取地址=&变量

取该地址上变量的值=*p=*&变量

必须是改变地址上的值,不是改变地址

如果是改变地址:

#include <stdio.h>
#include<string.h> 
//#include <iostream>
//using namespace std;
void swap(int* a,int* b)
{
	int *temp=a;
	a=b;
	b=temp;
}
int main()
{
	int a1=3,a2=4;
	int* p1=&a1;int* p2=&a2;
	swap(p1,p2);
	printf("%d %d",a1,a2);

}

输出仍为:3 4

 引用(更方便)

#include <stdio.h>
#include<string.h> 
//#include <iostream>
//using namespace std;
void swap(int& a,int& b)
{
	int temp=a;
	a=b;
	b=temp;
}
int main()
{
	int a1=3,a2=4;
//	int* p1=&a1;int* p2=&a2;
	swap(a1,a2);
	printf("%d %d",a1,a2);

}

输出:4 3 

指针的引用

给指针加上引用,交换两个指针

#include <stdio.h>
#include<string.h> 
#include<typeinfo>
//#include <iostream>
//using namespace std;
void swap(int* &p1,int* &p2)
{
	//对指针的swap 
	int* temp=p1;
	p1=p2;
	p2=temp;

}
int main()
{
	int a1=3,a2=4;
	int* p1=&a1;int* p2=&a2;
	swap(p1,p2);
	printf("%d %d ",a1,a2);
	printf("%d %d ",*p1,*p2);

}

输出: 

但是不能这样:

#include <stdio.h>
#include<string.h> 
#include<typeinfo>
//#include <iostream>
//using namespace std;
void swap(int* &p1,int* &p2)
{
	//对指针的swap 
	int* temp=p1;
	*p1=*p2;
	*p2=*temp;

}
int main()
{
	int a1=3,a2=4;
	int* p1=&a1;int* p2=&a2;
	swap(p1,p2);
	printf("%d %d ",a1,a2);
	printf("%d %d ",*p1,*p2);

}

这个输出:4 4 4 4 

因为temp和p1同为指针,指向同一地址,*p1赋值为*p2时,*temp对应地址的值也是*p2

 

2.冒泡排序

第一次冒泡,交换n-1次 

第二次冒泡,交换n-2次

……

第n-1次冒泡,交换1次

n-1+(n-2)+……+1=总次数

标签:p2,p1,int,c++,a1,算法,a2,简单,include
From: https://blog.csdn.net/m0_68339197/article/details/139042873

相关文章

  • c++/c语法基础【2】
    目录1.memset数组批量赋值2.字符数组 ​编辑输入输出: 字符数组直接输入输出%s: gets!string.h 1.strlen:字符串去掉末尾\0的长度......
  • 【每周例题】力扣 C++ 字符串相乘
    字符串相乘题目字符串相乘题目分析1.首先,题目上标出了一条:注意:不能使用任何内置的BigInteger库或直接将输入转换为整数。这就是这道题的难度所在2.这样子的话,我们可以从手写乘法计算来寻找思路: ①首先我们需要将各位相乘的结果放入数组ansArr中,我们使用双重for循环计算......
  • WPF一个简单的属性编辑控件
    代码:publicclassPropertiesControl:Grid{[TypeConverter(typeof(LengthConverter))]publicdoubleRowHeight{get{return(double)GetValue(RowHeightProperty);}set{SetValue(RowHeightProperty,......
  • 『C++初阶』第四章--- 模板初级
    1.泛型编程    如何实现一个适合于所有类型的通用的交换函数呢?voidSwap(int&left,int&right){inttemp=left;left=right;right=temp;}voidSwap(double&left,double&right){doubletemp=left;left=right;right=temp;}voidSwap(ch......
  • C++基础知识学习笔记(5)——函数
    学习参考:https://www.bilibili.com/video/BV1et411b73Z?p=95&spm_id_from=pageDriver&vd_source=cc561849591f6a210152150b2493f6f3函数函数的默认参数可以为形参提供默认值。intadd(inta,intb=1,intc=2){ returna+b+c;}intmain(){ cout<<(add(1,3,......
  • 【2024】文字游侠AI丨一键创作爆文赚米!只需简单五步,小白可上手,附渠道和详细教程!
    在信息爆炸的今日,如何借助AI人工智能工具在头条等平台赚取收入?何谓“文字游侠”?它又是如何操作的?它的可靠性又如何呢?作为一名实践者,我愿与大家分享一些经验,希望对你们有所帮助。首先,让我们来了解一下什么是“文字游侠”。它是一种AI智能创作工具,能够根据原始内容进行二次创......
  • C++容器之无序集(std::unordered_set)
    目录1概述2使用实例3接口使用3.1construct3.2assigns3.3iterators3.4capacity3.5find3.6count3.7equal_range3.8emplace3.9emplace_hint3.10insert3.11erase3.12clear3.13swap3.14bucket_count3.15max_bucket_count3.16bucket_s......
  • 简单的贪心(一)
    礼物 查看测评数据信息国庆马上要到了。小明喜欢的礼物有n种分别是:公仔、电子手表、漫画书等。每种礼物有一件,每种礼物价钱都不一样。小明手头上有m元。小明最多可以买多少件礼物?输入格式第一行,两个整数:nm 1<=n<=100,1<=m<=100000。第二行,n个空格分开的整数(......
  • 代码随想录算法训练营第十六天 | 104.二叉树的最大深度、559.n叉树的最大深度、111.二
    104.二叉树的最大深度题目链接:https://leetcode.cn/problems/maximum-depth-of-binary-tree/文档讲解:https://programmercarl.com/0104.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E6%9C%80%E5%A4%A7%E6%B7%B1%E5%BA%A6.html#%E7%AE%97%E6%B3%95%E5%85%AC%E5%BC%80%E8%AF%BE......
  • Java数据结构与算法(平衡二叉树)
    前言平衡二叉树是为了提高二叉树的查询速度,通过满足特定的条件来保持其平衡性。平衡二叉树具有以下特点:左子树和右子树的高度差不会大于1,这是为了确保树的高度不会过大,从而减少查询时的磁盘I/O开销,提高查询速度。平衡二叉树上的所有结点的平衡因子(左子树深度减去右子树深度的......