描述:判断输入的字符串是不是一个有效的IP地址
接口:boolisIPAddressValid(constchar* pszIPAddr)
输入:pszIPAddr 字符串
输出:true 有效的IP地址,false 无效的IP地址
约束:
XXX.XXX.XXX.XXX 格式01.1.1.1, 1.02.3.4 IP子段以0开头为不合法IP0.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;
}
实现一个可存储若干个单词的字典,实现以下功能:
代码
#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;
else
return -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;
}
给定字符串内有很多正整数,要求对这些正整数进行排序,然后返回排序后指定位置的正整数 排序要求:按照每个正整数的后三位数字组成的整数进行从小到大排序。
说明(以下内容考生无须检查,调用者保证):
示例: 如字符串内容 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!
样例输入: abcd12345ed125ss123058789
样例输出: 输出123058789,函数返回值 9
函数接口: unsignedint Continumax(char** pOutputstr, char* intputstr)
输入参数:char* intputstr 输入字符串;
输出参数:char** pOutputstr 连续最长的数字串,如果连续最长的数字串的长度为0,应该返回空字符串;如果输入字符串是空,也应该返回空字符串;
返回值:连续最长的数字串的长度
代码
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;
}
求解扩展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);
}
原文:http://blog.csdn.net/thisinnocence/article/details/41790835