C++ 基础学习 (自用)

一、头文件

 1.  iostream

#include <iostream>

 iostream  是标准的 输入 输出 流 头文件,编码是必备。再加上这句话(using namespace std; )在输入输出是就可以直接 cout<<  <<endl;   cin >> ; 不然的话,输入输出都得  在cout前面加std:: 例如(std::cout << std::endl;

#include <iostream>
 using namespace std;
 
 int main(){
 	cout << "Hello,World!" << endl;
 	return 0;
 }

2. cmath

#include <cmath>

cmath是数学函数库头文件 ,用于计算

常用函数:

abs()计算绝对值  ; sqrt() 计算平方根    cbrt()计算立方根

pow()幂运算      ceil() 向上取整    floor() 向下取整

3. cstring

#include <cstring>

cstring 是字符串头文件,输入输出字符串时需要它。

#include <iostream>
#include <cstring>
 using namespace std;
 
 int main(){
 	string str ="Hello,World!" ;
 	cout << str << endl;
 	return 0;
 }

4.algorithm  标准算法库

#include <algorithm>

max() 最大值    min()最小值     sort()排序

 5.     list  列表头文件

         stack   栈头文件

        map   图头文件

#include <list>

#include <stack>

#include <map>

6.  iomanip 操作符

#include <iomanip>

在C++中,#include <iomanip>是用于包含输入输出流操作符的头文件。这个头文件提供了一些用于格式化输入输出的操作符和函数,例如setwsetprecisionsetfill等。通过包含这个头文件,可以在程序中使用这些格式化操作符和函数来控制输出的格式,例如设置字段宽度小数位数填充字符等。

例如,可以使用setw来设置输出的字段宽度:

         使用 fixed << setprecision( ) 设置小数位数;

#include <iostream>
#include <iomanip>
 using namespace std;
 
 
 int main(){
 	
   int num = 123;
   double average1=1.23;
   
    cout << setw(10) << num << endl; //输出宽度 
    cout << fixed << setprecision(1) << average1 << endl; // 只输出一位小数 
 	return 0;
 }

这段代码将会输出“         123 ”和 “1.2 ”

因为setw(10)设置了输出的字段宽度为10。

fixed <<setprecision(1) << 设置输出小数位数为1.

二、训练笔记

1.  输入一个字符  ,生成一个等腰三角形,(高度也自己输入)

#include <iostream>
 using namespace std;
 
 int main(){
 	
    char c ; 
    int high ;
    
    cin >> c >> high ;
    
    for (int i =1 ; i<=high; i++){
    	
// 控制高度或者说行数,high,从第一行开始

    	for(int j =1; j<=high-i;j++ ){
    		cout << " ";
//  每一行中空格的数量,从图中可看出是一个倒序,
//比如 三行的等腰三角形,空格数为 2,1,0
		}
		for (int k =1; k<=2*i-1;k++){
			cout << c;
  // 输入字符 规律是 2*行数-1
		}
		cout << endl;  // 换行
	}
	 
 	return 0;
 }

结果

随记 :注意做题的规律分析,要求做一个等腰三角形,第一个for就控制行数,在第一个for中观察每一行中空格的数量字符的数量

空格:倒序,例如图中五行的等腰三角形,每行空格数为 4、3、2、1、0

  字符: 等差数列,1 3 5........ 故规律为         2*行数-1

2.闰年判断

       四年一闰,     百年不闰     ,四百年再闰

#include <iostream>
 using namespace std;
 
 int main(){
 	
    int x ;
   cin >> x ;
     
    if (x%100!=0 && x%4==0){
    	cout<< true << endl;
	} else if(x%400==0){
			cout<< true << endl;
	}else{
		cout<< false << endl;
	}
	 
 	return 0;
 }

3. 数组中找最小值;

#include <iostream>
 using namespace std;
 
 
 int main(){
 	
 	const int MAXN = 100;
     int x;
     int arr[MAXN];
     cin >> x ;
     
     for(int i =0 ;i<x;i++){
     	cin >> arr[i];  //将输入的数存入数组

	 } 
	 
	 int min =arr[0];
      for(int i =1 ;i<x;i++){
     	if (arr[i]<min){
     		min=arr[i];
		 }
	 } 
     cout<< min<<endl;
	 
 	return 0;
 }

笔记:

const:

声明常量:可以用const关键字来声明常量,例如:

const int MAX_VALUE = 100;

明只读变量:可以用const关键字来声明只读变量,这意味着该变量的值在声明后不能被修改,例如:

const int SIZE = 10;

函数参数:可以用const关键字来指示函数参数是只读的,这样函数内部不能修改该参数的值,例如:

void printArray(const int arr[], int size) {
    // 函数内部不能修改arr数组的值
}

  输入一组数到一个数组中;

#include <iostream>
 using namespace std;
 
 int main(){
 	
 	const int MAXN = 100;
     int x;
     int arr[MAXN];
     cin >> x ;
     
     for(int i =0 ;i<x;i++){
     	cin >> arr[i]; // 先是输入,后边直接数组下标就行。
	 } 

     
 	return 0;
 }

4.

#include <iostream>
#include <iomanip>
using namespace std;

int main(){
	int n , k;
	cin >>n >>k;
	
    int	a=n/k;
    //例如,如果 n 是 20,k 是 3,那么在 1 到 20 之间,
	//有多少个数是 3 的倍数呢?我们可以用整数除法 n / k 来计算。20 除以 3 等于 6,所以有 6 个数是 3 的倍数。
    int b=n-a;
    
   double sum1= a*(k+k*a)/2.0;
   //如果我们把所有的 K 的倍数从 1 到 n 之间列出来,会得到一个等差数列,
   //首项是 K,公差也是 K。这个等差数列的和可以用等差数列求和公式来计算。
   // sn = (a1+an)n/2
    cout<< sum1 <<endl;
    
      double sum2= n*(1+n)/2.0-sum1;
     // 全部的和减去 K倍的和;
   // 法2 double sum2 = (countB * (k + 1) + n) / 2.0;  // 非 K 的倍数的和 
    cout<<sum2<<endl;
      
    double average1 = sum1/a;		
	double average2 = sum2/b;
	
	// setprecision 设置输出小数位数
   cout << fixed << setprecision(1) << average1 << " " << fixed << setprecision(1) << average2 << endl;
    return 0;
	
} 

随记  : 计算平均值的时候除了传统算法之外,要有别的思想方法,如上题中求出可以被K整除的数是,可以直接用n/k求出个数,再用等差公式就和。                                            

5.

#include <iostream> 
using namespace std;

int main(){
	
	int n, k;
	
	cin >> n >> k;
	bool a[n]; 
	//状态数组 
	for(int i=0; i<n; i++){
		a[i]=true;  // 最开始都是开灯 
	}
		
//	for(int i=0; i<n; i++){
//		cout  << a[i] <<" " ; 
//	}
	
	// 进行操作 
	for(int i=1; i<=k ; i++){
		// 人
		for(int j=1 ; j<=n ; j++){
			//灯
		if(j%i==0){
// 人是从1开始的,灯在数组中从下标0开始;
			a[j-1]=!a[j-1];
		} 
			 
		} 
	} 		
	  
	 //判断逗号 
	 bool first = true; 
	for (int i =1 ; i<=n ; i++){
		if(a[i-1]==0){
			 if (!first) {
                cout <<',';
            }
            cout << i ;
            first = false;
		}	
	}
		cout << endl;
		
	return 0;
		
}

随记: 开关灯问题中,最主要的部分就是对状态数组的判断,最开始都标记为true视为开灯,接下来开始for循环 。第一个for控制人,第二个for控制灯,当灯号与人号可以除尽是,在改变数组的状态(从true到false),依次循环下去。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值