开灯问题
#include <stdio.h>
#include <string.h>
#define MAXN 1000+10
int a[MAXN];
int main()
{
int i,j,n,k,first=1;
memset(a,0,sizeof(a));
scanf("%d%d",&n,&k);
for (i=1;i<=k;i++)
{
for (j=1;j<=n;j++)
if (j%i==0)a[j]=!a[j];
}
for (i=1;i<=n;i++)
if (a[i]) {
if (first) first=0;else printf("");
printf("%d",i);
}
printf("\n");
return 0;
}memset作用是把数组清零
first可以表示当前要输出的变量是否为第一个
第一个变量之前不能有空格,其他都有
蛇形填数
#include <stdio.h>
#include <string.h>
#define MAXN 10
int a[MAXN][MAXN];
int main()
{
int n,x,y,tot=0;
scanf("%d",&n);
memset(a,0,sizeof(a));
tot=a[x=0][y=n-1]=1;
while(tot<n*n)
{
while(x+1<n && !a[x+1][y])a[++x][y]=++tot;
while(y-1>=0&&!a[x][y-1])a[x][–y]=++tot;
while(x-1>=0&&!a[x-1][y])a[–x][y]=++tot;
while(y+1<n&&!a[x][y+1])a[x][++y]=++tot;
}
for (x=0;x<n;x++)
{
for (y=0;y<n;y++)
printf("%3d",a[x][y]);
printf("\n");
}
return 0;
}
竖式问题
#include <stdio.h>
#include <string.h>
#define NULL 0
int main()
{
int i,ok,abc,de,x,y,z,count=0;
char s[20],buf[99];
scanf("%s",s);
for(abc=111;abc<=999;abc++)
for(de=11;de<=99;de++)
{
x=abc*(de%10);y=abc*(de/10);z=abc*de;
sprintf(buf,"%d%d%d%d%d",abc,de,x,y,z);
ok=1;
for (i=0;i<strlen(buf);i++)
if (strchr(s,buf[i])==NULL)ok=1;
if (ok)
{
printf("<%d>\n",++count);
printf("%5d\nX%4d\n------\n%5d\n%4d\n-----\n%5d\n\n",abc,de,x,y,z);
}
}
printf(“The number of solutions=%d\n”,count);
return 0;
}