T643688 T3
题意:
输入一个时间,求出该时间之后的第一个回文时间(回文时间是指对于一个 24 小时制时间表示的合法字符串 ab:cd,一定有 a=d,b=c)
错误代码:
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);
char a,b,c,d,s;
cin>>a>>b>>s>>c>>d;
while(true)
{
if(d<'9')
{
d++;
}
else
{
d='0';
c++;
}
if(c=='6')
{
c='0';
b++;
}
if(b>'9')
{
a++;
}
if(a=='2')
{
if(b=='4')
{
b='0';
a='0';
}
}
if(a==d&&b==c)
{
cout<<a<<b<<s<<c<<d;
break;
}
}
return 0;
}
错误原因:
注意到关于 b 的判断了吗?
C++if(b>'9')
{
a++;
}
当 b 开始进位的时候没有归零。例如样例00:00答案就会错误。
正确代码:
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);
char a,b,c,d,s;
cin>>a>>b>>s>>c>>d;
while(true)
{
if(d<'9')
{
d++;
}
else
{
d='0';
c++;
}
if(c>='6')
{
c='0';
b++;
}
if(b>'9')
{
b='0';
a++;
}
if(a=='2')
{
if(b=='4')
{
b='0';
a='0';
}
}
if(a==d&&b==c)
{
cout<<a<<b<<s<<c<<d;
break;
}
}
return 0;
}
正确思路:
运用4个变量 ABCD 来模拟时间的流逝。同时判断如果 a == d 并且 b == c。那么我们就输出 A,B,”:“,C,D。
T643376 T5
题意:
给一个二进制的数字,将其转换到八进制。
错误代码:
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);
string s,d="";
cin>>s;
while(s.size()%3!=0)
{
s="0"+s;
}
for(int i=s.size()-1;i>=0;i-=3)
{
string a;
a+=s[i-2];
a+=s[i-1];
a+=s[i];
if(a=="001")d="1"+d;
if(a=="010")d="2"+d;
if(a=="011")d="3"+d;
if(a=="100")d="4"+d;
if(a=="101")d="5"+d;
if(a=="110")d="6"+d;
if(a=="111")d="7"+d;
}
cout<<d;
return 0;
}
错误原因:
如果 a 等于000的情况没有判断。
正确代码:
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);
string s,d="";
cin>>s;
while(s.size()%3!=0)
{
s="0"+s;
}
for(int i=s.size()-1;i>=0;i-=3)
{
string a;
a+=s[i-2];
a+=s[i-1];
a+=s[i];
if(a=="000")d="0"+d;
if(a=="001")d="1"+d;
if(a=="010")d="2"+d;
if(a=="011")d="3"+d;
if(a=="100")d="4"+d;
if(a=="101")d="5"+d;
if(a=="110")d="6"+d;
if(a=="111")d="7"+d;
}
cout<<d;
return 0;
}
正确思路:
由于数据会到100位 (1267650600228229401496703205376) ,所以我们需要强行转换。首先将字符串 s 的长度补充到3的倍数,然后在三个一组进行判断。
正确代码:
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);
string s,d="";
cin>>s;
while(s.size()%3!=0)
{
s="0"+s;
}
for(int i=s.size()-1;i>=0;i-=3)
{
string a;
a+=s[i-2];
a+=s[i-1];
a+=s[i];
if(a=="000")d="0"+d;
if(a=="001")d="1"+d;
if(a=="010")d="2"+d;
if(a=="011")d="3"+d;
if(a=="100")d="4"+d;
if(a=="101")d="5"+d;
if(a=="110")d="6"+d;
if(a=="111")d="7"+d;
}
cout<<d;
return 0;
}
T643690 T6
题意:
输入 n 块巧克力,每块巧克力的边长为 H和 W。有 k 个小朋友。现在可以将巧克力切成正方形的样子,并问所有小朋友都能分到巧克力的情况下,分到的最大巧克力边长是多少?
错误代码:
C++#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
int n,k;
struct st{
int s1,s2;
}s[100005];
int f(int x)
{
int cnt=0;
for(int i=1;i<=n;i++)
{
int a1,a2,a3;
a1=s[i].s1/x;
a2=s[1].s2/x;
a3=a1*a2;
cnt+=a3;
}
if(cnt>=k)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>k;
for(int i=1;i<=n;i++)
{
cin>>s[i].s1>>s[i].s2;
}
int l=0,r=100005,mid;
while(l+1<r)
{
mid=(l+r)/2;
if(f(mid)==1)
{
l=mid;
}
else
{
r=mid;
}
}
cout<<l;
return 0;
}
错误原因:
C++ for(int i=1;i<=n;i++)
{
int a1,a2,a3;
a1=s[i].s1/x;
a2=s[1].s2/x;
a3=a1*a2;
cnt+=a3;
}
切割巧克力的时候,A2总是取第一块巧克力的边长 (简称手滑写错)
正确代码:
C++#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
int n,k;
struct st{
int s1,s2;
}s[100005];
int f(int x)
{
int cnt=0;
for(int i=1;i<=n;i++)
{
int a1,a2,a3;
a1=s[i].s1/x;
a2=s[1].s2/x;
a3=a1*a2;
cnt+=a3;
}
if(cnt>=k)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>k;
for(int i=1;i<=n;i++)
{
cin>>s[i].s1>>s[i].s2;
}
int l=0,r=100005,mid;
while(l+1<r)
{
mid=(l+r)/2;
if(f(mid)==1)
{
l=mid;
}
else
{
r=mid;
}
}
cout<<l;
return 0;
}
重要代码解释:
C++int f(int x)
{
int cnt=0;
for(int i=1;i<=n;i++)
{
int a1,a2,a3;
a1=s[i].s1/x;
a2=s[1].s2/x;
a3=a1*a2;
cnt+=a3;
}
if(cnt>=k)
{
return 1;
}
else
{
return 0;
}
}
用for循环遍历所有的巧克力,并且用cnt记录切割的总共巧克力数量。如果总共巧克力数量大于目标数 k,就返回一,否则返回0。
另外,由于数据过大,所以主体用二分查找法去查找最大的值。
T643691 T7
题意:
给一个 n,找出不同放法出现的五子连线的数量(设为 k)输出 k 值的和
原代码纯粹骗分
正确代码:
C++#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
int a[7][7],n,t[30];
void f()
{
int ant=0;
for(int i=1;i<=5;i++)
{
if(a[i][1]==1&&a[i][2]==1&&a[i][3]==1&&a[i][4]==1&&a[i][5]==1)
{
ant++;
}
if(a[1][i]==1&&a[2][i]==1&&a[3][i]==1&&a[4][i]==1&&a[5][i]==1)
{
ant++;
}
}
if(a[1][1]==1&&a[2][2]==1&&a[3][3]==1&&a[4][4]==1&&a[5][5]==1)
{
ant++;
}
if(a[1][5]==1&&a[2][4]==1&&a[3][3]==1&&a[4][2]==1&&a[5][1]==1)
{
ant++;
}
t[ant]=1;
}
void dfs(int x,int y,int cnt)
{
if(cnt==n)
{
f();
return ;
}
else if(x>=6)
{
return;
}
else if(y>=6)
{
dfs(x+1,1,cnt);
}
else
{
a[x][y]=1;
dfs(x,y+1,cnt+1);a[x][y]=0;
dfs(x,y+1,cnt);a[x][y]=0;
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
dfs(1,1,0);
int ant=0,i=25;
while(i!=0)
{
if(t[i]==1)
{
ant+=i;
}
i--;
}
cout<<ant;
return 0;
}
正确思路:
本题棋盘的数量已经规定只有5×5。可以用 DFS 深度优先枚举出所有的排列方式,也就是选这个点是否安放棋子。如果安放的棋子达到了 n,那么就再用一个 f 函数。来判断当前方式能形成几种连线。
重要代码解释:
DFS 深度优先搜索,主要是判断这个点是放还是不放。如果放的数量到达了 n,那么就可以开始用 f 函数。
C++void dfs(int x,int y,int cnt)
{
if(cnt==n)
{
f();
return ;
}
else if(x>=6)
{
return;
}
else if(y>=6)
{
dfs(x+1,1,cnt);
}
else
{
a[x][y]=1;
dfs(x,y+1,cnt+1);a[x][y]=0;
dfs(x,y+1,cnt);a[x][y]=0;
}
}
f 函数用循环判断该点是否可以连成一条线。循环完之后单独判断两个对角
C++void f()
{
int ant=0;
for(int i=1;i<=5;i++)
{
if(a[i][1]==1&&a[i][2]==1&&a[i][3]==1&&a[i][4]==1&&a[i][5]==1)
{
ant++;
}
if(a[1][i]==1&&a[2][i]==1&&a[3][i]==1&&a[4][i]==1&&a[5][i]==1)
{
ant++;
}
}
if(a[1][1]==1&&a[2][2]==1&&a[3][3]==1&&a[4][4]==1&&a[5][5]==1)
{
ant++;
}
if(a[1][5]==1&&a[2][4]==1&&a[3][3]==1&&a[4][2]==1&&a[5][1]==1)
{
ant++;
}
t[ant]=1;
}
总结:
比赛时一定要细心再细心。不能再出现像第6题、第3题之类的情况。判断时所有的条件要考虑周到,不能像第5题判断条件缺少。