Software Development Training OJ Practice

Source: Internet
Author: User

Game 24-point gameplay algorithm

Question: Give 4 1-10 numbers, pass subtraction, get the number 24 even if victory
Input: 4 1-10 digits. [number allows repetition, test case guarantees no exception number]
Output: True or False

#include <iostream> #include <stdlib.h> #include <string.h> #include <stdio.h>using namespace Std;void cal (Double re[6], double A, double b) {Re[0] = a + b;re[1] = a-b;re[2] = b-a;re[3] = A * b;re[4] = A * 1.0/ B;RE[5] = b * 1.0/A;}  BOOL Is24points (double A, double b) {Double re[6];cal (re, A, b), int i;for (i = 0; i < 6; ++i) {if (re[i] = =) {return true;}} return false;}  void Calthree (double re[108], double A, double b, double c) {int I, j, k;k = 0;double fi[6];d ouble tt[6];cal (FI, A, b); for (i = 0; i < 6; ++i) {cal (TT, Fi[i], c); for (j = 0; j < 6; ++j) {re[k++] = Tt[j];}} Cal (FI, A, c); for (i = 0; i < 6; ++i) {cal (TT, Fi[i], b); for (j = 0; j < 6; ++j) {re[k++] = Tt[j];}} Cal (FI, B, c); for (i = 0; i < 6; ++i) {cal (TT, Fi[i], a); for (j = 0; j < 6; ++j) {re[k++] = Tt[j];}}  BOOL Game24points (int A, int b, int c, int d) {double Re[108];int i;calthree (Re, b, C, D); for (i = 0; i < 108; i++) {if (Is24points (Re[i], a)) return true; Calthree (Re, a, C, D), for (i = 0; i < 108; i++) {if (Is24points (Re[i], B)) return true; Calthree (Re, A, B, D), for (i = 0; i < 108; i++) {if (Is24points (Re[i], C)) return true;} Calthree (Re, A, B, C), for (i = 0; i < 108; i++) {if (Is24points (Re[i], D)) return true; return false;} int main () {cout << game24points (1, 2, 3, 4) << endl;cout << game24points (7, 2, 1, ten) << Endl;cou T << game24points (7, 7, 7, 7) << Endl;return 0;}
Periodic string problems

Problem: If a string can be repeated multiple times by a string of length k, we say that it has a period of K. For example, ABCABCABCABC has a period of 3 (note that it can also be 6 and 12 for a period, resulting in a minimum period of 3). The length of the string is less than or equal to 100 and is guaranteed by the caller.
Interface:int GetMinPeriod(char *inputstring)
Input:char * inputstring 字符串
Return:int 字符串最小周期

int Getminperiod (char *inputstring) {/* here implements the function */int I, J;int len = strlen (inputstring); int result = Len;for (i = 1; i < LEN/2 + 1; i++) {for (j = 0; J < Len-i; J + +) {if (inputstring[j]! = inputstring[j + i]) break; if (j = = len-i) {result = I;break;}} return result;}

Remove repeating characters

Problem: Given a string, all the characters in the string are deleted, the remaining characters are preserved, and the processed string is output. You need to guarantee the order in which characters appear and are case-sensitive.
Interface:int GetResult(const char *input, char *output)
Input:inputInput string Output:outputOutput string
Returns: 0 successes-1 failures and exceptions

#include <map> #include <iostream>using namespace Std;int getresult (const char *input, char *output) {if (Inpu t = = NULL | | Output = = NULL) Return-1;map<char, int> m;const char *p = input;while (*p) {M.insert (Map<char, Int>::value_typ E (*p, 0));p + +;} p = Input;char *p2 = Output;while (*p) {m[*p]++;if (m[*p] = = 1) {*p2++ = *p;} p++;} *P2 = ' + '; return 0;} int main () {char *p = "Aabbcdae"; char *ou = (char *) malloc (1000); GetResult (P, OU); cout << ous << Endl;}

N Queens

question: The Queen is the most powerful piece in chess. On the board shown below, the Queen can attack all the pieces that are located in the position where the arrows are covered. Can we put N queens on the Chessboard (NXN), and none of them can attack the rest of the Queen? Please write a program to find out a total of several methods.
Interface:intPlaceQueenMethodNum(int n);
Input:int nNumber of Queens returns: number of places to put the N Queens scheme

#include <stdio.h> #include <stdlib.h> #include <iostream>bool find (int row, int col, int *q) {int i = 1;w Hile (i < row) {if (q[i] = = Col | | abs (I-ROW) = = ABS (Q[i]-col)) return false;i++;} return true;} void place (int row, int n, int *q, int *re) {if (Row > N) {(*re) ++;return;} int col;for (col = 1; col <= N; col++) {if (Find (row, col, q)) {Q[row] = col;place (row + 1, N, q, RE);}}} int placequeenmethodnum (int n) {int re = 0;int q[20];p lace (1, N, Q, &re); return re;} int main () {std::cout<<placequeenmethodnum (8);}

Horrible factorial.

Problem: Calculating factorial n! is a terrible thing, because when n is not very large, n! will be a big value. For example 13! = 6227020800, has exceeded the range of values that we commonly use for the unsigned int type. Please design a program so that it can calculate the factorial of a number within 100, and the result is output in the form of a string
Interface:void CalcNN(int n, char *pOut)
Input:int nThe order multiplier to be entered
Output:char *pOutSettlement results, memory is managed by the caller

#include <iostream>using namespace std;void calcnn (int n, char *pout) {int A[5000];memset (a, 0, sizeof (a)); a[0] = 1;  int I, j, k, len = 0;for (i = 1; I <= n; ++i) {for (j = 0; J <= Len; ++j) a[j] *= i;for (j = 0; J <= Len; ++j) {if (A[j] <) Continue;k = J;while (k <= len) {if (A[len] > 9) ++len;a[k + 1] + = A[k]/10;a[k]%= 10;++k;}}} Char *p = pout;for (i = len; I >= 0; i) {*p++ = ' 0 ' + a[i];} *p = ' n '; return;} int main () {char *p = (char *) malloc (1000); CALCNN (p); cout << p;}

Software Development Training OJ Practice

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.