SOLVE JUM PIN GRM C#
加減數
using System;
public class Program
{
public static void Main()
{
int[] group1 = {26,10,48,22,32};
int[] group2 = {5,10,8,4,13};
int[] group3 = {28,17,49,8,39};
for(int i = 0; i< group1.Length; i++){
for(int j = 0; j< group2.Length; j++){
for(int k = 0; k< group3.Length; k++){
if(group1[i] * group2[j] - group3[k] == 55){
Console.WriteLine(group1[i] +", "+group2[j]+","+group3[k]);
}
}
}
}
Console.WriteLine("Hello World");
}
}
public class Program
{
public static void Main()
{
int[] group1 = {26,10,48,22,32};
int[] group2 = {5,10,8,4,13};
int[] group3 = {28,17,49,8,39};
for(int i = 0; i< group1.Length; i++){
for(int j = 0; j< group2.Length; j++){
for(int k = 0; k< group3.Length; k++){
if(group1[i] * group2[j] - group3[k] == 55){
Console.WriteLine(group1[i] +", "+group2[j]+","+group3[k]);
}
}
}
}
Console.WriteLine("Hello World");
}
}
數獨
using System;
public class Program
{
public static int boardSize = 9;
public static void Main()
{
int[,] board = {{0,0,0, 0,2,7, 1,0,0},
{ 0,0,0, 9,0,0, 0,0,0},
{0,0,7, 0,0,0, 0,0,0},
{0,0,0, 0,6,4, 0,0,0},
{7,0,0, 0,0,0, 0,1,2},
{6,0,0, 0,0,0, 0,0,0},
{9,0,0, 0,0,1, 0,0,0},
{0,0,0, 0,0,0, 0,0,0},
{0,7,3, 0,0,6, 9,8,1}};
Console.WriteLine(solveBoard(board));
printBoard(board);
}
public static bool isSameInRow(int [,] board, int number, int row){
for(int i = 0; i< boardSize; i++){
if(board[row,i] == number){
return true;
}
}
return false;
}
public static bool isSameInCol(int [,] board, int number, int col){
for(int i = 0; i< boardSize; i++){
if(board[i,col] == number){
return true;
}
}
return false;
}
public static bool isSameInBox(int [,] board, int number,int row, int col){
int localBoxRow = row - row%3;
int localBoxCol = col - col%3;
for (int i =localBoxRow; i< localBoxRow+3; i++){
for(int j = localBoxCol; j < localBoxCol+3; j++){
if(board[i,j] == number){
return true;
}
}
}
return false;
}
public static bool isValidPlacement(int [,] board, int number, int row, int col){
return ((!isSameInRow(board, number, row))&&
(!isSameInCol(board, number, col))&&
(!isSameInBox(board, number,row, col))//);
&& isExtraCondition(board));
}
public static bool isExtraCondition(int [,] board){
if(board[0,0] == 0 || board[0,1] == 0 || board[0,2] == 0
|| board[0,3] == 0 || board[1,0] == 0 ) {return true;}
if(board[0,0]+ board[0,1]+board[0,2]+board[0,3]+board[1,0] == 22){
return true;
}
return false;
}
public static bool solveBoard(int[,] board){
bool allSolved = true;
for(int row = 0; row < boardSize; row++){
for(int col = 0; col < boardSize; col++){
if(board[row,col] == 0){
allSolved = false;
for(int numberToTry = 1; numberToTry <= boardSize; numberToTry++){
if(isValidPlacement(board, numberToTry, row, col)){
board[row,col] = numberToTry;
if(solveBoard(board)){
return true;
}else {
board[row,col] = 0;
}
}
}
return false;
}
}
}
if(allSolved)return true;
return false;
}
public static void printBoard(int[,] board){
for(int i = 0; i< boardSize;i++){
for(int j = 0; j< boardSize;j++){
Console.Write(board[i,j]);
if(j % 3 == 2){
Console.Write("|");
}
}
Console.WriteLine("");
if(i % 3 == 2){
Console.WriteLine("--------------");
}
}
}
}
public class Program
{
public static int boardSize = 9;
public static void Main()
{
int[,] board = {{0,0,0, 0,2,7, 1,0,0},
{ 0,0,0, 9,0,0, 0,0,0},
{0,0,7, 0,0,0, 0,0,0},
{0,0,0, 0,6,4, 0,0,0},
{7,0,0, 0,0,0, 0,1,2},
{6,0,0, 0,0,0, 0,0,0},
{9,0,0, 0,0,1, 0,0,0},
{0,0,0, 0,0,0, 0,0,0},
{0,7,3, 0,0,6, 9,8,1}};
Console.WriteLine(solveBoard(board));
printBoard(board);
}
public static bool isSameInRow(int [,] board, int number, int row){
for(int i = 0; i< boardSize; i++){
if(board[row,i] == number){
return true;
}
}
return false;
}
public static bool isSameInCol(int [,] board, int number, int col){
for(int i = 0; i< boardSize; i++){
if(board[i,col] == number){
return true;
}
}
return false;
}
public static bool isSameInBox(int [,] board, int number,int row, int col){
int localBoxRow = row - row%3;
int localBoxCol = col - col%3;
for (int i =localBoxRow; i< localBoxRow+3; i++){
for(int j = localBoxCol; j < localBoxCol+3; j++){
if(board[i,j] == number){
return true;
}
}
}
return false;
}
public static bool isValidPlacement(int [,] board, int number, int row, int col){
return ((!isSameInRow(board, number, row))&&
(!isSameInCol(board, number, col))&&
(!isSameInBox(board, number,row, col))//);
&& isExtraCondition(board));
}
public static bool isExtraCondition(int [,] board){
if(board[0,0] == 0 || board[0,1] == 0 || board[0,2] == 0
|| board[0,3] == 0 || board[1,0] == 0 ) {return true;}
if(board[0,0]+ board[0,1]+board[0,2]+board[0,3]+board[1,0] == 22){
return true;
}
return false;
}
public static bool solveBoard(int[,] board){
bool allSolved = true;
for(int row = 0; row < boardSize; row++){
for(int col = 0; col < boardSize; col++){
if(board[row,col] == 0){
allSolved = false;
for(int numberToTry = 1; numberToTry <= boardSize; numberToTry++){
if(isValidPlacement(board, numberToTry, row, col)){
board[row,col] = numberToTry;
if(solveBoard(board)){
return true;
}else {
board[row,col] = 0;
}
}
}
return false;
}
}
}
if(allSolved)return true;
return false;
}
public static void printBoard(int[,] board){
for(int i = 0; i< boardSize;i++){
for(int j = 0; j< boardSize;j++){
Console.Write(board[i,j]);
if(j % 3 == 2){
Console.Write("|");
}
}
Console.WriteLine("");
if(i % 3 == 2){
Console.WriteLine("--------------");
}
}
}
}