用c语言代码编辑出所有的水仙数,每个水仙数独占一行
时间: 2024-05-22 17:16:37 浏览: 10
#include <stdio.h>
#include <math.h>
int main() {
int i, j, k, n, m;
for (n = 100; n <= 999; n++) {
i = n / 100; //获取百位数字
j = n / 10 % 10; //获取十位数字
k = n % 10; //获取个位数字
m = pow(i, 3) + pow(j, 3) + pow(k, 3); //计算立方和
if (m == n) { //判断是否为水仙数
printf("%d\n", n); //输出水仙数
}
}
return 0;
}
相关问题
用c语言代码写一个数独游戏
好的,以下是用C语言实现的数独游戏代码:
```c
#include <stdio.h>
#include <stdbool.h>
#define SIZE 9
bool is_row_valid(int sudoku[][SIZE], int row, int num) {
for (int col = 0; col < SIZE; col++) {
if (sudoku[row][col] == num) {
return false;
}
}
return true;
}
bool is_col_valid(int sudoku[][SIZE], int col, int num) {
for (int row = 0; row < SIZE; row++) {
if (sudoku[row][col] == num) {
return false;
}
}
return true;
}
bool is_box_valid(int sudoku[][SIZE], int start_row, int start_col, int num) {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
if (sudoku[row + start_row][col + start_col] == num) {
return false;
}
}
}
return true;
}
bool is_valid(int sudoku[][SIZE], int row, int col, int num) {
return is_row_valid(sudoku, row, num) &&
is_col_valid(sudoku, col, num) &&
is_box_valid(sudoku, row - row % 3, col - col % 3, num);
}
bool find_unassigned(int sudoku[][SIZE], int *row, int *col) {
for (*row = 0; *row < SIZE; (*row)++) {
for (*col = 0; *col < SIZE; (*col)++) {
if (sudoku[*row][*col] == 0) {
return true;
}
}
}
return false;
}
bool solve_sudoku(int sudoku[][SIZE]) {
int row, col;
if (!find_unassigned(sudoku, &row, &col)) {
return true;
}
for (int num = 1; num <= 9; num++) {
if (is_valid(sudoku, row, col, num)) {
sudoku[row][col] = num;
if (solve_sudoku(sudoku)) {
return true;
}
sudoku[row][col] = 0;
}
}
return false;
}
void print_sudoku(int sudoku[][SIZE]) {
for (int row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++) {
printf("%d ", sudoku[row][col]);
}
printf("\n");
}
}
int main() {
int sudoku[SIZE][SIZE] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
};
if (solve_sudoku(sudoku)) {
print_sudoku(sudoku);
} else {
printf("No solution found.\n");
}
return 0;
}
```
这个程序实现了数独游戏的求解功能,可以通过修改 `sudoku` 数组中的初始值来设置不同的数独谜题。
用c语言写个数独游戏代码
好的,我可以回答这个问题。以下是一个简单的数独游戏代码示例:
#include <stdio.h>
#define N 9
#define UNASSIGNED
int grid[N][N] = {
{3, , 6, 5, , 8, 4, , },
{5, 2, , , , , , , },
{, 8, 7, , , , , 3, 1},
{, , 3, , , , , 2, },
{9, , , 8, , , , , 5},
{, 5, , , , , 6, , },
{1, 3, , , , , 2, 5, },
{, , , , , , , 7, 4},
{, , 5, 2, , 6, 3, , }
};
int find_unassigned_location(int *row, int *col) {
for (*row = ; *row < N; (*row)++) {
for (*col = ; *col < N; (*col)++) {
if (grid[*row][*col] == UNASSIGNED) {
return 1;
}
}
}
return ;
}
int used_in_row(int row, int num) {
for (int col = ; col < N; col++) {
if (grid[row][col] == num) {
return 1;
}
}
return ;
}
int used_in_col(int col, int num) {
for (int row = ; row < N; row++) {
if (grid[row][col] == num) {
return 1;
}
}
return ;
}
int used_in_box(int box_start_row, int box_start_col, int num) {
for (int row = ; row < 3; row++) {
for (int col = ; col < 3; col++) {
if (grid[row + box_start_row][col + box_start_col] == num) {
return 1;
}
}
}
return ;
}
int is_safe(int row, int col, int num) {
return !used_in_row(row, num) && !used_in_col(col, num) && !used_in_box(row - row % 3, col - col % 3, num);
}
int solve_sudoku() {
int row, col;
if (!find_unassigned_location(&row, &col)) {
return 1;
}
for (int num = 1; num <= 9; num++) {
if (is_safe(row, col, num)) {
grid[row][col] = num;
if (solve_sudoku()) {
return 1;
}
grid[row][col] = UNASSIGNED;
}
}
return ;
}
void print_grid() {
for (int row = ; row < N; row++) {
for (int col = ; col < N; col++) {
printf("%d ", grid[row][col]);
}
printf("\n");
}
}
int main() {
if (solve_sudoku()) {
print_grid();
} else {
printf("No solution exists");
}
return ;
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)