Software Development Training OJ Exercise 02

Source: Internet
Author: User

String IP address judgment

Description: Determines whether the input string is a valid IP address
Interface:boolisIPAddressValid(constchar* pszIPAddr)
Input:pszIPAddrString
Output:trueA valid IP address,falseInvalid IP address
Constraints:

  • Input IP for XXX.XXX.XXX.XXX format
  • The string ends with a space that is considered a legitimate IP
  • The middle of the string contains spaces that are considered illegal IPs
  • Similarly 01.1.1.1 , the 1.02.3.4 IP sub-segment starts with 0 as an illegal IP
  • A single 0 is considered a legitimate IP, 0.0.0.0 also a legitimate IP

Code

#include <iostream> #include <ctype.h>using namespace Std;bool isipaddressvalid (const char* pszipaddr) {if (  Pszipaddr = = NULL) return false;const char* a = Pszipaddr;int begin, End, Len;len = strlen (PSZIPADDR); for (begin = 0; begin < Len; ++begin) {if (A[begin]! = ") break;} for (end = len-1; end >= 0;--end) {if (A[end]! = ") {break;}} if (Begin >= End | |!isdigit (a[begin]) | |!isdigit (a[end]) return false;struct State {char Currrent;char previous;int c Harseqnum;int Pointnum;} St = {0, 0, 0, 0};int I, J, num;for (i = begin; I <= end; ++i) {st.previous = St.currrent;st.currrent = A[i];if (st.c Urrrent = = '. ') {if (st.previous = = '. ') Return False;st.pointnum++;if (St.pointnum > 3) return false;num = a[i-st.charseqnum]-' 0 '; for (j = 1; J < St.char SeqNum; ++J) {num = num * + a[i-st.charseqnum + j]-' 0 ';} if (num > 255) {return false;} St.charseqnum = 0;} else if (IsDigit (st.currrent)) {st.charseqnum++;if (st.previous = = ' 0 ' && st.charseqnum = = 2) {return false;} if (St.charseqnum > 3) {return false;} if (i = = end) {num = a[i + 1-st.charseqnum]-' 0 '; for (j = 1; j < St.charseqnum; ++j) {num = num * + a[i + 1-st. Charseqnum + j]-' 0 ';} if (num > 255) {return false;}}} else {return false;}} if (st.pointnum! = 3) return False;return true;} int main () {const char* a = "110.1.210.1"; bool B = Isipaddressvalid (a); cout << b;}

find brother Words

Implement a dictionary that stores several words to achieve the following features:

    • Add words in the dictionary, cannot repeat, the words are composed of lowercase English letters, without other characters;
    • Finds the number of sibling words in the dictionary for the specified word;
    • Finds the sibling word for the specified ordinal of the specified word, specifying the ordinal number of the sibling word in the dictionary in Dictionary order, starting at 1 for the sorted sequence number;
    • Clears all the words in the dictionary;

Code

#include <set> #include <string> #include <vector> #include <algorithm> #include <iostream >using namespace std;set<string> dict;int Addoneword (char* Word) {String a = Word;if (Dict.insert (a). Second) return 0;elsereturn-1;} BOOL Isbro (String A, string b) {sort (A.begin (), A.end ()), Sort (B.begin (), B.end ()), return a = = B;} int Findsimilarwordnum (char* Word) {String a = Word;set<string>::iterator it;int count = 0;for (it = Dict.begin (); I T! = Dict.end (); ++it) {if (a = *it && Isbro (A, *it)) ++count;} return count;} int Findonesimilarword (char* Word, int Seq, char* similarword) {String a = Word;vector<string> ve;set<string> :: Iterator it;for (it = Dict.begin (); It! = Dict.end (); ++it) {if (A! = *it && Isbro (A, *it)) {ve.push_back (*it);} }if (ve.size () = = 0 | | Seq > Ve.size ()) {*similarword = ' similarword '; return-1;} else {ve[seq-1].copy (ve[seq-1].length, (), 0); return 0 ;}} void Clearallwords (void) {dict.clear ();} int main (){char *test_word[7] = {"Mock", "AABC", "abc", "Ckom", "BCAA", "ABCA",}; Addoneword (Test_word[0]); Addoneword (test_word[1]); Addoneword (test_word[2]); Addoneword (Test_word[3]); Addoneword (Test_word[4]);    Addoneword (test_word[5]); int a = Findsimilarwordnum (Test_word[0]); cout << a << Endl;    Char *expectword = {"BCAA"};    Char similarword[51] = {'} '};    int Seq = 2;    int b = Findonesimilarword (test_word[1], Seq, Similarword);    cout << b << Endl; cout << Similarword;}

Shaping string Sorting

There are a number of positive integers within a given string that require the ordering of these positive integers, and then return the ordering of the positive integers at the specified position after the sort: from small to large, an integer consisting of the next three digits of each positive integer.

    • If it is less than three bits, it is compared by an integer consisting of the actual number of digits
    • If they are equal, sort in the original order in the input string

Instructions (The following candidates do not need to check, caller guarantee):

    • string ends with ' \ n ' and contains only numbers, spaces
    • The positive integers within a string are separated by a single space, and there is no space at the end of the string

Example: the 1223 22 3232 2016 2016 22 1223 3232 3rd number after the query is sorted, such as the string content, after sorting by the rules 1223 .

Code

#include <iostream> #include <vector> #include <algorithm> #include <stdio.h>using namespace Std;bool Comp (const int &a, const int &b) {return a% < b% 1000;} int find_string (const char* input_string, int serial_number,int output_string_max_length, char* output_string) {if ( Input_string = = 0 | | !*input_string) {*output_string = ' + '; return-1;} vector<int> nums;int n = 0;const char *p = input_string;while (*p) {if (*p = = ') {nums.push_back (n); n = 0;++p;cont Inue;} n = n * + *p-' 0 '; ++p;} Nums.push_back (n); sort (Nums.begin (), Nums.end (), comp), if (Serial_number > Nums.size ()) {*output_string = ' + '; return-1;} int a = Nums[serial_number-1];int k = 0, tt = a;while (tt! = 0) {++k;tt/= 10;} if (Output_string_max_length <= k) {*output_string = ' + '; return-1;} sprintf (output_string, "%d", a); return 0;}  int main () {const char *IN_STR = "1223 3232"; Char out_str[5];find_string (IN_STR, 3, sizeof (OUT_STR), out_str); cout  << Out_str; 1223} 

Find the longest consecutive string of numbers in strings

Please find the longest consecutive number string in the string and return the length of the string, and if there is a continuous number string of the same length, the last consecutive number string is returned; Note: The number string only needs to be a number of the can, do not require order, such as the number string "1234" length is less than the number string " 1359055 ", if there is no number, returns an empty string (" ") instead of null!
Sample input: abcd12345ed125ss123058789
Sample output: Output 123058789, function return value 9
Function Interface:unsignedint Continumax(char** pOutputstr, char* intputstr)
Input parameters:char* intputstrInput string;
Output parameters:char** pOutputstrThe longest consecutive number string, if the length of the longest consecutive number string is 0, an empty string should be returned, and an empty string should be returned if the input string is empty;
Return value: The length of the longest consecutive number string

Code

unsigned int continumax (char** poutputstr,  char* intputstr) {if (intputstr = = NULL) {*poutputstr = (char*) malloc (2); * *poutputstr = ' + '; return 0;} *poutputstr = (char*) malloc (strlen (INTPUTSTR) +1); char *p = intputstr;unsigned int count = 0;unsigned int max = 0;char *PCU R,*pre;pcur = P;pre = P;while (*p) {if (IsDigit (*p)) {pcur = P;while (IsDigit (*p)) {++count;++p;}} if (Count >= max) {max = Count;pre = Pcur;} Count = 0;++p;} if (max = = 0) {**poutputstr = ' + ';} else {char *pt = *poutputstr;unsigned int i = 0;for (; i < Max; ++i) {*pt++ = *pre++; }*pt = ' + ';} return Max;}

Calculation and conversion of Fibonacci series

solves the nth and the first n of the extended Fibanacci and
Enter the first 2 digits of the extended Fibanacci sequence and the required number ordinal n to return the nth value
Enter the first 2 digits of the extended Fibanacci sequence and the required number ordinal n to return the sum of the first n items

Code

#include <iostream>using namespace Std;int getextfibonacci (int first, int second, int num) {int RE, i;if (num = = 1) r Eturn first;if (num = = 2) return second;for (i = 3; I <= num; ++i) {re = first + Second;first = Second;second = re;} return re;} int Calctotalvalueofextfibonacci (int first, int second, int num) {int RE, i;if (num = = 1) return first;if (num = 2) return First + second;int count = first + second;for (i = 3; I <= num; ++i) {re = first + Second;first = Second;second = Re;co Unt + = Re;} return count;} int main () {cout<< Getextfibonacci (1, 1, 5);cout<< "\ n";cout<< Calctotalvalueofextfibonacci (1,1,5);}

Software Development Training OJ Exercise 02

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.