标签: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;
}
标签:error c++ express cte 冒泡 power round ret ror
原文地址:https://www.cnblogs.com/wwqdata/p/11515342.html