c++基础知识强化

#include<iostream>
#include<ctime>
#include<stdlib.h>
#include<cstring>

using namespace std;


//从0到n-1随机等概率的输出m个不重复的数 
void test_rand(int n,int m){
          
          srand((unsigned)time(NULL));

          for(int i = 0; i < n; i++)
          {
               if(rand()%(n-i)<m)
               {
               		cout<<i<<' ';
               		m--;
               }
          }
          cout<<endl;
}
void prim(int m,int n)
{
	if(m>=n)
	{
		while(m%n)	n++;
		m/=n;
		prim(m,n);
		cout<<n<<endl;
	}
}

struct A{
	int a;
	
};
//struct A a1,a2; 全局变量初始化为0 

void func(char str_arg[100])
{
	cout<<sizeof(str_arg)<<endl;
}

int main()
{
	
	//这种方式随机初始化 
	int *p1=new int [10];
	for(int i=0; i<10 ; i++)
	{
		cout<<p1[i]<<' ';
	}
	cout<<endl;
	//这种方式初始化为0 
	int *p2=new int [10]();
	for(int i=0; i<10 ; i++)
	{
		cout<<p2[i]<<' ';
	}
	
	
	//2 求0~n-1随机等概率求m个不同的数 
	test_rand(10,3);
	
	//3 分解质因数 
	prim(8,3);
	 
	 //4 函数内部变量随机初始化 
	 struct A a1,a2;
	 cout<<a1.a<<' '<<a2.a;
	 
	 //5
	char str[] = "hello";
	cout<<sizeof(str)<<endl;//6 对吧字符串\0也计算上 
	cout<<strlen(str)<<endl;//5
	char *p = str;
	cout<<sizeof(p)<<endl;//4
	func(str);//4
	  
	//6
	 
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值