1.奇数:
题目
求 l l l到 r r r间的奇数
代码
#include <cstdio>
using namespace std;
int l,r;
int main(){
scanf("%d%d",&l,&r);
if (l%2==0) l++;
if (r%2==0) r--;
printf("%d",(r-l+1)/2+1);
for (int i=l;i<=r;i+=2) printf("\n%d",i);
return 0;
}
2.求和
代码:
#include <cstdio>
#include <cstring>
using namespace std;
int n,s,ans,i; int answer[501];
int main(){
freopen("sum.in","r",stdin);
freopen("sum.out","w",stdout);
scanf("%d",&s);
for (n=1;n*(n+1)/2<s;n++);//等差数列
for (i=n;;i++){
ans=i*(i+1)/2;
if ((ans-s)%2) continue;
if (ans==s) break;
memset(answer,0,sizeof(answer));
int k=ans;
for (int j=i;j>=1;j--){
if (k-j*2>=s) k-=j*2,answer[++answer[0]]=j;//捡回去
if (k==s) break;
}
if (k==s) break;
}
printf("%d",i);
for (int i=answer[0];i>=1;i--) printf("\n%d",answer[i]);
return 0;
}
3.圆环
题目
求最大公约数
##代码:
#include <cstdio>
#include <algorithm>
#include <cctype>
using namespace std;
int n,x,y;
int in(){
int ans=0; char c=getchar();
while (!isdigit(c)) c=getchar();
while (isdigit(c)) ans=ans*10+c-48,c=getchar();
return ans;
}
int main(){
n=in(); x=in();
for (int i=1;i<n;i++){
y=in();
int gcd=__gcd(x,y);//算法库
printf("%d/%d\n",x/gcd,y/gcd);
}
return 0;
}
4.旋转
题目
求旋转的图形与图形拼在一起的黑块个数。
代码:
#include <cstdio>
using namespace std;
int m=4,c[31],n; bool a[31][31],b[31][31],d[31][31];
int main(){
scanf("%d",&n); char x;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++){
if (j==1) getchar();
scanf("%c",&x); a[i][j]=x-48;
}
while (m--){
int ans=0;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++){
if (!b[i][j]&&a[i][j]) c[i]++;
b[i][j]=b[i][j]||a[i][j];
d[i][j]=a[j][n+1-i];//旋转
}
for (int i=1;i<=n;i++) ans+=c[i];
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++) a[i][j]=d[i][j];
printf("%d\n",ans);
}
return 0;
}