#include<bits/stdc++.h>
using namespace std;
bool isCharacter(char c){
c = tolower(c);
if(c>='a' && c<='z')
return true;
return false;
}
bool isValid(string s){
// 用户名的首字符必须是大写或者小写字母。
//用户名只能包含大小写字母,数字。
//用户名需要包含至少一个字母和一个数字。
//如果用户名合法,请输出 "Accept",反之输出 "Wrong"。
int len=s.length();
int d=0,c=0;
for(int i=0; i<len;++i){
char cur = s[i];
if(i==0 && isCharacter(cur) == false)//首字符是否是字符
return false;
if(isCharacter(cur)==false && isdigit(cur)==false)//既不是字符也不是字母
return false;
if(isCharacter(cur)) d++;
if(isdigit(cur)) c++;
}
return d && c;
}
int main(){
int num; string str;
cin>>num;
while(num--){
cin>>str;
if(isValid(str)) cout<<"Accept"<<endl;
else cout<<"Wrong"<<endl;
}
}
#include<bits/stdc++.h>
using namespace std;
bool cmp(pair<int,int> a, pair<int,int> b){
if(a.first == b.first) return a.second<b.second;
return a.first>b.first;
}
int main(){
int n,m, my, w; ; vector<pair<int,int>> vi;
cin>>n>>m;
for(int i=0;i<n;++i){
cin>>my>>w;
int total = my+2*w;
vi.push_back(make_pair(total, i));
}
sort(vi.begin(),vi.end(),cmp);
vector<int> ans;
for(int i=0;i<m;++i){
ans.push_back(vi[i].second+1);
}
sort(ans.begin(),ans.end());
for(auto x: ans){
cout<<x<<" ";
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m;
cin>>n;
vector<int> A(n+10), B(n+10,-1);
for(int i=1;i<n+1;++i){
cin>>A[i];
}
cin>>m;
for(int i=0; i<m;++i){
int op,k,x,y;
cin>>op;
if(op == 2){
cin>>x;
cout<<B[x]<<endl;
}
if(op==1){
cin>>k>>x>>y;
for(int i=0; i<k && y+i<n+1;++i){
B[y+i] = A[x+i];
}
}
}
return 0;
}
下方代码会让n变为0,在下面的循环里要用到n,以后尽量多用初始化,少用赋值,如果用赋值,不要用while(n–)这样
va.push_back(-1),vb.push_back(-1);
while(n--){
cin>>temp;
va.push_back(temp);
vb.push_back(-1);
}
#include<bits/stdc++.h>
using namespace std;
int main(){
int len;
string str;
cin>>len>>str;
int start ,tail;
bool flag=false;
for(int i=0; i<len; ++i){
if(str[i] == 'M')
flag =true;
if(flag ==true && str[i] == 'T'){
start=i+1;
break;
}
}
flag=false;
for(int i=len-1; i>0; --i){
if(str[i] == 'T')
flag=true;
if(flag==true && str[i] == 'M'){
tail = i-1;
break;
}
}
cout<<str.substr(start,tail-start+1);
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
vector<vector<int>> vi (n+1, vector<int>(n+1,0));
map<int,bool> mp;
for(int i=1; i<n+1; ++i){
for(int j=1; j<n+1;++j){
cin>>vi[i][j];
}
mp[i] = true;
}
vector<int> ans;
for(int i=1; i<n+1;i++){
for(int j=1; j<n+1; ++j){
int cur = vi[i][j];//倾向的城市
if(mp[cur] == true){//如果未选过
ans.push_back(cur);
mp[cur] = false;//选取
break;
}
}
}
for(auto x:ans){
cout<<x<<" ";
}
return 0;
}
下方初始化为错误 , pref 数组为序号0开始,应该为序号1开始
vector<vector<int>> pref;
for(int i=1; i<n+1;++i){//装入二维数组
vector<int> temp(n+1);
for(int j=1;j<n+1;++j){
cin>>temp[j];
}
pref.push_back(temp);
}
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
int main(){
int a,b,c,d,e,f,g;
cin>>a>>b>>c>>d>>e>>f>>g;
vector<pii> vi{{e,a},{f,b},{g,c}};
sort(vi.begin(),vi.end());//低到高
ll ans=0;
for(int i=2; i>=0;i--){//高到低
int value =vi[i].first, cnt=min(d,vi[i].second);
ans+= (ll)value*cnt;
d-=cnt;
}
cout<<ans;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
string str;
cin>>str;
int ans=0; vector<int> dp(n,0);//以i为右端点的序列的E-F数量差
if(str[0] == 'E') dp[0]=1;
else dp[0]=-1;
for(int i=1;i<n;++i){
char c= str[i];
int num = c=='E'?1:-1;
if(dp[i-1]<0) dp[i] = num;
else dp[i]=num+dp[i-1];
ans=max(ans,dp[i]);
}
cout<<ans;
return 0;
}