C++学习:总结

本文展示了四个C++程序片段,涉及数组排序、优先队列使用、括号匹配以及字符串关联数组操作。分别实现了一个整数数组的排序,一个计算满足条件的整数序列,一个检查括号是否平衡的函数,以及一个城市与电话号码的映射关系输出。
摘要由CSDN通过智能技术生成
#include <bits/stdc++.h>
using namespace std;
int main()
{
  int n;
  cin >> n;
  int a[n];
  for(int i=0 ; i < n ;++i ){
    cin >> a[i];
  }

  sort(a,a + n);

  for(int i = 0;i< n;++i){
    cout  << a[i] << " ";
  }
  cout << endl;
  // 请在此输入您的代码
  return 0;
}
image-20240220164627226
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main(void) {
  ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int n,sum = 0; cin >> n;
	priority_queue <int> mm;
	for (int i = 0; i < n; ++i)
	{
		int x; cin >> x;
		mm.push(x);
		sum += x;
	}
	ll mx = mm.top();
	if (sum - mx >= mx-1)
	{
		cout << "Yes" << endl;
	}
	else {
		cout << "No" <<endl;
	}
	return 0;
		}
#include <bits/stdc++.h>
using namespace std;
const int N = 105;
 char s[N];
 stack <char> stc;

 int main(){
   int n;cin >> n;
   cin >> s;
   bool ans = true;
   for(int i = 0; i < n ;++i){
     if(s[i] == '('){
       stc.push('(');
     }
     else{
       if(stc.size() && stc.top() == '(' ){
         stc.pop();
       }
       else
        ans = false;
     }

    

   }
    if(stc.size())
      ans = false;
    
    if(ans)
      cout << "Yes" <<endl;
    else
      cout << "No" << endl;


   return 0;
 }
#include <bits/stdc++.h>
using namespace std;
map <string, vector<string> > mp;
vector<string> citynumbers;

int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int n; cin >> n;
    for (int i = 0; i < n; ++i) {
        string x, y; cin >> x >> y;
        if (!mp.count(y))
        {
            citynumbers.push_back(y);
        }
        mp[y].push_back(x);

    }

    for (const auto& citynumber : citynumbers)
    {
        cout << citynumber << ' ' << mp[citynumber].size() << '\n';
        for (const auto& i : mp[citynumber])
        {
            cout << i << '\n';
        }


    }
    // 请在此输入您的代码
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值