1.门牌制作

在这里插入图片描述

//通过将数字转换为字符串进行遍历字符串然后计数某个字符出现的总次数 
#include<bits/stdc++.h>
using namespace std;
int main(){
	int count = 0;
	for(int i = 1;i <= 2020; i++){
		string str = to_string(i); 
		for(char c:str){
	       if(c == '1') count++;	
	  }	
	}	
	cout<<count<<endl;
	return 0;	
}

2.输出下图的字母三角形

在这里插入图片描述

//字母三角形 ——字符串的拼接 
#include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin>>n;
	for(int i = 1;i <= n; i++){
		string space = string(n - i,' ');
		string ch = string(2 * i - 1,'A' + i - 1);
		cout<<space + ch<<endl;
	}
	return 0;
}

3.分巧克力

在这里插入图片描述
在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
int main()
{
  // 请在此输入您的代码
  int n,k;
  cin>>n>>k;
  vector<int> w(n),h(n);
  for(int i = 0;i < n; i++){
    cin>>w[i]>>h[i];
  }
  int l=1,r=999999;
  int max_d=0;
  while(l<=r){
    int mid = l + (r - l) / 2;
    long long count = 0;
    for(int i = 0;i < n;i++){
      count += (long long) ((w[i] / mid) * (h[i] / mid));
      if(k<=count) break;
    }
    if(count>=k){
      max_d = mid;
      l = mid + 1;
    }
    else{
      r = mid - 1;
    }
  }
  cout<<max_d<<endl;
  return 0;
}

4.递归的例题

在这里插入图片描述

1.最大公约数

#include<bits/stdc++.h>
using namespace std;
int gcd(int m,int n){
	if(n == 0) return m;
	return gcd(n,m % n);
}
int main(){
	int m,n;
	cin>>m>>n;
	int res = gcd(m,n);
	cout<<res<<endl;
	return 0;
}

2.FJ的字符串

#include<bits/stdc++.h>
using namespace std;
string fun(int n){
	if(n == 1) return "A";
	return fun(n-1)+(char)('A' + n -1)+fun(n-1)	;
}
int main() {
	int n;
	cin>>n;
	cout<<fun(n)<<endl;
	return 0;
}

在这里插入图片描述

3.递归实现指数型枚举(dfs)

方法二(状态数组)

#include<bits/stdc++.h>
using namespace std;
const int N = 15;
int n;
int s[N];//状态数组,0表示待考虑,1表示选择该数,2表示不选择 
//x表示当前位置 
void dfs(int x){
	if(x == n){
		for(int i = 0;i < n ;i++){
			if( s[i] ==  i){
			print("%d",i + 1); 
			}
		}
		printf("\n");
		return ;
	}
	s[x] = 2;
	dfs(x+1);//不选
	s[x] = 0;//撤销,恢复到原先的状态 
	
	
	s[x] = 1;
	dfs(x + 1);//选 
	s[x] = 0; 
} 
int main(){
	cin>>n;
	dfs(n);
	return 0;
}

4.全排列

#include<bits/stdc++.h>
using namespace std;
const int N = 10;
int n;
bool used[N];
int res[N];

void dfs(int i){
	if(i>n){
		for(int k = 1;k <= n;k++){
			cout<<res[k]<<' ';
		}
		cout<<endl;
		return ;
	}
	for(int j = 1;j <= n;j++){
		if(!used[j]){
			res[i] = j;
			used[j] = true;
			dfs(i+1);
			used[j] = false;
			res[i] = 0;
		}
	}
}
int main(){
	cin>>n;
	dfs(1);
	return 0;
}

动态规划(DP)

在这里插入图片描述
在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;

int n, m, res;
int dp[35][35];
int main(){
	cin>>n>>m;
	if(m % 2==0&& n %2 == 0) return 0;
	else{
		for(int i = 1;i <=n; i++){
			for(int j = 1; j <= m; j++){
				if(i ==1 && j == 1) dp[i][j] = 1;
				else if(i % 2 == 1|| j % 2 == 1) dp[i][j] = dp[i-1][j] + dp[i][j-1];
			}
		}
		cout<<dp[n][m]<<endl;
	}            
	return 0;
}

排序

1.sort()函数

sort是c++标准库中的 中所提供的排序函数,能够对容器中的元素进行排序,默认数升序排序
示例:

#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

int main(){
	vector<int> nums = {1,9,5,2,8,2,};
	sort(nums.begin(),nums.end());
	for(int num : nums){
		cout<<num<< " ";
	}
	return 0;
}

2. 自定义比较函数(降序)

#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

bool compare(int a, int b){
	return a>b;	
}
int main(){
	vector<int> nums = {1,9,5,2,8,2,};
	sort(nums.begin(),nums.end(),compare);
	for(int num : nums){
		cout<<num<< " ";
	}
	return 0;
}

例: 学生成绩排序(由高到低)

#include<bits/stdc++.h>
using namespace std;

struct Student {
	string name;
	int score;
};

bool cmp_score(Student x,Student y){
	return x.score>y.score;
}
int main(){
	Student stu[3];
	string n;
	int s;
	for(int i = 0; i < 3; i++) {
		cin>>n>>s;
		stu[i].name = n;
		stu[i].score = s;
	}
	sort(stu,stu + 3,cmp_score);
	for(int i = 0;i < 3 ;i++){
		cout<<stu[i].name<<" "<<stu[i].score<<endl;
	}
	return 0;    
}

在这里插入图片描述

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐