
算法练习
文章平均质量分 58
JamesDoctor
这个作者很懒,什么都没留下…
展开
-
Programming Challenges ( 挑战编程 ) 部分习题解答索引
逐步更新中。。。第1章110101 The 3n+1 problem 解答110102 Minesweeper 解答110104 LC-Display 解答110105 Gr原创 2013-08-30 14:44:40 · 1427 阅读 · 0 评论 -
110602 How Many Pieces of Land
// Max = 1 + C(n, 2) + C(n, 4)// Refer to http://en.wikipedia.org/wiki/Dividing_a_circle_into_areas or // http://www.arbelos.co.uk/Papers/Chords-regions.pdf #include #include #include using原创 2013-11-18 13:25:15 · 697 阅读 · 0 评论 -
110508 Pairsumonious Numbers
#include #include #include #include using namespace std;static int FindNumber(const vector& src, int target, int start, int end){ if (end < start) return -1; int mid = (start + end) / 2;原创 2013-10-29 14:48:18 · 610 阅读 · 0 评论 -
110507 The Stern-Brocot Number System
#include using namespace std;class SternBrocotNo{public: SternBrocotNo(int x, int y) : m_x(x), m_y(y) {} SternBrocotNo(const SternBrocotNo& other) : m_x(other.m_x), m_y(other.m_y) {} bool o原创 2013-10-28 13:05:36 · 486 阅读 · 0 评论 -
110506 Polynomial coefficients
#include using namespace std;static int C(int n, int k){ if (k == 0) return 1; int a = 1, b = 1; for (int i = 1; i <= k; ++i) { b *= i; a *= n; --n; } return a / b;}static int原创 2013-10-28 11:33:17 · 645 阅读 · 0 评论 -
110505 A multiplication game
This solution gets "Wrong answer" result inhttp://www.programming-challenges.com/,but it gets "Accepted" result inhttp://uva.onlinejudge.org/.I don't know why so far.#include #include原创 2013-10-25 17:43:10 · 616 阅读 · 0 评论 -
110504 Ones
#include #include using namespace std;static int s_Multiplier[] = {0, 1, 0, 7, 0, 0, 0, 3, 0, 9};static void Multiply(int src, int multiplier, int& result, int& flag){ result = src * multipli原创 2013-10-24 13:30:15 · 593 阅读 · 0 评论 -
110503 The Archeologists' Dilemma
// Suppose the given number is X, then// X*(10^Y) Y*log10 + logX < N < Y*log10 + log(X+1)// Another limitation is: Y > int(lgX) + 1#include #include using namespace std;static int SmallestIn原创 2013-10-24 11:04:38 · 637 阅读 · 0 评论 -
110502 Reverse and Add
#include #include #include #include #include using namespace std;static bool IsReverse(const char* num, int len){ int mid = len / 2; for (int i = 0; i < mid; ++i) { if (num[i] != num[len原创 2013-10-18 13:59:45 · 787 阅读 · 0 评论 -
110501 Primary Arithmetic
#include #include using namespace std;#define BUF_SIZE 1024static bool Add(const char& ch1, const char& ch2, bool currentFlag){ int num1 = ch1 - '0'; int num2 = ch2 - '0'; int flag = curren原创 2013-10-18 12:29:08 · 805 阅读 · 0 评论 -
110408 Football (aka Soccer)
This solution gets "Wrong answer" result inhttp://www.programming-challenges.com/,but it gets "Accepted" result inhttp://uva.onlinejudge.org/.I don't know why so far.#include #include原创 2013-10-17 12:16:26 · 638 阅读 · 0 评论 -
110407 ShellSort
This solution gets "Wrong answer" result inhttp://www.programming-challenges.com/,but it gets "Accepted" result inhttp://uva.onlinejudge.org/.According to the explanation in http://www.program原创 2013-10-16 13:22:51 · 603 阅读 · 0 评论 -
110902 Playing with Wheels
#include #include #include #include using namespace std;static void GetDigitsFromNumber(int number, int& thousands, int& hundreds, int& tens, int& ones){ ones = number % 10; number /= 10; t原创 2013-11-20 15:43:37 · 913 阅读 · 0 评论 -
110903 The Tourist Guide
#include #include #include #include using namespace std;class TouristGuid{public: TouristGuid(int cities, int roads) { int city1, city2, capacity; m_cities.resize(cities); for (int i原创 2013-11-20 15:45:19 · 857 阅读 · 0 评论 -
110901 Bicoloring
#include #include #include using namespace std;class Graph{public: Graph(int vertexCnt) { m_vertexCnt = vertexCnt; int edges; cin >> edges; m_relations = new bool*[m_vertexCnt];原创 2013-11-20 15:42:09 · 785 阅读 · 0 评论 -
110801 Little Bishops
// Refer to http://blog.youkuaiyun.com/metaphysis/article/details/6553011 to see the design of following program.import java.util.*;public class Main { private static void ClearArray(long[][] data, int原创 2013-12-18 15:39:30 · 854 阅读 · 0 评论 -
110706 Smith Numbers
import java.util.*;import static java.lang.Math.*;public class Main { private static List s_Primes; static { s_Primes = new LinkedList(); s_Primes.add(2L); long limit = (long)sqrt(200000原创 2013-12-05 14:43:25 · 698 阅读 · 0 评论 -
110703 Euclid problem
import java.util.*;public class Main { private static void EuclidAlgorithm(int a, int b) { if (a < b) { EuclidAlgorithm(b, a); int temp = ResultX; ResultX = ResultY; ResultY = te原创 2013-12-05 17:56:49 · 774 阅读 · 0 评论 -
110705 Summation of Four Primes
import java.util.*;import static java.lang.Math.*;public class Main { private static Map s_PrimesInfo; static { s_PrimesInfo = new HashMap(); } private static boolean IsPrime(int x) { B原创 2013-12-04 14:40:43 · 600 阅读 · 0 评论 -
110704 Factovisors
import java.util.*;import static java.lang.Math.*;public class Main { private static boolean IsPrime(int x) { int mid = (int)(sqrt(x)); for (int i = 2; i <= mid; ++i) if ((x % i) == 0)原创 2013-12-04 13:13:33 · 638 阅读 · 0 评论 -
110707 Marbles
import java.util.*;public class Main { private static long GetGCD(long a, long b) { if (a == b) return a; if (a < b) return GetGCD(b, a); while(true) { long temp = b; b =原创 2013-12-10 16:24:42 · 729 阅读 · 0 评论 -
110702 Carmichael Numbers
import java.util.*;import static java.lang.Math.*;public class Main { private static boolean IsPrime(long number) { long mid = (long)(sqrt((double)number)); for (long i = 2; i <= mid; ++i)原创 2013-12-02 14:05:48 · 566 阅读 · 0 评论 -
110701 Light, more light
//import java.io.*;import java.util.*;import static java.lang.Math.*;class Main implements Runnable{ /* static String ReadLn(int maxLength){ // utility function to read from stdin,原创 2013-12-02 13:55:11 · 753 阅读 · 0 评论 -
110601 How many Fibs
#include #include #include using namespace std;class Integer{public: Integer(const char* buf, int len) { Init(buf, len); } Integer() {} void Init(const char* buf, int len) { for (原创 2013-11-15 16:40:29 · 666 阅读 · 0 评论 -
110604 Expressions
#include #include #include #include #include #include using namespace std;static const int STEP = 4;static const int UNIT = pow(10, STEP);class Integer{public: Integer(char* buf, int le原创 2013-11-26 16:54:34 · 682 阅读 · 0 评论 -
110603 Counting
#include #include using namespace std;class Integer{public: Integer(char* buf, int len) { for (int i = 0; i < len; ++i) m_data.push_back(buf[i] - '0'); } Integer() {} void Print()原创 2013-11-25 12:36:19 · 713 阅读 · 0 评论 -
110307 Doublets
This solution gets "Wrong answer" result inwww.programming-challenges.com/,but it gets "Accepted" result inhttp://uva.onlinejudge.org/.According to the explanation in http://www.programm原创 2013-09-23 17:23:22 · 615 阅读 · 0 评论 -
110405 Shoemaker's Problem
#include using namespace std;class ShoeMaker{public: void Init(int index, int days, int punishment) { m_index = index; m_days = days; m_punishment = punishment; } class Comparer { p原创 2013-10-15 12:06:47 · 539 阅读 · 0 评论 -
110206 Erdös Numbers ( Erdos Numbers )
#include #include #include #include #include #include #include using namespace std;#define ERDOS "Erdos, P."struct AuthorInfo{public: AuthorInfo(const string& authorName) : m_Name(autho原创 2013-08-30 14:57:08 · 726 阅读 · 0 评论 -
110203 Hartals
#include #include #include using namespace std;void SetStrikeDays(char* pDays, int nDays, int interval){ for(int i = interval; i <= nDays; i += interval) { int day = i % 7; if ((day != 6)原创 2013-08-30 14:50:41 · 503 阅读 · 0 评论 -
110107 Check The Check
#include #include using namespace std;#define DIM_SIZE (8 + 2 + 2)#define LINE_SIZE 8#define DELTA ((DIM_SIZE - LINE_SIZE) / 2)enum ChessType_t{ BLACK = 0, WHITE};class Chess{public:原创 2013-08-30 14:44:02 · 701 阅读 · 0 评论 -
110104 LC-Display
#include #include #include #include #include #include using namespace std;typedef unsigned int SIZE_t;typedef char DATA_t;static char s_Elems[] = {' ', '-', '|'};static bool s_Properties[原创 2013-08-30 14:34:28 · 726 阅读 · 0 评论 -
110102 MineSweeper
#include #include #include #include using namespace std;typedef unsigned int Dim_t;class MineMatrix{public: MineMatrix(Dim_t rows, Dim_t cols) : m_rows(rows + 2), m_cols(cols + 2), m_Mi原创 2013-08-30 14:30:01 · 949 阅读 · 0 评论 -
110205 Stack 'em Up ( Stack em Up )
#include #include #include #include #include using namespace std;#define POKER_CNT 52#define LINE_BUF_CNT 6static char s_Line[LINE_BUF_CNT];void ShuffleOnPokers(int* target, int* shuffleMe原创 2013-08-30 14:55:10 · 856 阅读 · 0 评论 -
110204 Crypt Kicker
#include #include #include #include #include #include using namespace std;#define MAX_WORD_LEN 16#define MAX_BUF_SIZE 4096typedef vector DictStrCol_t;typedef vector LineStrCol_t;typedef ma原创 2013-08-30 14:52:47 · 1773 阅读 · 0 评论 -
110202 Poker Hands
#include #include #include #include using namespace std;enum Color_t{ RED_HEART, BLACK_HEART, SQUARE, GRASS};enum RankType_t{ MAX = 0, PAIR, DOUBLE_PAIR, TRIPLE, SEQ, SAME_COLOR,原创 2013-08-30 14:49:35 · 938 阅读 · 0 评论 -
110201 Jolly Jumpers
#include using namespace std;inline int abs(int prev, int next){ int sub = next - prev; if (sub < 0) return -sub; return sub;}inline void OutputResult(bool success){ if (success) { c原创 2013-08-30 14:47:30 · 546 阅读 · 0 评论 -
110106 Interpreter
#include #include #include #include using namespace std;#define BIT_CNT 3#define REG_CNT 10#define MEM_CNT 1000#define ADD(x, y) (((x) + (y)) % 1000)#define MULT(x, y) (((x) * (y)) % 1000)原创 2013-08-30 14:40:28 · 674 阅读 · 0 评论 -
110105 Graphical Editor
#include #include #include #include using namespace std;#define MAX_DIM 250#define INIT_COLOR 'O'typedef int Dim_t;typedef char Color_t;struct Graph{ Graph() : Row(0), Col(0) {原创 2013-08-30 14:37:45 · 841 阅读 · 0 评论 -
110207 Contest Scoreboard
#include #include #include #include #include #include using namespace std;#define PUNISH_TIME 20class Team{public: Team(int no) : m_No (no), m_solvedProblems(0), m_totalTime(0) { } ~原创 2013-08-30 15:00:17 · 756 阅读 · 0 评论