微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

codeforces之4.1学习记录

记录一些之前没见过的代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 2000000000ll
#define SIZE 100000ll
#define pb push_back
//#define fin cin
int main()
{
    ifstream fin("input.txt",ios::in);
    ios_base::sync_with_stdio(false);
    cout.tie(0);
    cin.tie(0);
  cout<<setprecision(10);
  cout<<fixed;
  int n;
  string a,b;
  cin>>n>>a>>b;
  int k=0;
  int d[n];
  for(int i=0;i<n;i++)
  {
    d[i]=b[i]-a[i];
  }
  for(int i=0;i<n-1;i++)
  {
    if(d[i]%2==1)
    {
      d[i]--;
      d[i+1]+=26;
    }
    if(d[i+1]<0)
    {
      d[i]-=2;
      d[i+1]+=26*2;
    }
    d[i]/=2;
  }
  d[n-1]/=2;
  string out="";
  for(int i=n-1;i>-1;i--)
  {
    //cout<<d[i]<<" ";
    if(a[i]+d[i]>'z')
    {
      d[i-1]++;
      char o = a[i]+d[i]-26;
      out+=o;
    }else
    {
      char o = a[i]+d[i];
      out+=o;
    }
  }
  reverse(out.begin(),out.end());
  cout<<out;
}
e题代码

1.ifstream fin("input.txt",ios::in);

定义一个来自于文件input.txtt的输入流,用ios::in方式 打开,in方式表示要读取文件,文件不存在的话,不建立文件,而是得到一个空的ifstream对像所以一般程序中会有这样的写法:ifstream fin("D:\\studf.txt",ios::in)if(fin!=NULL)......

2.ios_base::sync_with_stdio(false);

这个语句能使cin的输入更快

cin,cout速度慢,是因为先把要输出的东西存入缓冲区,再输出,导致效率降低,而这段ios_base::sync_with_stdio(false)可以来打消iostream的输入输出缓存,可以节省许多时间,使效率与scanf与printf相差无几

3.减少cin,cout时间

ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);

4.控制格式输入输出函数setprecision(10);

5.reverse()反转字符串

#include<algorithm>

reverse(a,a+len);

6.找最大值最小值函数

max_element()函数和min_element()函数

string s;

char maxchar = *max_element(s.begin(), s.end()) ;

或者是

int a[n];

int maxn = *max_element(a,a+n) ;

7.set<char>(s.begin(), s.end()).size() == s.length();

string s;

set<char>(s.begin(), s.end()).size() == s.length();

意思是设一个char型的set集合

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐