题目详细
| 题目 | 思路(没有/有一点/有) | 分数 |
|---|
| T850018 T1 | 有 | 10 |
| T850019 T2 | 有 | 100 |
| T850020 T3 | 有 | 0 |
| T850023 T4 | 有 | 100 |
| T850021 T5 | 有一点 | 0 |
| T850024 T6 | 有一点 | 75 |
| T850025 T7 | 有一点 | 39 |
T1 T850018
- 题意:有T组数据,开始输入n和t(t组数据)循环t次,每次输入一个字符串和一个数字,用if判断字符串是哪个位运算符,更新n后输出。
- 思路:代码很简单,先输入n和t,循环t次,每次输入x和y(一个字符串和一个数字),if判断后根据x更新n并输出即可。
- 原来的错误代码
C++#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,m;
cin>>n>>m;
string x;
int y;
while(m--){
cin>>x>>y;
if(x=="&"){
cout<<(n&y)<<endl;
n=n&y;
}else if(x=="^"){
cout<<(n^y)<<endl;
n=n^y;
}else if(x=="|"){
cout<<(n|y)<<endl;
n=n|y;
}else if(x=="<<"){
cout<<(n<<y)<<endl;
n=n<<y;
}else if(x==">>"){
cout<<(n>>y)<<endl;
n=n>>y;
}
}
return 0;
}
C++#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,m;
cin>>n>>m;
string x;
int y;
while(m--){
cin>>x>>y;
if(x=="&"){
cout<<(n&y)<<endl;
n=n&y;
}else if(x=="^"){
cout<<(n^y)<<endl;
n=n^y;
}else if(x=="|"){
cout<<(n|y)<<endl;
n=n|y;
}else if(x=="<<"){
cout<<(n<<y)<<endl;
n=n<<y;
}else if(x==">>"){
cout<<(n>>y)<<endl;
n=n>>y;
}
}
return 0;
}
- 错因:看到题目上写的“请用 int 完成这些操作”以为只用写int就行,没关“#define int long long”,删掉后过了。
T2 T850019
- 题意:T组数据,开始输入t(t组数据)循环t次,每次输入一个数字,用sum累加数字二进制下‘1’的数量,判断奇偶。
- 思路:先输入t,循环t次,每次输入x(一个数字),用函数变成二进制后再用sum累加‘1’的数量,最后输出。
- 原来的错误代码:无
- AC代码
C++#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int f(int x){
int ans=0;
while(x!=0){
if(x%2==1){
ans++;
}
x/=2;
}
return ans;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,sum=0;
cin>>n;
while(n--){
int x;
cin>>x;
sum+=f(x);
}
if(sum%2==0){
cout<<sum<<" "<<"0";
}else{
cout<<sum<<" "<<"1";
}
return 0;
}
T850020 T3
- 题意:开始输入n(一个数字),输出1到n中的素数。
- 思路:先输入n,循环n次,用函数(或埃式筛)判断i是否为素数,是则sum++,否则不动。最后输出sum。
- 原来的错误代码
C++#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int cnt[100000005];
void h(){
cnt[1]=cnt[0]=1;
for(int i=1;i<=1e8;i++){
if(cnt[i]==0){
for(int s=2;s*i<=1e8;s++){
cnt[s*i]=1;
}
}
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
h();
int n,sum=0;
cin>>n;
for(int i=1;i<=n;i++){
if(cnt[i]==0){
sum++;
}
}
cout<<sum;
return 0;
}
C++#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
bool cnt[100000005];
void h(){
cnt[1]=cnt[0]=1;
for(int i=1;i<=1e8;i++){
if(cnt[i]==0){
for(int s=2;s*i<=1e8;s++){
cnt[s*i]=1;
}
}
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
h();
int n,sum=0;
cin>>n;
for(int i=1;i<=n;i++){
if(cnt[i]==0){
sum++;
}
}
cout<<sum;
return 0;
}
- 错因:光看到题目上写的时间限制了,没看内存限制,把数组的int改成bool后过了。
T4 T850023
- 题意:输入两个数和一个n,若1≤x≤y≤n,x+y==n,x 和 y 都是质数则输出,若没有则输出“-1 -1”。
- 思路:思路1:枚举两个数,但会超时。思路2:枚举其中一个数,用减法算出另一个数,判断后输出。
- 原来的错误代码:无
- AC代码
C++#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int cnt[10000005];
void h(){
cnt[1]=cnt[0]=1;
for(int i=1;i<=1e7;i++){
if(cnt[i]==0){
for(int s=2;s*i<=1e7;s++){
cnt[s*i]=1;
}
}
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
h();
int n,m=0;
cin>>n;
for(int i=1;i<=n;i++){
int k=n-i;
if(cnt[k]==0&&cnt[i]==0&&i<=k){
cout<<i<<" "<<k<<endl;
m++;
}
}
if(m==0){
cout<<"-1 -1";
}
return 0;
}
T850021 T5
- 题意:T组数据,开始输入t(t组数据)循环t次,每次输入两个数字(x,y),若第x和第y个质数异或值是否是1,为一则输出“yes”否则输出“no”。
- 思路:原思路是按题目意思将两数异或再判断,但会RE,经过老师的提醒,我发现有且只有一种可能——1和2或2和1,直接判断即可。
- 原来的错误代码
C++#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int cnt[100005],a[100005];
void h(){
cnt[1]=cnt[0]=1;
for(int i=1;i<=1e5;i++){
if(cnt[i]==0){
for(int s=2;s*i<=1e5;s++){
cnt[s*i]=1;
}
}
}
int x=1;
for(int j=1;j<=1e5;j++){
if(cnt[j]==0){
a[x]=j;
x++;
}
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
h();
int t;
cin>>t;
for(int i=1;i<=t;i++){
int n,y;
cin>>n>>y;
if((a[n]^a[y])==1){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
}
return 0;
}
C++#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
for(int i=1;i<=t;i++){
int x,y;
cin>>x>>y;
if(x==1&&y==2||x==2&&y==1){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
}
return 0;
}
- 错因:没有想过只有2这个质数二进制下结尾是0,而其他都为1,而只有2和3二进制下异或值是1。
T6 T850024
- 题意:开始输入n,循环n次,每次输入两个数字(x,y),若x到y中i(这个数)和(i+1)/2都为质数,sum++。最后输出sum。
- 思路:原思路是按题目意思将两数中的数都查看一边,但只有75分,经过老师的提醒,我发现可以用前缀和做,最后输出a[r]-a[l-1]。
- 原来的错误代码
C++#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int cnt[100005],a[100005];
void h(){
cnt[1]=cnt[0]=1;
for(int i=1;i<=1e5;i++){
if(cnt[i]==0){
for(int s=2;s*i<=1e5;s++){
cnt[s*i]=1;
}
}
}
int x=1;
for(int j=1;j<=1e5;j++){
if(cnt[j]==0){
a[x]=j;
x++;
}
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
h();
int t;
cin>>t;
for(int i=1;i<=t;i++){
int n,y,sum=0;
cin>>n>>y;
for(int i=n;i<=y;i++){
int k=(i+1)/2;
if(cnt[i]==0&&cnt[k]==0){
sum++;
}
}
cout<<sum<<endl;
}
return 0;
}
C++#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int cnt[100005],a[100005];
void h(){
cnt[1]=cnt[0]=1;
for(int i=2;i<=1e5;i++){
if(cnt[i]==0){
for(int s=2;s*i<=1e5;s++){
cnt[s*i]=1;
}
}
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
h();
int n;
cin>>n;
a[0]=0;
for(int i=1;i<=1e5;i++){
int x=0;
if(cnt[i]==0&&cnt[(i+1)/2]==0){
x=1;
}
a[i]=a[i-1]+x;
}
for(int i=1;i<=n;i++){
int x,y;
cin>>x>>y;
cout<<a[y]-a[x-1]<<endl;
}
return 0;
}
- 错因:之前没有想过用前缀和做,加上后就不超时了。
- 备注:范围是后面的1e6喔,不是1e5。
T7 T850025
- 题意:开始输入n,输入n个数,输出其他数中自己因子的数量。
- 思路:原思路是按题目意思将所有数都查看一遍,但只有39分,经过老师的提醒,我发现可以用两个数组来存,第1个用来存这个数出现的次数,第2个用来存因子的数量,(怎么加就不细说了)最后用第2个数组-1(减掉自己)得到答案。
- 原来的错误代码
C++#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int a[1000000];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t,ans=0;
cin>>t;
for(int i=1;i<=t;i++){
cin>>a[i];
}
for(int i=1;i<=t;i++){
for(int j=1;j<=t;j++){
if(a[i]==1){
ans=1;
}else if(a[i]%a[j]==0){
ans++;
}
}
cout<<ans-1<<endl;
ans=0;
}
return 0;
}
C++#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int a[10000000];
int cnt[10000005];
int sum[10000005];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t,ans=0;
cin>>t;
for(int i=1;i<=t;i++){
cin>>a[i];
cnt[a[i]]++;
}
for(int i=1;i<=1e6;i++){
if(cnt[i]!=0){
for(int j=i;j<=1e6;j+=i){
sum[j]+=cnt[i];
}
}
}
for(int i=1;i<=t;i++){
ans=sum[a[i]]-1;
cout<<ans<<endl;
}
return 0;
}