数组的复制:
#include <string.h>
memcpy(b, a, sizeof(int)*k);
memcpy(b, a, sizeof(double)*k);
memcpy(b, a, sizeof(a));
有编号为1~n的n盏灯,第一个人把所有灯打开,第2个人按下所有编号为2的倍数的开关,第3个人按下所有编号为3的倍数的开关,
依次类推,一共有k个人,问最后有哪些灯开着?
#include <stdio.h>
int main()
{
int n, k;
scanf("%d%d",&n, &k);
int a[1001] = {0};
for(int i = 1; i <= k; i++)
for(int j = 1; j <= n; j++)
if(j % i == 0)
a[j] = !a[j];
int first = 1;
for(int i = 1; i <= n; i++)
if(a[i] == 1)
{
if(first)
{
first = 0;
printf("%d", i);
}
else
printf(" %d", i);
}
printf("\n");
return 0;
}
memset的使用:
#include <stdio.h>
#include <string.h>
#define MAXN 1000+10
int a[MAXN];
int main()
{
memset(a, 0, sizeof(a));
int n, k;
scanf("%d%d", &n, &k);
for(int i = 1; i <= k; i++)
for(int j = 1; j <= n; j++)
if(j % i == 0)
a[j] = !a[j];
int first = 1;
for(int i = 1; i <= n; i++)
if(a[i])
{
if(first)
{
printf("%d", i);
first = 0;
}
else
printf(" %d", i);
}
printf("\n");
return 0;
}
sprintf itoa atoi函数的使用
竖式问题:找出所有形如abc*de的算式,算式中所有数字来自输入的数字集合。
#include <stdio.h>
#include <string.h>
int main()
{
char s[20], buf[99];
int abc, de, x, y, z, ok, count = 0, i;
scanf("%s", s);
for(abc = 100; abc <= 999; abc++)
for(de = 10; 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 = 0;
if(ok)
{
printf("<%d>\n", ++count);
printf("%5d\nX%4d\n-----\n%5d\n%5d\n-----\n%5d\n", abc, de, x, y, z);
}
}
printf("The number of solutions = %d\n", count);
return 0;
}