T644263 T2
题意:
n乘 x 的范围内是 y 的倍数的有多少个?
错误代码:
C++#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,x,y,cnt=0;
cin>>n>>x>>y;
for(int i=0;i<n;i++)
{
if(x*i%y==0)
{
cnt++;
}
}
cout<<cnt;
return 0;
}
错误原因:
变量的范围开小了,应该是long long
正确代码:
C++#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll n,x,y,cnt=0;
cin>>n>>x>>y;
for(int i=0;i<n;i++)
{
if(x*i%y==0)
{
cnt++;
}
}
cout<<cnt;
return 0;
}
正确思路:
直接根据题意进行模拟
T644264 T3
题意:
输入一行字符串。约定‘#’可以表示前一个字符无效。‘@’可以让他与前一个换行符之间的字符全部无效。
错误代码:
C++#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int f=0;
string s,d;
cin>>s;
for(int i=0;i<s.size();i++)
{
if(s[i+1]!='#'&&s[i]!='#')
{
d+=s[i];
}
}
for(int i=d.size()-1;i>=0;i--)
{
if(d[i]=='@')
{
f=1;
for(int j=i+1;j<d.size();j++)
{
cout<<d[j];
}
}
}
if(f==0)
{
cout<<d;
}
return 0;
}
根据代码逻辑,那么输入以下样例aaaabbb##的时候会多余将 b 给删除。导致代码输出aaaabb,而实际上应该输出aaaabbb
正确代码 :
C++#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
stack<char>s;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
char x;
while(cin>>x)
{
if(x=='#')
{
s.pop();
}
else if(x=='@')
{
while(s.size()!=0)
{
s.pop();
}
}
else
{
s.push(x);
}
}
string a;
while(s.size()!=0)
{
a=s.top()+a;
s.pop();
}
cout<<a;
return 0;
}
正确思路:
运用栈,根据题目要求,如果输入#,那么就将上一次输入的内容弹出。如果输入@,那么就将栈清空。最后用一个字符串 a 来进行倒序的存储,最后输出 a。
T644265 T4
随机生成一个正整数集合,集合中的数各不相同,然后输出:其中有多少个数,恰好等于集合中另外两个(不同的)数之和?
错误代码:
C++#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
int a[105],sum=1,s[10005],n;
void dfs(int step,int cnt,int ans)
{
if(cnt==2||step>n)
{
if(cnt==2)
{
s[sum]=ans;
sum++;
}
return ;
}
dfs(step+1,cnt+1,ans+a[step]);
dfs(step+1,cnt,ans);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
dfs(1,0,0);
int cnt=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<sum;j++)
{
if(s[j]==a[i])
{
cnt++;
}
}
}
cout<<cnt;
return 0;
}
错误原因:
答案可能会有重复的,需要去除重复的,最后输出的才是真正的 CNT 答案。但是我们的代码中并没有去重,所以会重复记录一些相同的答案。
正确代码 :
C++#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
int a[105],t[1000005],cnt;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
for(int s=1;s<=n;s++)
{
if(a[i]!=a[j]&&a[i]!=a[s]&&a[j]!=a[s]&&t[a[s]]==0&&a[i]+a[j]==a[s])
{
cnt++;
t[a[s]]++;
}
}
}
}
cout<<cnt;
return 0;
}
重要代码解释及解题思路:
重要代码解释:
C++for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
for(int s=1;s<=n;s++)
{
if(a[i]!=a[j]&&a[i]!=a[s]&&a[j]!=a[s]&&t[a[s]]==0&&a[i]+a[j]==a[s])
{
cnt++;
t[a[s]]++;
}
}
}
}
前面两个for循环枚举出所有的方案。第三个for循环足够判断它是否等于某个数值。然后创建一个桶,判断它是否已经被cnt记录过。
解题思路:
逐个枚举所有情况,并用一个桶统计已出现的情况,如果发现该情况与以出现的情况相同,那么就不统计,否则的话我们还需要判断是否满足题目中的要求,如果满足,那么就统计。
T644266 T5
题意:
给出一个数,其偶数位不变(个位为第 1 位,十位为第 2 位,以此类推),奇数位做如下变换:将数字乘以 7,如果不大于 9 则作为变换结果,否则把结果的各位数相加,如果结果不大于 9 则作为变换结果,否则(结果仍大于 9)继续把各位数相加,直到结果不大于 9,作为变换结果。变换结束后,把变换结果的各位数相加,如果得到的和是 8 的倍数,则称一开始的正整数为幸运数。
错误代码:
C++#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
string s;
cin>>s;
for(int j=s.size()-1;j>=0;j--)
{
if(j%2==0)
{
if(s[j]=='0')s[i]='0';
else if(s[j]=='1')s[j]='7';
else if(s[j]=='2')s[j]='5';
else if(s[j]=='3')s[j]='3';
else if(s[j]=='4')s[j]='1';
else if(s[j]=='5')s[j]='8';
else if(s[j]=='6')s[j]='6';
else if(s[j]=='7')s[j]='4';
else if(s[j]=='8')s[j]='1';
else if(s[j]=='9')s[j]='9';
}
}
int da=0;
for(int j=0;j<s.size();j++)
{
da+=int(s[j]-'0');
}
if(da%8==0)cout<<"T\n";
else cout<<"F\n";
}
return 0;
}
错误原因:
C++if(j%2==0)
如果刚开始输入的字符串是偶数位的话,那么这一行代码奇数位的字符就不会进行更改,反倒是偶数位的会进行更改。
正确代码 :
C++#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
string s;
cin>>s;
int cnt=1;
for(int j=s.size()-1;j>=0;j--)
{
if(cnt%2==1)
{
if(s[j]=='0')s[j]='0';
else if(s[j]=='1')s[j]='7';
else if(s[j]=='2')s[j]='5';
else if(s[j]=='3')s[j]='3';
else if(s[j]=='4')s[j]='1';
else if(s[j]=='5')s[j]='8';
else if(s[j]=='6')s[j]='6';
else if(s[j]=='7')s[j]='4';
else if(s[j]=='8')s[j]='2';
else if(s[j]=='9')s[j]='9';
}
cnt++;
}
int da=0;
for(int j=0;j<s.size();j++)
{
da+=int(s[j]-'0');
}
if(da%8==0)cout<<"T\n";
else cout<<"F\n";
}
return 0;
}
主要修改及解题思路
主要修改:
C++int cnt=1;//14
if(cnt%2==1)//17
cnt++;//30
运用一个 CNT 变量来辅助判断奇偶,第一个字符为奇数,所以 cnt为1。之后每次循环将cnt增加。判断 CNT 的奇偶就可以判断当前位数的奇偶。
解题思路:
每次输入一个字符串。遍历字符串的每一位。因为只可能有9种情况,所以可以打表。同时在更改位数之前判断其奇偶。更改之后再逐个遍历它的每一位。用一个 int类型的变量。来进行每一位的增加,最后判断它是否为8的倍数。
重要代码解释
C++if(cnt%2==1)
{
if(s[j]=='0')s[j]='0';
else if(s[j]=='1')s[j]='7';
else if(s[j]=='2')s[j]='5';
else if(s[j]=='3')s[j]='3';
else if(s[j]=='4')s[j]='1';
else if(s[j]=='5')s[j]='8';
else if(s[j]=='6')s[j]='6';
else if(s[j]=='7')s[j]='4';
else if(s[j]=='8')s[j]='2';
else if(s[j]=='9')s[j]='9';
}
打表,列举所有情况。
T644267 T6
题意:
给出 n 个数。让每个数减 k。求每个数减K可以达到 m 时候 k 的最大值。
错误代码:
C++#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
int a[1000005];
int n,m;
int cheak(int x)
{
int cnt=0;
for(int i=1;i<=n;i++)
{
if(a[i]-x>=0)cnt+=(a[i]-x);
}
if(cnt>=m)return 1;
else return 0;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
int l=1,r=1000001,mid;
while(l+1!=r)
{
mid=(l+r)/2;
if(cheak(mid)==1)l=mid;
else r=mid;
}
cout<<l;
return 0;
}
错误原因:
右端点开小了,同时数组没有用long long 。
正确代码 :
C++#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
ll a[1000005];
ll n,m;
ll cheak(ll x)
{
ll cnt=0;
for(ll i=1;i<=n;i++)
{
if(a[i]-x>=0)cnt+=(a[i]-x);
}
if(cnt>=m)return 1;
else return 0;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(ll i=1;i<=n;i++)
{
cin>>a[i];
}
ll l=1,r=400000001,mid;
while(l+1!=r)
{
mid=(l+r)/2;
if(cheak(mid)==1)l=mid;
else r=mid;
}
cout<<l;
return 0;
}
主要修改及解题思路
正确思路:
由于数字较大,所以我们需要用二分查找法的方式查找出答案。
主要修改
将右端点增大,同时更改 a 数组的类型。
重要代码解释:
cheak函数用于判断当前的数字是否可以满足条件。可以满足返回1,否则返回0
C++ll cheak(ll x)
{
ll cnt=0;
for(ll i=1;i<=n;i++)
{
if(a[i]-x>=0)cnt+=(a[i]-x);//只有高于这个高度的树才会被砍断。
}
if(cnt>=m)return 1;
else return 0;
}
T644268 T7
题意:
输入 N。随后输入 n 条限制,每条限制的形式为“X 必须紧邻着 Y 挤奶”,要求奶牛 X 在挤奶顺序中必须紧接在奶牛 Y 之后,或者紧接在奶牛 Y 之前。
用 8 行输出一个奶牛的顺序,每行输出一头奶牛的名字,满足所有的限制。如果由多种顺序符合要求,输出字典序最小的奶牛顺序。
错误代码偏分,但是1分也没偏到
正确代码 :
C++#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
string a[]={" ","Beatrice","Belinda","Bella","Bessie","Betsy","Blue","Buttercup","Sue"},sc[10];
int t[10],pos[10],n,f;
bool z;
struct st
{
int a,b;
}s[10];
int wz(string x)
{
for(int i=1;i<=8;i++)
{
if(x==a[i])
{
return i;
}
}
return 0;
}
void dfs(int step)
{
if(f==1)return ;
if(step>8)
{
z=1;
for(int i=1;i<=n;i++)
{
int s1=s[i].a;
int s2=s[i].b;
if(abs(pos[s1]-pos[s2])!=1)
{
z=0;
break;
}
}
if(z==1)
{
for(int i=1;i<=8;i++)
{
cout<<sc[i]<<"\n";
}
f=1;
}
return ;
}
for(int i=1;i<=8;i++)
{
if(t[i]==0)
{
t[i]=1;
sc[step]=a[i];
pos[i]=step;
dfs(step+1);
t[i]=0;
}
}
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
string x,m,y;
cin>>x>>m>>m>>m>>m>>y;
int s1=wz(x);
int s2=wz(y);
s[i].a=s1;
s[i].b=s2;
}
dfs(1);
return 0;
}
正确思路:
一共只有8个名字,所以可以用 DFS 深度优先搜索去逐个遍历出所有的情况。同时用一个结构体来存储两头牛之间的相邻情况。每次列出一种情况之后,就用循环去判断它是否满足两条牛相邻的情况。如果满足就输出,并且将 f 变量改变。(f 变量是统计是否已经输出)
重要代码解释
wz函数用于获取当前牛的位置
C++int wz(string x)
{
for(int i=1;i<=8;i++)
{
if(x==a[i])//如果传入参数与这头牛的名字相等
{
return i;//返回这头牛的位置
}
}
return 0;
}
C++ for(int i=1;i<=8;i++)
{
if(t[i]==0)
{
t[i]=1;
sc[step]=a[i];
pos[i]=step;
dfs(step+1);
t[i]=0;
}
}
//50--60
这段代码用于逐个判断是否选择过这头牛,如果没有选过,那么就可以进行选或不选的操作。
C++ if(step>8)
{
z=1;
for(int i=1;i<=n;i++)
{
int s1=s[i].a;
int s2=s[i].b;
if(abs(pos[s1]-pos[s2])!=1)
{
z=0;
break;
}
}
if(z==1)
{
for(int i=1;i<=8;i++)
{
cout<<sc[i]<<"\n";
}
f=1;
}
return ;
}
//27--49
步数大于8的时候,遍历整个列表。判断列表中的各牛的位置是否满足条件。若不满足条件, Z为0。若满足条件输出这一种方案。同时将 f 设为1
总结:
比赛时的**数据范围需要查看清楚*。(T2,T6) ,循环中 重要的判断 ,特别是关于次数的判断,一定需要思考清楚再写代码。(T3,T5),以及题目中 主要的数据类型 ,需要先仔细读懂题目主要想要干什么,然后再开始写。(T3)