T619145 质数筛
正确代码
#include<bits/stdc++.h>
using namespace std;
int n;
int zhishu(int x)
{
if(x<=1)
{
return 0;
}
for(int i=2;i*i<=x;i++)
{
if(x%i==0)
{
return 0;
}
}
return 1;
}
int a[1000000];
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
if(zhishu(a[i])==1)
{
cout<<a[i]<<" ";
}
}
return 0;
} T619147 陶陶摘苹果
#include<bits/stdc++.h>
using namespace std;
struct node
{
int g,l;
}k[100000];
bool cmp(node x,node y)
{
if(x.l==y.l)
{
return x.g<y.g;
}
return x.l<y.l;
}
int main()
{
int n,s,a,b,cnt=0;
cin>>n>>s>>a>>b;
for(int i=1;i<=n;i++)
{
cin>>k[i].g>>k[i].l;
}
sort(k+1,k+n+1,cmp);
for(int i=1;i<=n;i++)
{
if(k[i].g<=a+b&&s>=0&&s-k[i].l>=0)
{
cnt++;
s=s-k[i].l;
}
}
cout<<cnt;
return 0;
} T619148 单词的长度
#include<bits/stdc++.h>
using namespace std;
string a;
int b[10000];
int main()
{
int cnt=0;
while(cin>>a)
{
cnt++;
b[cnt]=a.size();
}
for(int i=1;i<=cnt;i++)
{
if(i==cnt)
{
cout<<b[i];
return 0;
}
cout<<b[i]<<",";
}
return 0;
} T619149 迷宫出口
错误代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
cout<<"Yes";
return 0;
} 正确代码
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
const int N = 25;
int n, k, ans;
int a[25], vis[25];
bool isprime(int x) {
if (x <= 1) return 0;
for (int i = 2; i * i <= x; i++) {
if (x % i == 0) return 0;
}
return 1;
}
void dfs(int u) // 当前选择到了第u个数字,已经选中了num个
{
if (u > n) {
int sum = 0, cnt = 0;
for (int i = 1; i <= n; i++) {
if (vis[i]) sum += a[i], cnt++;
}
if (cnt == k && isprime(sum)) ans++;
return;
}
// 选或不选
vis[u] = 1;
dfs(u + 1);
vis[u] = 0;
dfs(u + 1);
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
dfs(1);
cout << ans;
return 0;
}选数
正确代码 对整个写法不熟
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
const int N = 25;
int n, k, ans;
int a[25], vis[25];
bool isprime(int x) {
if (x <= 1) return 0;
for (int i = 2; i * i <= x; i++) {
if (x % i == 0) return 0;
}
return 1;
}
void dfs(int u) // 当前选择到了第u个数字,已经选中了num个
{
if (u > n) {
int sum = 0, cnt = 0;
for (int i = 1; i <= n; i++) {
if (vis[i]) sum += a[i], cnt++;
}
if (cnt == k && isprime(sum)) ans++;
return;
}
// 选或不选
vis[u] = 1;
dfs(u + 1);
vis[u] = 0;
dfs(u + 1);
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
dfs(1);
cout << ans;
return 0;
}T619153 全排列问题
正确代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e4+1;
int n,box[10];
bool vis[N];
void dfs(int x){
if(x>n){
for(int i=1;i<=n;++i){
cout<<" "<<box[i];
}
cout<<"\n";
}
for(int i=1;i<=n;++i){
if(vis[i])continue;
box[x]=i;
vis[i]=true;
dfs(x+1);
vis[i]=false;
}
}
int main(){
cin>>n;
dfs(1);
return 0;
}