码迷,mamicode.com
首页 > 编程语言 > 详细

数据结构 C++冒泡排序 数组当参数传递

时间:2019-09-12 23:52:59      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:error   c++   express   cte   冒泡   power   round   ret   ror   

冒泡排序
#include <iostream>

using namespace std;
void bubblesort1A(int A[],int n);
int main() {
int A[10]={0},n=0,i=0;
cin>>n;
for( i=0;i<n;i++)
cin>>A[i];
bubblesort1A( A , n);
for(int i=0;i<n;i++)
cout<<A[i]<<endl;
return 0;
}
void bubblesort1A(int A[],int n)
{
bool sorted = false;
while (!sorted)
{
sorted = true;
for (int i = 1; i < n; i++)
{
if (A[i - 1] > A[i])
{
swap(A[i - 1], A[i]);
sorted = false;
}
}n--;
}
}

数组当参数传递
#include <iostream>
using namespace std;
int test2(int a[]){
for(int i=0;i<5;i++){
cout<<a[i]<<endl;
}
}
int main(){
int a[5] = {1,2,3,4,5};
test2(a);
}
统计整数二进制展开中数位1的总数
#include <iostream>
using namespace std;
int countOnes( unsigned int n);
int main()
{ int n;
cin>>n;
countOnes(n);
cout<<countOnes(n)<<endl;
return 0;
}
int countOnes( unsigned int n)
{
int ones = 0;
while( 0< n)//在n缩减到0之前
{
ones +=(1&n);//检查最低位,若为1则计数 1看作00000001
n>>=1;//右移一位
}
return ones;
}
 
幂函数算法(蛮力迭代法)
#include<iostream>
using namespace std;
__int64 power2BF_I (int n);
int main(){
int n,z=0;
cin>>n;
cout<< (__int64) power2BF_I (n)<<endl;//如此处不加括号12: error: expected primary-expression before ‘long‘cout<< __int64 power2BF_I (n)<<endl;
return 0;
}
__int64 power2BF_I (int n){
__int64 pow=1;
while(0<n--)
pow<<=1;
return pow;
}

 

数据结构 C++冒泡排序 数组当参数传递

标签:error   c++   express   cte   冒泡   power   round   ret   ror   

原文地址:https://www.cnblogs.com/wwqdata/p/11515342.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!