軟體開發訓練 OJ 練習02

來源:互聯網
上載者:User

標籤:fsm   c++   

字串IP地址判斷

描述:判斷輸入的字串是不是一個有效IP地址 
介面:boolisIPAddressValid(constchar* pszIPAddr)
輸入:pszIPAddr 字串 
輸出:true 有效IP地址,false 無效的IP地址
約束:

  • 輸入IP為 XXX.XXX.XXX.XXX 格式
  • 字串兩端含有空格認為是合法IP
  • 字串中間含有空格認為是不合法IP
  • 類似於 01.1.1.1, 1.02.3.4 IP子段以0開頭為不合法IP
  • 子段為單個0 認為是合法IP,0.0.0.0 也算合法IP

代碼

#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 charSeqNum;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.currrent == '.') {if (st.previous == '.')return false;st.pointNum++;if (st.pointNum > 3)return false;num = a[i - st.charSeqNum] - '0';for (j = 1; j < st.charSeqNum; ++j) {num = num * 10 + 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 * 10 + 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;}

尋找兄弟單詞

實現一個可儲存若干個單詞的字典,實現以下功能:

  • 在字典中加入單詞,不能重複,單詞由小寫英文字母組成,不含其它字元;
  • 尋找指定單詞在字典中的兄弟單詞個數;
  • 尋找指定單詞的指定序號的兄弟單詞,指定序號指字典中兄弟單詞按字典順序, 排序後的序號從1開始;
  • 清空字典中所有單詞;

代碼

#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(); it != 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 = '\0';return -1;} else {ve[Seq - 1].copy(SimilarWord, 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] = {'\0'};    int Seq = 2;    int b = FindOneSimilarWord (Test_Word[1], Seq, SimilarWord);    cout << b << endl;    cout << SimilarWord;}

整形字串排序

給定字串內有很多正整數,要求對這些正整數進行排序,然後返回排序後指定位置的正整數 排序要求:按照每個正整數的後三位元字組成的整數進行從小到大排序。

  • 如果不足三位,則按照實際位元組成的整數進行比較
  • 如果相等,則按照輸入字串中的原始順序排序

說明(以下內容考生無須檢查,調用者保證):

  • 字串以’\0’結尾,僅包含數字、空格
  • 字串內正整數之間以單個空格分隔,字串首尾沒有空格
  • 正整數格式為十進位,大小:1~1000000,正整數的數字非零開始

樣本: 如字串內容 1223 22 3232 2016, 按照規定排序後 2016 22 1223 3232,查詢排序後的第3個數是 1223.

代碼

#include <iostream>#include <vector>#include <algorithm>#include <stdio.h>using namespace std;bool comp(const int &a, const int &b) {return a % 1000 < 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 = '\0';return -1;}vector<int> nums;int n = 0;const char *p = input_string;while (*p) {if (*p == ' ') {nums.push_back(n);n = 0;++p;continue;}n = n * 10 + *p - '0';++p;}nums.push_back(n);sort(nums.begin(), nums.end(), comp);if(serial_number > nums.size()){*output_string = '\0';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 = '\0';return -1;}sprintf(output_string, "%d", a);return 0;}int main() {const char *in_str = "1223 22 3232 2016";char out_str[5];find_string(in_str, 3, sizeof(out_str), out_str);cout << out_str;  // 1223}

在字串中找出最長連續的數字串

請一個在字串中找出連續最長的數字串,並把這個串的長度返回;如果存在長度相同的連續數字串,返回最後一個連續數字串; 注意:數字串只需要是數字組成的就可以,並不要求順序,比如數字串“1234”的長度就小於數字串“1359055”,如果沒有數字,則返回Null 字元串(“”)而不是NULL! 
範例輸入: abcd12345ed125ss123058789 
範例輸出: 輸出123058789,函數返回值 9 
函數介面: unsignedint Continumax(char** pOutputstr, char* intputstr)
輸入參數:char* intputstr 輸入字串; 
輸出參數:char** pOutputstr 連續最長的數字串,如果連續最長的數字串的長度為0,應該返回Null 字元串;如果輸入字串是空,也應該返回Null 字元串;
返回值:連續最長的數字串的長度

代碼

unsigned int Continumax(char** pOutputstr,  char* intputstr){if( intputstr == NULL ){*pOutputstr = (char*)malloc(2);**pOutputstr = '\0';return 0;}*pOutputstr = (char*)malloc(strlen(intputstr)+1);char *p = intputstr;unsigned int count = 0;unsigned int max = 0;char *pcur,*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 = '\0';} else {char *pt = *pOutputstr;unsigned int i = 0;for(; i < max; ++i){*pt++ = *pre++;}*pt = '\0';}return max;}

Fibonacci 數列的計算和轉換

求解擴充Fibanacci的第n項和前n項和 
輸入擴充Fibanacci數列的前2個數字和要求的數字序號 n,返回第n個數值 
輸入擴充Fibanacci數列的前2個數字和要求的數字序號 n,返回前n項之和

代碼

#include <iostream>using namespace std;int GetExtFibonacci(int first, int second, int num) {int re, i;if (num == 1)return 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;count += re;}return count;}int main() {cout<< GetExtFibonacci(1, 1, 5);cout<<"\n";cout<< CalcTotalValueOfExtFibonacci(1,1,5);}

軟體開發訓練 OJ 練習02

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.