二进制有几个1
int count_( int n)
{
int count=0;
while(n)
{
n=n&(n-1);
count++;
}
return count;
}
int main()
{
int a=0;
scanf("%d",&a);
int count=count_(a) ;
printf("%d",count);
return 0;
}
三子棋
#define _CRT_SECURE_NO_WARNINGS
#include
#include"game.h"
void InitBoard(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
//void DisplayBoard(char board[ROW][COL], int row, int col)
//{
// int i = 0;
// for (i = 0; i < row; i++)
// {
// printf(" %c | %c | %c\n", board[i][0], board[i][1], board[i][2]);
// if (i < row - 1)
// {
// printf("---|---|---\n");
// }
//
// }
//}
void DisplayBoard(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
if (i < row - 1)
{
//printf("---|---|---\n");
int j = 0;
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
}
}
}
void PlayerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("玩家请下棋:>\n");
printf("请输入你要下的地方:>");
scanf("%d %d", &x, &y);
if (x >= 1 && x = 1 && y
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("坐标已被占用,请重新输入\n");
}
}
else
{
printf("输入错误,请重新输入;>");
}
}
}
void ComputerMove(char board[ROW][COL], int row, int col)
{
printf("电脑请下棋;>\n");
while (1)
{
int x = rand()%row;
int y = rand()%col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
int Isfull(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (i = 0; i < col; i++)
{
if (board[i][j] == ' ')
return 0;
}
}
return 1;
}
char Iswin(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
{
return board[i][1];
}
}
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
{
return board[i][1];
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ' )
{
return board[1][1];
}
if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ')
{
return board[1][1];
}
int ret = Isfull(board,row,col);
if (ret == 1)
{
return 'Q';
}
else
{
return 'C';
}
#include
#include
#include
#define ROW 3
#define COL 3
void InitBoard(char board[ROW][COL], int row, int col);
void DisplayBoard(char board[ROW][COL], int row, int col);
void PlayerMove(char board[ROW][COL], int row, int col);
void ComputerMove(char board[ROW][COL], int row, int col);
char Iswin(char board[ROW][COL], int row, int col);
#include
#include"game.h"
#include"game.c"
void menu()
{
printf("*\n");printf(" 1.play \n");printf(" 0.exit \n");printf("\n");}void game(){char board[COL][ROW];InitBoard(board, ROW, COL);DisplayBoard(board, ROW, COL);char ret = 0;while (1){PlayerMove(board, ROW, COL);DisplayBoard(board, ROW, COL);ret=Iswin(board, ROW, COL);if (ret != 'C'){break;}ComputerMove(board, ROW, COL);DisplayBoard(board, ROW, COL);ret=Iswin(board,ROW, COL);if (ret != 'C'){break;}}if (ret == '#'){printf("电脑赢\n");}else if (ret == '')
{
printf("玩家赢\n");
}
else
{
printf("平局\n");
}
DisplayBoard(board, ROW, COL);
}
int main()
{
srand((unsigned)time(NULL));
int input = 0;
do
{
menu();
printf("请输入你的选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新选择\n");
break;
}
} while (input);
return 0;
}