收发牌程序
#include <stdio.h>
#include <sys/types.h>#include <unistd.h>
#include <stdlib.h>
int main ()
{
int poker,flag[53] = {0};
int i;
srand(getpid());
for (i = 0;i < 52;i++)
{
poker = rand()%52;
while (1 == flag[poker] )
{
poker = rand()%52;
}
flag[poker] = 1;
if(poker >=0 && poker <= 12)
{
printf("红桃%d\n",poker+1);
}
else if (poker >= 13 && poker <=25)
{
printf("黑桃%d\n",poker-12);
}
else if(poker >= 26 && poker <=38)
{
printf("方块%d\n",poker-25);
}
else if(poker >=39 && poker <=51)
{
printf("菜花%d\n",poker-38);
}
}
移位程序
#include <stdio.h>
int main()
{
char c = 'a';
int tmp = 0;
int count = 0;
int i;
char a = c;
for(i = 0;i < 8;i++)
{
tmp = c & 1;
if(tmp == 1)
{
count++;
}
c = c >>1;
}
printf("there are %d 1 in %c\n",count,a);
}
用指针方法逆序输出字符串
#include <stdio.h>#include <stdlib.h>
#include <string.h>
int main()
{
char*str;
int length,i;
str = (char*)malloc(sizeof(char)*50);
scanf("%s",str);
length = strlen(str);
for(i = length-1;i >= 0;i--);
{
printf("%c",*(str + i));
}
return 0;
}