Programming little Practice OJ question 06

Source: Internet
Author: User

The simplification of one-element polynomial

For the input of the unary polynomial, the same kind of the merger, and in descending order of the index, the output of the processed unary polynomial. As input: "7x^4-5x^6+3x^3", Output: " -5x^6+7x^4+3x^3".

#include <map> #include <string> #include <stdlib.h> #include <iostream>using namespace std;    void Orderpolynomial (char* inputstring, char* outputstring) {map< int, int, greater<int> > m;    String input (inputstring);    if (input[0]! = '-') {input = "+" + input + "+";    } else {input = input + "+";    } unsigned int len = Input.length ();    bool Iscoeff = true;    BOOL Isminus = false;    int coefficient = 0, exponent = 0;            for (unsigned int i = 0; i < len; ++i) {if (input[i] = = '-') {Iscoeff = true;        Isminus = true;            } if (input[i] = = ' + ') {Iscoeff = true;        Isminus = false; } if (Input[i] >= ' 0 ' && input[i] <= ' 9 ') {if (Iscoeff) {coefficient = Coeff            Icient * + input[i]-' 0 ';            } else {exponent = exponent * + input[i]-' 0 ';         }} if (input[i] = = ' X ') {   Iscoeff = false;            if (isminus) {coefficient = coefficient *-1;                }} if ((input[i] = = ' + ' | | input[i] = = '-') && I! = 0) {if (M.count (exponent) = = 0) {            M[exponent] = coefficient;            } else {m[exponent] + = coefficient;            } coefficient = 0;        exponent = 0;    }} String output = "";    map< int, int, greater<int> >::iterator it;        for (it = M.begin (); It! = M.end (); ++it) {char cc[20], ee[20];        Itoa (It->second, CC, 10);        Itoa (It->first, EE, 10);        String C (cc);         String e (EE);        if (It->second > 0 && it = = M.begin ()) {output + = (c + "x^" + e);        } if (It->second > 0 && it! = M.begin ()) {output + = ("+" + C + "x^" + e);        } if (It->second < 0) {output + = (c + "x^" + e); }} output.copy(OutputString, Output.length (), 0); return;}

Binary tree Traversal (ordered by preface)

The preface and sequence of a binary tree are given.

#include <iostream> #include <string>using namespace std;typedef struct node{    char c;    struct Node *left;    struct Node *right;} Node, *pnode;static string pre;static string mid;void creattreebypremid (pnode &p, int i, int j, int len) {    if (len <= 0) return;    p = new Node;    P->c = Pre[i];    P->left = NULL;    P->right = NULL;    int m = mid.find_first_of (Pre[i]);    Creattreebypremid (P->left, i + 1, J, m-j);    Creattreebypremid (P->right, i + 1 + (m-j), M + 1, len-1-(M-J));} void Posttraverse (Pnode p) {    if (p) {        posttraverse (p->left);        Posttraverse (p->right);        cout << p->c;    }} int main () {    while (CIN >> pre >> mid) {        Pnode root;        Creattreebypremid (root, 0, 0, pre.length ());        Posttraverse (root);        cout << Endl;    }    return 0;}

Chorus (maximum increment sub-sequence)

A row of classmates N, after the team K, other students height meet T1 < T2 < ... < Ti, ti > Ti+1 > ... > TK (1 <= i <= K), ask for at least the number of teams to be out. Enter the first line to indicate the number of students N, the second row for each student's height.

#include <iostream>using namespace Std;int main () {int n, sum = 0;    CIN >> N;    int inc1[200], inc2[200], a[200];    for (int i = 1; I <= n; ++i) cin >> A[i];    INC1[1] = 1; Inc1[i] From left to A[i] longest ascending sub-sequence//State transfer recursion: inc1[i] = max{Inc1[j] |        (1 <= J < i) and A[i] > A[j]} + 1 for (int i = 2; I <= n; ++i) {inc1[i] = 1;  for (int j = 1; j < i; ++j) {if (A[i] > A[j] && inc1[j] + 1 > inc1[i]) inc1[i]        = Inc1[j] + 1;    }} Inc2[n] = 1;        for (int i = n-1; I >= 1; i.) {inc2[i] = 1;  for (int j = n; j > i;--j) {if (A[i] > A[j] && inc2[j] + 1 > inc2[i]) inc2[i]        = Inc2[j] + 1; }} for (int i = 1; I <= n; ++i) {if (Inc1[i] + inc2[i]-1 > sum) sum = Inc1[i] + inc2[i]    -1;    } cout << n-sum; return 0;}

Integer separated (the sum of the powers of 2)

An integer is split into the sum of the powers of 2, the number of species that can be split, the input n, and the output f (n)% 1000000000.

#include <iostream>using namespace std;//An integer can always be split into a power of 2 and, given an integer, how many methods of splitting int main () {    int *p = new int[10000 ];    int n;    while (CIN >> N) {        p[0] = p[1] = 1;        for (int i = 2; I <= n; ++i) {            if (i% 2)                p[i] = p[i-1];            else                p[i] = (P[i-1] + P[I/2])% 1000000000;        }        cout << P[n] << Endl;    }    Delete[] p;    return 0;}

Sum of large numbers

Input and output string representation of large number

#include <iostream> #include <string>using namespace std;void str2num (string str, int num[]) {for    ( unsigned int i = 0; I < str.length (); ++i) {        num[str.length ()-1-i] = str[i]-' 0 ';    }} Large number sum, number entered as a string not exceeding 100 bits int main () {    string A, B;    Cin >> a >> b;    int na[101] = {0};    int nb[100] = {0};    Str2Num (A, NA);    Str2Num (b, NB);    for (unsigned int i = 0; i <; ++i) {        na[i] + nb[i];        Na[i + 1] + = Na[i]/ten;        Na[i]%=;    }    int i = +;    for (; I >= 0; i) {        if (na[i]! = 0) {break            ;        }    }    for (; I >= 0; i.) {        cout << na[i];    }    return 0;}

The beauty of the name

The name is lowercase, its drift brightness is the sum of all its letters, the beauty of each letter range between 1 to 26, and are different, to maximize the degree of beauty.

#include <iostream> #include <string> #include <algorithm> #include <map> #include <vector > #include <functional>using namespace std;//name is the lowercase letter, with the beauty degree number [1,26] in the value of one by one corresponds to their own arrangement, to find the maximum beauty value of the name.    int main () {int n;    CIN >> N;        for (int i = 0; i < n; ++i) {string str;        CIN >> str;        Map<char, int> m;            for (unsigned int i = 0; i < str.size (); ++i) {if (M.count (str[i]) = = 0) {M[str[i]] = 1;            } else {++m[str[i]];        }} Map<char, int>::iterator it;        Vector<int> ve;        for (it = M.begin (); It! = M.end (); ++it) {ve.push_back (It->second);        } sort (Ve.begin (), Ve.end (), greater<int> ());        int sum = 0;        for (unsigned int i = 0, j = n; i < ve.size (); ++i,--j) {sum + = ve[i] * j;    } cout << sum << endl; } return 0;}

Count

There are n individuals in a circle, order automatic arranging. From the first person began to count (from 1 to 3 off), where 3 of the people quit, ask the last one left is the original number of the first.

#include <iostream> #include <vector>using namespace Std;int main () {    int n;    while (CIN >> N) {        vector<int> ve;        for (int i = 1; I <= n; ++i) {            ve.push_back (i);        }        int out = 0, count = 0, index = 0;        while (out < n-1) {            if (Ve[index]! = 0) {                ++count;            }            if (count = = 3) {                Ve[index] = 0;                Count = 0;                ++out;            }            ++index;            if (index = = N) {                index = 0;            }        }        for (unsigned int i = 0; i < ve.size (); ++i) {            if (ve[i]! = 0) {                cout << ve[i] << Endl;                Break    ;    }}} return 0;}

Programming little Practice OJ question 06

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.