逛商场
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int n,a[N],x,ans=0;
int main() {
cin>>n;
for(int i=1; i<=n; i++) cin>>a[i];
cin>>x;
for(int i=1; i<=n; i++) {
if(a[i] <= x) {
ans ++, x-=a[i];
}
}
cout<<ans;
return 0;
}
进制转换
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int a[N], p=0;
int main() {
int n,r; cin>>n>>r;
while(n) {
a[++p] = n%r;
n /= r;
}
for(int i=p; i>=1; i--) {
if(a[i] <= 9) cout<<a[i];
else cout<<char(a[i] - 10 + 'A');
}
return 0;
}
P431 春游
点击查看代码
#include <iostream>
using namespace std;
int n,m,x,st[1010];
int main() {
cin>>n>>m;
while(m--){
cin>>x;
st[x] = 1; // st[x]=1, 编号 x 的同学到了
}
int cnt=0;
for(int i=0; i<n; i++) cnt += st[i];
if(cnt == n) cout<<n;
else{
for(int i=0; i<n; i++)
if(!st[i]) cout<<i<<" ";
}
return 0;
}
P432 密码合规检测
点击查看代码
#include <iostream>
using namespace std;
char line[101];
char pwd[101];
// 检查从 str 开始、长度为 l 的密码是否合规
bool check(char* str, int l) {
if (l < 6 || l > 12)
return false;
bool hasC = false, hasL = false, hasD = false, hasS = false;
for (int i = 0; str[i] != '\0'; i++) {
if ('A' <= str[i] && str[i] <= 'Z') {
hasC = true;
} else if ('a' <= str[i] && str[i] <= 'z') {
hasL = true;
} else if ('0' <= str[i] && str[i] <= '9') {
hasD = true;
} else if (str[i] == '!' || str[i] == '@' || str[i] == '#' ||
str[i] == '$') {
hasS = true;
} else
return false;
}
if (!hasS)
return false;
if (hasC + hasL + hasD < 2)
return false;
return true;
}
int main() {
cin >> line;
// 按逗号对输入进行切分,并依次判断
int len = 0;
for (int i = 0; line[i] != '\0'; i++) {
if (line[i] != ',') {
pwd[len] = line[i];
len++;
} else {
pwd[len] = '\0';
if (check(pwd, len))
cout << pwd << endl;
len = 0;
}
}
if (len > 0) {
pwd[len] = '\0';
if (check(pwd, len))
cout << pwd << endl;
}
return 0;
}
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int a[N], p=0;
bool chk(string s) {
int has_a=0, has_b=0,has_c=0,has_d=0;
int fa=0,fb=0,fc=0;
for(int i=0; i<s.size(); i++) {
if(s[i]>='a' && s[i]<='z') has_a ++, fa=1;
if(s[i]>='A' && s[i]<='Z') has_b ++, fb=1;
if(s[i]>='0' && s[i]<='9') has_c ++, fc=1;
if(s[i]=='!' || s[i]=='@' || s[i]=='#' || s[i]=='$') has_d++;
}
return (has_a + has_b + has_c + has_d == s.size()) &&
(s.size() >=6 && s.size()<=12) &&
(fa + fb+fc >=2 && has_d);
}
int main() {
string s="seHJ12!@,sjdkffH$123,sdf!@&12HDHa!,123&^YUhg@!";
getline(cin, s);
// xxxxx,yyyyyy
for(int i=0, j=0; i<s.size(); i=j+1) {
j = s.find(',', i);
if(chk(s.substr(i, max(j-i, (int)s.size())))) {
cout<<s.substr(i, max(j-i, (int)s.size()))<<endl;
}
if(j==-1) break;
}
return 0;
}