The code is below and i should also explain the algorithm. Well, What we are doing here is that we already defined the size to be 9x9 sudoku and are getting values using loops. All the empty spots are given UNASSIGNED value. Then we have functions to tell that if it is safe to put a value in the empty box by calculation and according to the rules of Sudoku it checks for is there any other some number horizontally and vertically and do the sum of the row and column is less than or equal to required or not. If the functions returns true then the program puts the value there.#include #define UNASSIGNED 0#define N 9bool FindUnassignedLocation(int grid[N][N], int &row, int &col);bool isSafe(int grid[N][N], int row, int col, int num);bool SolveSudoku(int grid[N][N]){int row, col;if (!FindUnassignedLocation(grid, row, col))return true; // success!for (int num = 1; num
#include<iostream> #include<vector> unsigned count_digits (unsigned num, const unsigned base=10) { unsigned count=1; while (num/=base) ++count; return count; } class number { std::vector<unsigned> value; unsigned base; public: number (const unsigned _value, const unsigned _base=10): value {}, base {_base} { *this = _value; } number& operator= (const unsigned _value); operator unsigned () const; bool is_narcissistic () const; }; number& number::operator= (unsigned _value) { unsigned count = count_digits (_value, base); value.resize (count); while (count) { value[value.size()-count--] = _value%base; _value/=base; } return *this; } number::operator unsigned () const { unsigned num = 0; for (unsigned index=0; index<value.size(); ++index) num += value[index]*static_cast<unsigned>(std::pow (base, index)); return num; } bool number::is_narcissistic () const { unsigned num = 0; for (unsigned index=0; index<value.size(); ++index) num += static_cast<unsigned>(std::pow (value[index], value.size())); return num == static_cast<unsigned> (*this); } unsigned main() { const unsigned min=1; const unsigned max=100; std::cout << "Narcissistic numbers in the range " << min << " through " << max << ":\n\t"; for (unsigned n=min; n<=max; ++n) if (number(n).is_narcissistic()) std::cout << n << ' '; std::cout << '\n' << std::endl; }
The element at offset index 3 is 12. int num[6] {20, 45, 56,12, 19, 34}; assert (num[3]==12);
To Find the number in that matrix and check that number adjacency elements... import java.util.Scanner; public class FindAdjacencyMatrix { public static int[][] array1 = new int[30][30]; public static int i,j,num,m,n; public static void main(String args[]) { Scanner input = new Scanner(System.in); //------------------------------------------------------------------------------------------------- System.out.println("Enter the m ,n matrix"); m = input.nextInt(); n = input.nextInt(); //------------------------------------------------------------------------------------------------- System.out.println("Enter the matrix Element one by one:"); for(i = 0; i < m; i++) { for(j = 0; j < n; j++) { array1[i][j] = input.nextInt(); } } System.out.println("The Given Matrix is :"); for(i = 0; i < m; i++) { for(j = 0; j < n; j++) { System.out.print(" "+array1[i][j]); } System.out.print("\n"); } //------------------------------------------------------------------------------------------------- System.out.println("Find The Adjacency Elements for Given Number : "); System.out.println("Enter The Number : "); num = input.nextInt(); for(i = 0; i < m; i++) { for(j = 0; j < n; j++) { if(num == array1[i][j]) { System.out.println("Element is Found :"+num); findAdjacency(num,i,j); break; } } } //-------------------------------------------------------------------------------------- } private static void findAdjacency(int elem,int row,int col) { try { if( array1[row][col-1]!=-1) { System.out.println("Left Adjacency : "+array1[row][col-1]); } } catch(Exception e){ System.out.println(" Exception Throwing "); } try{ if(array1[row][col+1]!= -1) { System.out.println("Right Adjacency : "+array1[row][col+1]); } }catch(Exception e){ System.out.println(" Exception Throwing "); } try { if(array1[row-1][col]!= -1) { System.out.println("Top Adjacency : "+array1[row-1][col]); } } catch(Exception e){ System.out.println(" Exception Throwing "); } try { if(array1[row+1][col]!= -1) { System.out.println("Botto Adjacency : "+array1[row+1][col]); } } catch(Exception e){ System.out.println(" Exception Throwing "); } } //---------------------------------------------------------------------------------------------- }
Dim x As String x = "HELLO" Dim tmpString As String tmpString = Nothing For j = 0 To x.Length - 1 Dim int int=x.Length - 1 int=int-j tmpString=x(j) Next
The code is below and i should also explain the algorithm. Well, What we are doing here is that we already defined the size to be 9x9 sudoku and are getting values using loops. All the empty spots are given UNASSIGNED value. Then we have functions to tell that if it is safe to put a value in the empty box by calculation and according to the rules of Sudoku it checks for is there any other some number horizontally and vertically and do the sum of the row and column is less than or equal to required or not. If the functions returns true then the program puts the value there.#include #define UNASSIGNED 0#define N 9bool FindUnassignedLocation(int grid[N][N], int &row, int &col);bool isSafe(int grid[N][N], int row, int col, int num);bool SolveSudoku(int grid[N][N]){int row, col;if (!FindUnassignedLocation(grid, row, col))return true; // success!for (int num = 1; num
#include<iostream> #include<vector> unsigned count_digits (unsigned num, const unsigned base=10) { unsigned count=1; while (num/=base) ++count; return count; } class number { std::vector<unsigned> value; unsigned base; public: number (const unsigned _value, const unsigned _base=10): value {}, base {_base} { *this = _value; } number& operator= (const unsigned _value); operator unsigned () const; bool is_narcissistic () const; }; number& number::operator= (unsigned _value) { unsigned count = count_digits (_value, base); value.resize (count); while (count) { value[value.size()-count--] = _value%base; _value/=base; } return *this; } number::operator unsigned () const { unsigned num = 0; for (unsigned index=0; index<value.size(); ++index) num += value[index]*static_cast<unsigned>(std::pow (base, index)); return num; } bool number::is_narcissistic () const { unsigned num = 0; for (unsigned index=0; index<value.size(); ++index) num += static_cast<unsigned>(std::pow (value[index], value.size())); return num == static_cast<unsigned> (*this); } unsigned main() { const unsigned min=1; const unsigned max=100; std::cout << "Narcissistic numbers in the range " << min << " through " << max << ":\n\t"; for (unsigned n=min; n<=max; ++n) if (number(n).is_narcissistic()) std::cout << n << ' '; std::cout << '\n' << std::endl; }
The element at offset index 3 is 12. int num[6] {20, 45, 56,12, 19, 34}; assert (num[3]==12);
unsigned binary_to_gray (unsigned num) { return num ^ (num >> 1); } unsigned gray_to_binary (unsigned num) { /* note: assumes num is no more than 32-bits in length */ num ^= (num >> 16); num ^= (num >> 8); num ^= (num >> 4); num ^= (num >> 2); num ^= (num >> 1); return num ; }
Access automatically creates an index for the primary key field in a table. In addition, Access automatically creates an index for any field name that contains the following letter sequences: code, ID, key, or num.
#include<iostream> #include<vector> #include<cassert> class magic_square { friend std::ostream& operator<< (std::ostream&, const magic_square&); private: size_t dim; std::vector<std::vector<size_t>> elem; public: magic_square (const size_t); magic_square (const magic_square& m): dim {m.dim}, elem {m.elem} {} magic_square (magic_square&& m): dim {m.dim}, elem {std::move (m.elem)} {} size_t size () const { return dim*dim; } size_t order () const { return dim; } size_t* operator[] (const size_t); #ifndef NDEBUG bool assert_valid(); #endif DEBUG }; magic_square::magic_square (const size_t order): dim {order}, elem {} { if (dim < 3) throw std::range_error ("Magic square's must have an order greater than 2"); elem.resize (dim); for (size_t row=0; row!=dim; ++row) elem[row].resize(dim); switch (dim % 4) { case (0): { size_t box = dim / 4; size_t lower = 1; size_t upper = dim * dim; for (size_t x=0; x!=dim; ++x) { size_t row = x+1; for (size_t y=0; y!=dim; ++y) { size_t col = y+1; if (((row<=box row>dim-box) && (col<=box col>dim-box)) ((row>box && row<=dim-box) && (col>box && col<=dim-box))) elem[x][y] = lower; else elem[x][y] = upper; ++lower; --upper; } } } break; case (1): case (3): { size_t num=0; size_t row=0; size_t col=(dim-1)/2; while (num<dim*dim) { elem[row][col] = ++num; size_t new_row = !row?dim:row-1; size_t new_col = col+1; if (new_row==dim) { if (new_col==dim) { new_col=col; --new_row; } else new_row=dim-1; } else if (new_col==dim) new_col=0; if (elem[new_row][new_col]!=0) { new_row=row+1; new_col=col; } row = new_row; col = new_col; } } break; case (2): { size_t num = 0; size_t quarter = dim / 2; magic_square temp (quarter); for (size_t loop=0; loop!=4; ++loop) { num = loop * quarter * quarter; switch (loop) { case (0): for (size_t row=0; row!=quarter; ++row) for (size_t col=0; col!=quarter; ++col) { elem[row][col] = temp.elem[row][col] + num; } break; case (1): for (size_t row=quarter; row!=dim; ++row) for (size_t col=quarter; col!=dim; ++col) { size_t x = row-quarter; size_t y = col-quarter; elem[row][col] = temp.elem[x][y] + num; } break; case (2): for (size_t row=0; row!=quarter; ++row) for (size_t col=quarter; col!=dim; ++col) { size_t x = row; size_t y = col-quarter; elem[row][col] = temp.elem[x][y] + num; } break; case (3): for (size_t row=quarter; row!=dim; ++row) for (size_t col=0; col!=quarter; ++col) { size_t x = row-quarter; size_t y = col; elem[row][col] = temp.elem[x][y] + num; } break; } } size_t div = (quarter-1) / 2; for (size_t row=0; row!=quarter; ++row) { size_t min_col=row==div?1:0; size_t max_col=row==div?div+1:div; for (size_t col=min_col; col!=max_col; ++col) std::swap (elem[row][col], elem[row+quarter][col]); } if (div=div-1) { for (size_t row=0; row!=quarter; ++row) { for (size_t col=dim-div; col!=dim; ++col) { std::swap (elem[row][col], elem[row+quarter][col]); } } } } break; } #ifndef NDEBUG assert_valid(); #endif NDEBUG } #ifndef NDEBUG bool magic_square::assert_valid() { size_t old_sum = 0; size_t sum = 0; for (size_t row=0; row!=dim; ++row) { sum = 0; for (size_t col=0; col!=dim; ++col) sum += elem[row][col]; if (!old_sum) old_sum = sum; assert (old_sum sum); return true; } #endif NDEBUG std::ostream& operator<< (std::ostream& os, const magic_square& s) { for (size_t x=0; x!=s.dim; ++x) { for (size_t y=0; y!=s.dim; ++y) { os << s.elem[x][y] << '\t'; } os << '\n'; } return os; } int main() { std::cout << "Magic square of order 3\n" << magic_square (3) << std::endl; std::cout << "Magic square of order 4\n" << magic_square (4) << std::endl; std::cout << "Magic square of order 6\n" << magic_square (6) << std::endl; }
#include<iostream> #include<vector> #include<time.h> std::vector<unsigned> factors (const unsigned num) { std::vector<unsigned> vec; if (num) { for (unsigned f=1; f<num/2; ++f) if (!(num%f)) vec.push_back (f); if (num>1) vec.push_back(num); } return (vec); } bool exists (unsigned num, std::vector<unsigned>& vec) { for (unsigned index=0; index<vec.size(); ++index) if( num==vec[index] ) return( true ); return( false ); } std::vector<unsigned> intersection (std::vector<unsigned>& vec1, std::vector<unsigned>& vec2) { std::vector<unsigned> intersects; for (unsigned index=0; index<vec1.size(); ++index) if (exists (vec1[index], vec2)) intersects.push_back( vec1[index] ); return (intersects); } int main() { srand ((unsigned) time (NULL)); // repeat 10 times... unsigned repeat=10; do{ // select two random numbers (0 to RAND_MAX) unsigned num1 = (unsigned) rand(); unsigned num2 = (unsigned) rand(); std::vector<unsigned> vec1 = factors (num1); std::vector<unsigned> vec2 = factors (num2); std::vector<unsigned> common = intersection (vec1, vec2); if (common.size()) std::cout<<"The GCF of "<<num1<<" and "<<num2<<" is "<<common.back()<<std::endl; else std::cout<<num1<<" and "<<num2<<" have no common factors"<<std::endl; }while(--repeat); } Example output: The GCF of 20754 and 19236 is 6 The GCF of 29182 and 10926 is 2 The GCF of 29587 and 4089 is 1 The GCF of 9652 and 29485 is 1 The GCF of 12406 and 21097 is 1 The GCF of 15959 and 10156 is 1 The GCF of 15619 and 16111 is 1 The GCF of 4486 and 30240 is 2 The GCF of 17121 and 32223 is 3 The GCF of 30290 and 10534 is 2 Press any key to continue . . .
num num num
To Find the number in that matrix and check that number adjacency elements... import java.util.Scanner; public class FindAdjacencyMatrix { public static int[][] array1 = new int[30][30]; public static int i,j,num,m,n; public static void main(String args[]) { Scanner input = new Scanner(System.in); //------------------------------------------------------------------------------------------------- System.out.println("Enter the m ,n matrix"); m = input.nextInt(); n = input.nextInt(); //------------------------------------------------------------------------------------------------- System.out.println("Enter the matrix Element one by one:"); for(i = 0; i < m; i++) { for(j = 0; j < n; j++) { array1[i][j] = input.nextInt(); } } System.out.println("The Given Matrix is :"); for(i = 0; i < m; i++) { for(j = 0; j < n; j++) { System.out.print(" "+array1[i][j]); } System.out.print("\n"); } //------------------------------------------------------------------------------------------------- System.out.println("Find The Adjacency Elements for Given Number : "); System.out.println("Enter The Number : "); num = input.nextInt(); for(i = 0; i < m; i++) { for(j = 0; j < n; j++) { if(num == array1[i][j]) { System.out.println("Element is Found :"+num); findAdjacency(num,i,j); break; } } } //-------------------------------------------------------------------------------------- } private static void findAdjacency(int elem,int row,int col) { try { if( array1[row][col-1]!=-1) { System.out.println("Left Adjacency : "+array1[row][col-1]); } } catch(Exception e){ System.out.println(" Exception Throwing "); } try{ if(array1[row][col+1]!= -1) { System.out.println("Right Adjacency : "+array1[row][col+1]); } }catch(Exception e){ System.out.println(" Exception Throwing "); } try { if(array1[row-1][col]!= -1) { System.out.println("Top Adjacency : "+array1[row-1][col]); } } catch(Exception e){ System.out.println(" Exception Throwing "); } try { if(array1[row+1][col]!= -1) { System.out.println("Botto Adjacency : "+array1[row+1][col]); } } catch(Exception e){ System.out.println(" Exception Throwing "); } } //---------------------------------------------------------------------------------------------- }
In simple Python code: def convertToAngle(num): while True: if num < 0: num = 360 - num elif num > 360: num -= 360 else: break return num
#include<iostream> #include<sstream> #include<exception> std::string decimal_to_roman (unsigned num) { std::stringstream ss {}; while (num>0) { if (num>10000) throw std::range_error ( "ERROR: decimal_to_roman (unsigned num) [num is out of range]"); else if (num==10000) { ss<<"[M]"; num-=10000; } else if (num>=9000) { ss<<"[CM]"; num-=9000; } else if (num>=5000) { ss<<"[D]"; num-=5000; } else if (num>=4000) { ss<<"[CD]"; num-=4000; } else if (num>=1000) { ss<<"M"; num-=1000; } else if (num>=900) { ss<<"CM"; num-=900; } else if (num>=500) { ss<<"D"; num-=500; } else if (num>=400) { ss<<"CD"; num-=400; } else if (num>=100) { ss<<"C"; num-=100; } else if (num>=90) { ss<<"XC"; num-=90; } else if (num>=50) { ss<<"L"; num-=50; } else if (num>=40) { ss<<"XL"; num-=40; } else if (num>=10) { ss<<"X"; num-=10; } else if (num==9) { ss<<"IX"; num-=9; } else if (num>=5) { ss<<"V"; num-=5; } else if (num==4) { ss<<"IV"; num-=4; } else if (num>=1) { ss<<"I"; num-=1; } } return ss.str(); } int main (void) { for (unsigned n=1; n<=10000; ++n) { try { std::cout << n << "\t = " << decimal_to_roman(n) << std::endl; } catch (std::range_error& e) { std::cerr<<e.what()<<std::endl; break; } } }
num 1 is predator num 2 is the beast num 3 is mimi num 4 is peachy and num 5 is centi