华为OJ

1、字符串最后一个单词的长度

#include <iostream>
using namespace std;
const int mSize=130;
int Length_of_LastWord(char* str);
int main()
{
	char str[mSize];
	gets(str);
	int len=Length_of_LastWord(str);
	cout<<len<<endl;
	system("pause");
	return 0;
}
int Length_of_LastWord(char* str)
{
	if(!str)
		return 0;
	char temp[mSize];
	int len=strlen(str);
	for(int i=0;i<len;i++)
		temp[i]=str[len-1-i];
	int count=0;
	for(int i=0;i<len;i++)
	{
		if(temp[i]==' ')
			break;
		else
			count++;
	}
	return count;
}

  

2、公共字串计算

 1 #include <iostream>
 2 #include <string>
 3 const int mSize=20;
 4 using namespace std;
 5 int getCommonStrLength(char* s1, char* s2);
 6 int main()
 7 {
 8     char s1[mSize],s2[mSize];
 9     cin>>s1;
10     cin>>s2;
11     int maxCommon=getCommonStrLength(s1,s2);
12     cout<<maxCommon<<endl;
13     system("pause");
14     return 0;
15 }
16 int getCommonStrLength(char* s1, char* s2)
17 {
18     if(!s1&&!s2)
19         return -1;
20     if(!s1||!s2)
21         return 0;
22     int len1=strlen(s1);
23     int len2=strlen(s2);
24     char* p1,*p2;
25     if(len1>len2)
26     {
27         p1=s1;    p2=s2;
28     }
29     else
30     {
31         p1=s2;p2=s1;
32     }
33     int length[mSize];
34     for(int i=0;i<mSize;i++)
35         length[i]=0;
36     char* temp1,*temp2;
37     int i=0;
38     for(;*p2!='\0';p2++)
39     {
40         temp1=p1;
41         temp2=p2;
42         int count=0;
43         while(*temp1!='\0'&&*temp2!='\0')
44         {
45             if(*temp1==*temp2||*temp1==*temp2-32||*temp1==*temp2+32)
46             {
47                 temp1++;    temp2++;
48                 count++;
49             }
50             else
51             {
52                 temp1++;
53                 temp2=p2;
54             }
55         }
56         length[i]=count;
57         i++;
58     }
59     int max=length[0];
60     for(int i=1;i<mSize;i++)
61         if(length[i]>max)
62             max=length[i];
63     return max;
64 }

 

3、字符逆序

 

 1 #include <iostream>
 2 using namespace std;
 3 const int mSize=20;
 4 void Revert(char* s);
 5 int main()
 6 {
 7     char s[mSize];
 8     gets(s);
 9     Revert(s);
10     cout<<s<<endl;
11     system("pause");
12     return 0;
13 }
14 void Revert(char* s)
15 {
16     if(!s)
17         cout<<"Empty"<<endl;
18     char* p1,*p2;
19     p1=p2=s;
20     while(*p2!='\0')
21         p2++;
22     p2--;
23     while(p1<p2)
24     {
25         char temp;
26         temp=*p1;
27         *p1=*p2;
28         *p2=temp;
29         p1++;
30         p2--;
31     }
32 }

 

4、统计大写字母个数

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 int CalcCapital(string s);
 5 int main()
 6 {
 7     string s;
 8     getline(cin,s);
 9     int num=CalcCapital(s);
10     cout<<num<<endl;
11     system("pause");
12     return 0;
13 }
14 int CalcCapital(string s)
15 {
16     if(s.empty())
17         return 0;
18     int len=s.length();
19     int count=0;
20     for(int i=0;i<len;i++)
21     {
22         if(s[i]>='A'&&s[i]<='Z')
23             count++;
24     }
25     return count;
26 }

5、字符串反转

#include <iostream>
#include <string>
using namespace std;
void Revert(string &s);
int main()
{
    string s;
    getline(cin,s);
    Revert(s);
    cout<<s<<endl;
    system("pause");
    return 0;
}
void Revert(string &s)
{
    if(s.empty())
        cout<<"Empty"<<endl;
    char temp;
    int len=s.length();
    int i=0,j=len-1;
    while(i<j)
    {
        temp=s[i];
        s[i]=s[j];
        s[j]=temp;
        i++;
        j--;
    }
}

 

15、蛇形矩阵

 

#include <iostream>
using namespace std;
void GetResult(int n,int* result);
int main()
{
    int n;
    cin>>n;
    int mSize=(1+n)*n/2;
    int* result=new int[mSize];
    GetResult(n,result);
    int num=0;
    for(int i=0;i<n;i++)
    {
        int count=0;
        for(int j=0;j<n-i-1;j++)
        {
            cout<<result[j+num]<<" ";
            count++;
        }
        cout<<result[num+n-i-1]<<endl;
        count++;
        num+=count;
    }
    system("pause");
    return 0;
}
void GetResult(int n,int* result)
{
    int num=0;//count和num用于在一维数组中定位行和列
    for(int i=0;i<n;i++)
    {    
        int count=0;
        for(int j=0;j<n-i;j++)
        {
            if(j==0)//每行第一个元素的值
            {
                result[j+num]=(i+1)*i/2+1;
                count++;
            }    
            else//每行其他元素的值,由前一个元素得到
            {
                result[num+j]=result[num+j-1]+i+1+j;
                count++;
            }
            
        }        
        num+=count;
    }
}

 

 

16、字符串加密
#include <iostream>
using namespace std;
const int mSize=200;
void Delete(char* key);
bool Exist(char* s,char c);
int Position(char* al,char c);
void Encrypt(char* key,char* data,char* encrypt);
int main()
{
    char key[mSize];
    char data[mSize];
    cin>>key;
    cin.get();
    cin.getline(data,mSize);
    char* p=data;
    int lenData=strlen(p);
    char* encrypt=new char[lenData+1];
    Encrypt(key,data,encrypt);
    system("pause");
    delete []encrypt;
    return 0;
}
void Delete(char* key)
{
    int len=strlen(key);
    for(int i=0;i<len;i++)
    {
        for(int j=i+1;j<len;j++)
        {
            if(key[i]==key[j])
            {
                for(int k=j;k<len;k++)
                    key[k]=key[k+1];
            }
        }
    }
}
bool Exist(char* s,char c)
{
    char* p=s;
    while(*p!='\0')
    {
        if(*p==c)
            return true;
        p++;
    }
    return false;
}
int Position(char* al,char c)
{
    char* p=al;
    int index=0;
    while(*p!=c)
    {
        p++;
        index++;
    }
    return index;
}
void Encrypt(char* key,char* data,char* encrypt)
{
    Delete(key);//删除重复元素
    int lenKey=strlen(key);
    char alphabit[30]="abcdefghijklmnopqrstuvwxyz";
    char Alphabit[30]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    //定义一个新的字母表,根据key和题目的定义进行创建,同样为26个字符
    char* newalphabit=new char[30];
    char* newAlphabit=new char[30];
    for(int i=0;i<lenKey;i++)
    {
        if(key[i]>='A'&&key[i]<='Z')
            newAlphabit[i]=key[i];        
        else
            newAlphabit[i]=key[i]-32;    
    }
    char p='A';
    for(int i=lenKey;i<26;i++)
    {
        while(Exist(key,p)||Exist(key,p+32))
        {
            p++;
        }
        newAlphabit[i]=p;
        p++;
    }
    newAlphabit[26]='\0';
    for(int i=0;i<26;i++)
        newalphabit[i]=newAlphabit[i]+32;
    newalphabit[26]='\0';
    int lenData=strlen(data);
    for(int i=0;i<lenData;i++)
    {
        if(data[i]>='a'&&data[i]<='z')
        {
            int pos=Position(alphabit,data[i]);
            encrypt[i]=newalphabit[pos];
        }
        else if(data[i]>='A'&&data[i]<='Z')
        {
            int pos=Position(Alphabit,data[i]);
            encrypt[i]=newAlphabit[pos];
        }
        else
            encrypt[i]=data[i];
    }
    encrypt[lenData]='\0';
    cout<<encrypt<<endl;
    delete []newAlphabit;
    delete []newalphabit;
}

 

17、判断两个IP是否属于同一子网

 

#include <iostream>
#include <string>
using namespace std;
int checkNetSegment(string mask, string ip1, string ip2);
int main()
{
    string mask,ip1,ip2;
    cin>>mask>>ip1>>ip2;
    int i=checkNetSegment(mask,ip1,ip2);
    cout<<i<<endl;
    system("pause");
    return 0;
}

int checkNetSegment(string mask, string ip1, string ip2)
{
    //字符串中提取每一段的数值
    int M[4],IP1[4],IP2[4];
    int j1=0,j2=0,j3=0;
    int temp1,temp2,temp3;
    for(int i=0;i<4;i++)
    {
        if(i<3)
        {
            temp1=0;temp2=0;temp3=0;
            while(mask[j1]!='.')
                temp1=temp1*10+mask[j1++]-'0';
            j1++;
            while(ip1[j2]!='.')
                temp2=temp2*10+ip1[j2++]-'0';
            j2++;
            while(ip2[j3]!='.')
                temp3=temp3*10+ip2[j3++]-'0';
            j3++;
            M[i]=temp1;
            IP1[i]=temp2;
            IP2[i]=temp3;
        }
        else
        {
            int len1=mask.length(),len2=ip1.length(),len3=ip2.length();
            temp1=0;temp2=0;temp3=0;
            while(j1<len1)
                temp1=temp1*10+mask[j1++]-'0';
            while(j2<len2)
                temp2=temp2*10+ip1[j2++]-'0';
            while(j3<len3)
                temp3=temp3*10+ip2[j3++]-'0';
            M[i]=temp1;
            IP1[i]=temp2;
            IP2[i]=temp3;
        }
    }
    //检查合法性
    bool fm[4]={false},f1[4]={false},f2[4]={false},f;
    for(int i=0;i<4;i++)
    {
        if(M[i]>=0&&M[i]<=255)
            fm[i]=true;
        if(IP1[i]>=0&&IP1[i]<=255)
            f1[i]=true;
        if(IP2[i]>=0&&IP2[i]<=255)
            f2[i]=true;
    }
    f=fm[0]&&fm[1]&&fm[2]&&fm[3]&&f1[0]&&f1[1]&&f1[2]&&f1[3]&&f2[0]&&f2[1]&&f2[2]&&f2[3];
    if(f==false)
        return 1;
    //进行AND运算,检查两个IP是否属于同一个网络
    int result1[4],result2[4];
    for(int i=0;i<4;i++)
    {
        result1[i]=M[i]&IP1[i];
        result2[i]=M[i]&IP2[i];
    }
    f=true;
    for(int i=0;i<4;i++)
    {
        if(result1[i]!=result2[i])
            f=false;
    }
    if(f)
        return 0;
    else
        return 2;
}

 

18、输入一行字符,分别统计出包含英文字母、空格、数字和其它字符的个数

 

#include <iostream>
#include <string>
using namespace std;
int getEnglishCharCount(string str);
int getBlankCharCount(string str);
int getNumberCharCount(string str);
int getOtherCharCount(string str);
int main()
{
    string str;
    getline(cin,str);
    int numChar=getEnglishCharCount(str);
    int numBlank=getBlankCharCount(str);
    int numNum=getNumberCharCount(str);
    int numOther=getOtherCharCount(str);
/*    cout<<numChar<<" "<<numBlank<<" "<<numNum<<" "<<numOther<<endl;*/
    cout<<numChar<<endl;
    cout<<numBlank<<endl;
    cout<<numNum<<endl;
    cout<<numOther<<endl;
    system("pause");
    return 0;
}
int getEnglishCharCount(string str)
{
    int len=str.length();
    int count=0;
    for(int i=0;i<len;i++)
    {
        if((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z'))
            count++;
    }
    return count;
}

int getBlankCharCount(string str)
{
    int len=str.length();
    int count=0;
    for(int i=0;i<len;i++)
    {
        if(str[i]==' ')
            count++;
    }
    return count;
}
int getNumberCharCount(string str)
{
    int len=str.length();
    int count=0;
    for(int i=0;i<len;i++)
    {
        if(str[i]>='0'&&str[i]<='9')
            count++;
    }
    return count;
}

int getOtherCharCount(string str)
{
    int len=str.length();
    int count=0;
    for(int i=0;i<len;i++)
    {
        if(!((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z'))&&str[i]!=' '&&!(str[i]>='0'&&str[i]<='9'))
            count++;
    }
    return count;
}

 

 

 

19、称砝码

 

 

22、学英语

 

#include <iostream>
using namespace std;
void parse(long int n);
int main()
{
    int n;
    cin>>n;
    if(n<0)
        cout<<"error"<<endl;
    if(n==0)
        cout<<"zero"<<endl;
    parse(n);
    system("pause");
    return 0;
}
void parse(long int n)
{
    int num[20]={0};
    int i=0,len=0;
    while(n)
    {
        num[i]=n%10;
        n/=10;
        len++;
        i++;
    }
    i=9;
    while(num[i]==0&&i>=0)
        i--;
    for(int j=len-1;j>=0;j--)
    {
        if(j==9)
        {
            if(num[j]==1)
                cout<<"one billion";
            if(num[j]==2)
                cout<<"two billion";
            if(num[j]==3)
                cout<<"three billion";
            if(num[j]==4)
                cout<<"four billion";
            if(num[j]==5)
                cout<<"five billion";
            if(num[j]==6)
                cout<<"six billion";
            if(num[j]==7)
                cout<<"seven billion";
            if(num[j]==8)
                cout<<"eight billion";
            if(num[j]==9)
                cout<<"nine billion";
        }
        if(j==8)
        {
            if(len>9&&num[j]!=0)
                cout<<" ";
            if(num[j]==1)
                cout<<"one hundred million";
            if(num[j]==2)
                cout<<"two hundred million";
            if(num[j]==3)
                cout<<"three hundred million";
            if(num[j]==4)
                cout<<"four hundred million";
            if(num[j]==5)
                cout<<"five hundred million";
            if(num[j]==6)
                cout<<"six hundred million";
            if(num[j]==7)
                cout<<"seven hundred million";
            if(num[j]==8)
                cout<<"eight hundred million";
            if(num[j]==9)
                cout<<"nine hundred million";
        }
        if(j==7)
        {
            if(len>8&&num[j]!=0)
                cout<<" ";
            if(num[j]==1)
                cout<<"ten million";
            if(num[j]==2)
                cout<<"twenty million";
            if(num[j]==3)
                cout<<"thirty million";
            if(num[j]==4)
                cout<<"forty million";
            if(num[j]==5)
                cout<<"fifty million";
            if(num[j]==6)
                cout<<"sixty million";
            if(num[j]==7)
                cout<<"seventy million";
            if(num[j]==8)
                cout<<"eighty million";
            if(num[j]==9)
                cout<<"ninety million";
        }
        if(j==6)
        {
            if(len>7&&num[j]!=0)
                cout<<" ";
            if(num[j]==1)
                cout<<"one million";
            if(num[j]==2)
                cout<<"two million";
            if(num[j]==3)
                cout<<"three million";
            if(num[j]==4)
                cout<<"four million";
            if(num[j]==5)
                cout<<"five million ";
            if(num[j]==6)
                cout<<"six million";
            if(num[j]==7)
                cout<<"seven million";
            if(num[j]==8)
                cout<<"eight million";
            if(num[j]==9)
                cout<<"nine million";
        }
        if(j==5)
        {
            if(len>6&&num[j]!=0)
                cout<<" ";
            if(num[j]==1)
                cout<<"one hundred thousand";
            if(num[j]==2)
                cout<<"two hundred thousand";
            if(num[j]==3)
                cout<<"three hundred thousand";
            if(num[j]==4)
                cout<<"four hundred thousand";
            if(num[j]==5)
                cout<<"five hundred thousand";
            if(num[j]==6)
                cout<<"six hundred thousand";
            if(num[j]==7)
                cout<<"seven hundred thousand";
            if(num[j]==8)
                cout<<"eight hundred thousand";
            if(num[j]==9)
                cout<<"nine hundred thousand";
        }
        if(j==4)
        {
            if(len>5&&num[j]!=0)
                cout<<" ";
            if(num[j]==1)
                cout<<"ten thousand";
            if(num[j]==2)
                cout<<"twenty thousand";
            if(num[j]==3)
                cout<<"thirty thousand";
            if(num[j]==4)
                cout<<"forty thousand";
            if(num[j]==5)
                cout<<"fifty thousand";
            if(num[j]==6)
                cout<<"sixty thousand";
            if(num[j]==7)
                cout<<"seventy thousand";
            if(num[j]==8)
                cout<<"eighty thousand";
            if(num[j]==9)
                cout<<"ninety thousand";
        }
        if(j==3)
        {
            if(len>4&&num[j]!=0)
                cout<<" ";
            if(num[j]==1)
                cout<<"one thousand";
            if(num[j]==2)
                cout<<"two thousand";
            if(num[j]==3)
                cout<<"three thousand";
            if(num[j]==4)
                cout<<"four thousand";
            if(num[j]==5)
                cout<<"five thousand";
            if(num[j]==6)
                cout<<"six thousand";
            if(num[j]==7)
                cout<<"seven thousand";
            if(num[j]==8)
                cout<<"eight thousand";
            if(num[j]==9)
                cout<<"nine thousand";
        }
        if(j==2)
        {
            if(len>3&&num[j]!=0)
                cout<<" ";
            if(num[j]==1)
                cout<<"one hundred";
            if(num[j]==2)
                cout<<"two hundred";
            if(num[j]==3)
                cout<<"three hundred";
            if(num[j]==4)
                cout<<"four hundred";
            if(num[j]==5)
                cout<<"five hundred";
            if(num[j]==6)
                cout<<"six hundred";
            if(num[j]==7)
                cout<<"seven hundred";
            if(num[j]==8)
                cout<<"eight hundred";
            if(num[j]==9)
                cout<<"nine hundred";
        }
        if(j==1)
        {
            if(len>2&&num[j]!=0)
                cout<<" and ";
            if(num[j]==2)
                cout<<"twenty";
            if(num[j]==3)
                cout<<"thirty";
            if(num[j]==4)
                cout<<"forty";
            if(num[j]==5)
                cout<<"fifty";
            if(num[j]==6)
                cout<<"sixty";
            if(num[j]==7)
                cout<<"seventy";
            if(num[j]==8)
                cout<<"eighty";
            if(num[j]==9)
                cout<<"ninety";
            if(num[j]==1)
            {
                if(num[0]==0)
                    cout<<"ten";
                if(num[0]==1)
                    cout<<"eleven";
                if(num[0]==2)
                    cout<<"twelve";
                if(num[0]==3)
                    cout<<"thirteen";
                if(num[0]==4)
                    cout<<"fourteen";
                if(num[0]==5)
                    cout<<"fifteen";
                if(num[0]==6)
                    cout<<"sixteen";
                if(num[0]==7)
                    cout<<"seventeen";
                if(num[0]==8)
                    cout<<"eighteen";
                if(num[0]==9)
                    cout<<"nineteen";
                break;
            }
        }
        if(j==0)
        {
            if(len>1)
            {
                if(num[1]==0&&num[j]!=0)
                    cout<<" and ";
                if(num[1]!=0&&num[j]!=0)
                    cout<<" ";
            }
            if(len==1&&num[j]==0)
                cout<<"zero";
            if(num[j]==1)
                cout<<"one";
            if(num[j]==2)
                cout<<"two";
            if(num[j]==3)
                cout<<"three";
            if(num[j]==4)
                cout<<"four";
            if(num[j]==5)
                cout<<"five";
            if(num[j]==6)
                cout<<"six";
            if(num[j]==7)
                cout<<"seven";
            if(num[j]==8)
                cout<<"eight";
            if(num[j]==9)
                cout<<"nine";
        }
    }
    cout<<endl;
}
View Code

 

 

23、输入n个整数,输出其中最小的k个

 

#include <iostream>
#include <algorithm>
using namespace std;
const int mSize=100;
bool GetMinK(int n, int* In, int k, int* Out);
int main()
{
    int n,k,val;
    cin>>n>>k;
    int* In=new int[n];
    int* Out=new int[k];
    for(int i=0;i<n;i++)
    {
        cin>>val;
        In[i]=val;
    }
    bool f=GetMinK(n,In,k,Out);
    if(f)
    {
        for(int i=0;i<k-1;i++)
            cout<<Out[i]<<" ";
        cout<<Out[k-1]<<endl;
    }
    delete []In;
    delete []Out;
    system("pause");
    return 0;
}
bool GetMinK(int n, int* In, int k, int* Out)
{
    if(!In||k<=0)
        return false;
    sort(In,In+n);
    for(int i=0;i<k;i++)
        Out[i]=In[i];
    return true;
}

 

 

 

 

24、查找组成一个偶数最接近的两个素数

 

#include <iostream>
#include <algorithm>
using namespace std;
class PrimePair
{
public:
    int Min;
    int Max;
};
bool IfPrime(int n);
PrimePair findPrimeNumber(int number);
int main()
{
    int number;
    cin>>number;
    PrimePair p=findPrimeNumber(number);
    cout<<p.Min<<endl;
    cout<<p.Max<<endl;
    system("pause");
    return 0;
}
bool IfPrime(int n)
{
    for(int i=2;i<n;i++)
    {
        if(n%i==0)
            return false;
    }
    return true;
}
PrimePair findPrimeNumber(int number)
{
    PrimePair pair;
    int min=number/2,max=number/2;
    while(IfPrime(min)==false||IfPrime(max)==false||min+max!=number)
    {
        while(!IfPrime(min))
            min--;
        while(!IfPrime(max))
            max++;
        if(max+min==number)
            break;
        else
        {
            if(max+min>number)
                min--;
            if(max+min<number)
                max++;
        }
    }
    pair.Max=max;
    pair.Min=min;
    return pair;
}

 

 

 

25、放苹果

#include <iostream>
using namespace std;
int Count(int m,int n);
int main()
{
    int m,n;
    cin>>m>>n;
    int num=Count(m,n);
    cout<<num<<endl;
    system("pause");
    return 0;
}
int Count(int m,int n)
{
    if(m==0||n==1)
        return 1;
    if(m<n)
        return Count(m,m);
    else
        return Count(m-n,n)+Count(m,n-1);
}

 

 

27、求int型数据在内存中存储时1的个数

 

#include <iostream>
using namespace std;
int Count(unsigned int n);
int main()
{
    int n;
    cin>>n;
    int num=Count(n);
    cout<<num<<endl;
    system("pause");
    return 0;
}
int Count(unsigned int n)
{
    int num=0;
    while(n)
    {
        if(n%2==1)
            num++;
        n=n>>1;
    }
    return num;
}

 

28、句子逆序

 

#include <iostream>
#include <string>
using namespace std;
void RevertAll(string::iterator iter1,string::iterator iter2);
void Revert(string &s);
int main()
{
    string s;
    getline(cin,s);
    Revert(s);
    cout<<s;
    system("pause");
    return 0;
}
void Revert(string &s)
{
    if(s.empty())
        return;
    int len=s.length();
    RevertAll(s.begin(),s.end()-1);
    string::iterator iter1,iter2;
    iter1=s.begin();
    iter2=s.begin();
    while(iter2!=s.end())
    {
        if(*iter2!=' ')
            iter2++;
        else
        {
            RevertAll(iter1,iter2-1);
            iter2++;
            iter1=iter2;
        }        
    }
    RevertAll(iter1,iter2-1);
}
void RevertAll(string::iterator iter1,string::iterator iter2)
{
    char temp;
    while(iter1<iter2)
    {
        temp=*iter1;
        *iter1=*iter2;
        *iter2=temp;
        iter1++;
        iter2--;
    }
}

 

 

 

29、字符个数统计

#include <iostream>
#include <algorithm>
using namespace std;
const int mSize=100;
int num_char(char* s);
int main()
{
    char s[mSize];
    cin.getline(s,mSize);
    int num=num_char(s);
    cout<<num<<endl;
    system("pause");
    return 0;
}
int num_char(char* s)
{
    int len=strlen(s);
    sort(s,s+len);
    int num=0;
    char* p=s;
    while(*p!='\0')
    {
        if(*(p+1)!=*p)
            num++;
        p++;
    }
    return num;
}

 

30、提取不重复的整数
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
bool Single(int val,vector<int> v);
int Result(int n);
int main()
{
    int n;
    cin>>n;
    int result=Result(n);
    cout<<result<<endl;
    system("pause");
    return 0;
}
bool Single(int val,vector<int> v)
{
    vector<int>::iterator iter;
    for(iter=v.begin();iter!=v.end();iter++)
    {
        if(val==*iter)
            return false;
    }
    return true;
}
int Result(int n)
{
    int re=0;
    vector<int> result;
    int val;
    while(n)
    {
        val=n%10;
        if(val!=0&&Single(val,result))
            result.push_back(val);
        n/=10;
    }
    int len=result.size();
    vector<int>::iterator iter;
    for(iter=result.begin();iter!=result.end();iter++)
    {
        re+=(*iter)*pow(10.0,len-1);
        len--;
    }
    return re;
}

 


31、合并表记录


#include <iostream>
#include <map>
using namespace std;
int main()
{
    int n,key,val;
    cin>>n;
    map<int,int> m;
    for(int i=0;i<n;i++)
    {
        cin>>key>>val;
        if(m.find(key)==m.end())
            m[key]=val;
        else
            m[key]+=val;
    }
    map<int,int>::iterator iter;
    for(iter=m.begin();iter!=m.end();iter++)
        cout<<iter->first<<endl<<iter->second<<endl;
    system("pause");
    return 0;
}
 

 

32、取近似值


#include <iostream>
using namespace std;
int Switch(double a);
int main()
{
    double a;
    cin>>a;
    int re=Switch(a);
    cout<<re<<endl;
    system("pause");
    return 0;
}
int Switch(double a)
{
    int b=a*10;
    int temp=b%10;
    if(temp>=5)
    {
        if(a>0)
            a=int(a+1);
        else
            a=int(a-1);
    }
    else
    {
        if(a>0)
            a=int(a);
        else
            a=int(a-1);
    }
    return a;
}

 32、质数因子


#include <iostream>
#include <vector>
using namespace std;
bool IfPrime(int i);
void Result(long int number);
int main()
{
    int number;
    cin>>number;
    Result(number);
    system("pause");
    return 0;
}
bool IfPrime(int i)
{
    for(int j=2;j<i;j++)
        if(i%j==0)
            return false;
    return true;
}
void Result(long int number)
{
    vector<int> v;
    while(number>1)
    {
        for(int i=2;i<=number;i++)
        {
            if(number%i==0)
            {
                number/=i;
                v.push_back(i);
                break;
            }
        }
    }
    for(vector<int>::iterator iter=v.begin();iter!=v.end()-1;iter++)
    {
        if(IfPrime(*iter))
            cout<<*iter<<" ";
    }
    if(IfPrime(*(v.end()-1)))
        cout<<*(v.end()-1)<<endl;
}


34、进制转换


#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const int mSize=100;
void Change(char* In,char* Out);
int main()
{
    char In[mSize],Out[mSize];
    cin>>In;
    Change(In,Out);
    char* p=Out;
    while(*p!='\0')
    {
        cout<<*p;
        p++;
    }
    cout<<endl;
    system("pause");
    return 0;
}
void Change(char* In,char* Out)
{
    long long number=0;
    int len=strlen(In);
    
    int i=0;
    for(i=len-1;i>=0;i--)
    {
        if(In[i]>='0'&&In[i]<='9')
            number+=int(In[i]-'0')*pow(16.0,len-i-1);
        if(In[i]>='A'&&In[i]<='F')
            number+=int(In[i]-'0'-7)*pow(16.0,len-i-1);
    }
    i=0;
    while(number)
    {
        int temp=number%10;
        Out[i]=char(temp+'0');
        number/=10;
        i++;
    }
    Out[i]='\0';
    i--;
    char t;
    int j=0;
    while(j<i)
    {
        t=Out[j];
        Out[j]=Out[i];
        Out[i]=t;
        j++;
        i--;
    }
}

 


35、明明的随机数


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int mSize=100;
int Result(int* num,int n);
int main()
{
    int n;
    cin>>n;
    int num[mSize];
    for(int i=0;i<n;i++)
        cin>>num[i];
    int restNum=Result(num,n);
    for(int i=0;i<restNum;i++)
        cout<<num[i]<<endl;
    system("pause");
    return 0;
}
int Result(int* num,int n)
{
    sort(num,num+n);
    for(int i=0;i<n-1;i++)
    {
        if(num[i]==num[i+1])
        {
            for(int j=i+1;j<n-1;j++)
            {
                num[j]=num[j+1];
            }
            n--;
        }
    }
    return n;
}

36、求最小公倍数


#include <iostream>
using namespace std;
int Least(int a,int b);
int main()
{
    int a,b;
    cin>>a>>b;
    int least=Least(a,b);
    cout<<least<<endl;
    system("pause");
    return 0;
}
int Least(int a,int b)
{
    int p=a>b?a:b;
    while(p%a!=0||p%b!=0)
    {
        p++;
    }
    return p;
}

37、求解立方根


#include <iostream>
#include <iomanip>
using namespace std;
const int E=0.0001;
double getCubeRoot(double n);
int main()
{
    double n;
    cin>>n;
    double x=getCubeRoot(n);
    cout.setf(ios::fixed);
    cout<<setprecision(1)<<x<<endl;
    system("pause");
    return 0;
}
double getCubeRoot(double n)
{
    double x0=n,x1;
    x1=2*x0/3+(n/(3*x0*x0));
    while((x1-x0>E)||(x1-x0<-E))
    {
        x0=x1;
        x1=2*x0/3+(n/(3*x0*x0));
    }
    return x1;
}

38、字符统计


#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
const int mSize=100;
void Count(char* s);
int main()
{
    char s[mSize];
    gets(s);
    Count(s);
    system("pause");
    return 0;
}
void Count(char* s)
{
    map<char,int> m;
    char* p=s;
    while(*p!='\0')
    {
        if((*p>='A'&&*p<='Z')||(*p>='a'&&*p<='z')||*p==' '||(*p>='0'&&*p<='9'))
        {
            if(m.find(*p)==m.end())
                m[*p]=1;
            else
                m[*p]++;
        }
        p++;
    }
    map<char,int>::iterator iter;
    vector<char> vc;
    int len=m.size();
    for(int i=0;i<len;i++)
    {
        iter=m.begin();
        int maxV=iter->second;
        char maxK=iter->first;
        for(iter=m.begin();iter!=m.end();iter++)
        {
            if(iter->second>maxV)
            {
                maxV=iter->second;
                maxK=iter->first;
            }
        }
        vc.push_back(maxK);
        m.erase(maxK);
    }
    vector<char>::iterator it;
    for(it=vc.begin();it!=vc.end();it++)
        cout<<*it;
    cout<<endl;
}

 


44、记票统计

 
#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;
const int mSize=100;
void Vote(map<string,int> &candidate);
int main()
{
    map<string,int> candidate;
    Vote(candidate);
    system("pause");
    return 0;
}
void Vote(map<string,int> &candidate)
{
    int numC,numV;
    vector<string> temp;
    cin>>numC;
    cin.get();
    string name;
    for(int i=0;i<numC;i++)
    {
        getline(cin,name);
        if(candidate.find(name)==candidate.end())
        {
            candidate[name]=0;
            temp.push_back(name);
        }
    }
    cin>>numV;
    cin.get();
    int invalid=0;
    for(int i=0;i<numV;i++)
    {
        getline(cin,name);
        if(candidate.find(name)!=candidate.end())
            candidate[name]++;
        else
            invalid++;
    }
    vector<string>::iterator iter1;
    for(iter1=temp.begin();iter1!=temp.end();iter1++)
    {
        cout<<*iter1<<" : "<<candidate[*iter1]<<endl;
    }
    cout<<"Invalid : "<<invalid<<endl;
}

45、字符串通配符


#include <iostream>
using namespace std;
const int mSize=100;
bool Match(char* s1,char* s2);
int main()
{
    char s1[mSize],s2[mSize];
    cin>>s1>>s2;
    bool f=Match(s1,s2);
    if(f)
        cout<<"true"<<endl;
    else
        cout<<"false"<<endl;
    system("pause");
    return 0;
}
bool Match(char* s1,char* s2)
{
    bool f=false;
    char* p1=s1,*p2=s2;
    while(*p1!='\0'&&*p2!='\0')
    {
        if(*p1!=*p2&&*p1!=*p2+32&&*p1!=*p2-32)
        {
            if(*p1=='?')
            {
                p1++;
                p2++;
            }
            else if(*p1=='*')
            {
                if(*(p1+1)!='\0')
                {
                    p1++;
                    while(*p1!=*p2&&p2!='\0')
                    {
                        p2++;
                    }
                }
                else
                {
                    p1++;
                    f=true;
                }
            }
            else
                return f;
        }
        else
        {
            p1++;
            p2++;
        }
    }
    if(*p1=='\0'&&*p2=='\0')
        f=true;
    return f;
}

 

47、求最大连续bit数


#include <iostream>
using namespace std;
const int mSize=200;
int maxLenOfOne(int number);
int main()
{
    int number;
    cin>>number;
    int result=maxLenOfOne(number);
    cout<<result<<endl;
    system("pause");
    return 0;
}
int maxLenOfOne(int number)
{
    int bit[8];
    int i=0,j;
    int t;
    while(number)
    {
        t=number%2;
        number/=2;
        bit[i]=t;
        i++;
    }
    while(i<8)
        bit[i++]=0;
    i=0;
    j=7;
    while(i<j)
    {
        t=bit[i];
        bit[i]=bit[j];
        bit[j]=t;
        i++;
        j--;
    }
    int maxCount=0;
    for(i=0;i<8;i++)
    {
        int count=0;
        if(bit[i]==1)
        {
            j=i;
            while(bit[j]==1&&j<8)
            {
                count++;
                j++;
            }
        }
        if(count>maxCount)
            maxCount=count;
    }
    return maxCount;
}

 


48、超长正整数相加


#include <iostream>
#include <string>
using namespace std;
string AddLongInteger(string s1,string s2);
int main()
{
    string s1,s2;
    cin>>s1>>s2;
    string s=AddLongInteger(s1,s2);
    cout<<s<<endl;
    system("pause");
    return 0;
}
string AddLongInteger(string s1,string s2)
{
    int jw=0;
    int i=s1.size();
    int j=s2.size();
    int k;
    k=i>j?i:j;

    string s3;
    s3.resize(k);
    i--;
    j--;
    k--;
    while(i>=0&&j>=0)
    {
        int sum=s1[i]+s2[j]+jw-'0'-'0';
        if(sum>=10)
        {
            sum=sum%10;
            jw=1;
            s3[k]=sum+'0';
        }
        else
        {
            s3[k]=sum+'0';
            jw=0;
        }
        i--;
        j--;
        k--;
    }
    while(i>=0)
    {
        int sum=s1[i]+jw-'0';
        if(sum>=10)
        {
            sum=sum%10;
            jw=1;
            s3[k]=sum+'0';
        }
        else
        {
            s3[k]=sum+'0';
            jw=0;
        }
        i--;
        k--;
    }
    while(j>=0)
    {
        int sum=s2[j]+jw-'0';
        if(sum>=10)
        {
            sum=sum%10;
            jw=1;
            s3[k]=sum+'0';
        }
        else
        {
            s3[k]=sum+'0';
            jw=0;
        }
        j--;
        k--;
    }
    if(jw==1)
    {
        s3='1'+s3;
    }
    return s3;
}

49、计算字符串的相似度


#include <iostream>
#include <string>
#include <vector>
using namespace std;
string calculateStringDistance(string s1, string s2);
int MinOfThree(int a,int b,int c);
int main()
{
    string s1,s2;
    cin>>s1>>s2;
    string s=calculateStringDistance(s1,s2);
    cout<<s<<endl;
    system("pause");
    return 0;
}
int MinOfThree(int a,int b,int c)
{
    int min=a;
    if(b<min)
        min=b;
    if(c<min)
        min=c;
    return min;
}
string calculateStringDistance(string s1, string s2)
{
    int len1=s1.length(),len2=s2.length();
    int **a;
    a=new int *[len1];
    for(int i=0;i<len1;i++)
        a[i]=new int[len2];
    for(int i=0;i<len1;i++)
        a[i][0]=i;
    for(int j=0;j<len2;j++)
        a[0][j]=j;
    for(int i=1;i<len1;i++)
    {
        for(int j=1;j<len2;j++)
        {
            if(s1[i]==s2[j])
                a[i][j]=a[i-1][j-1];
            else
                a[i][j]=MinOfThree(a[i-1][j-1],a[i-1][j],a[i][j-1])+1;
        }
    }
    int minC=a[len1-1][len2-1]+1;
    vector<char> vc;
    while(minC)
    {
        char t=minC%10+'0';
        vc.push_back(t);
        minC/=10;
    }
    string s="1";
    s+="/";
    int i=vc.size()-1;
    while(i>=0)
    {
        s+=vc[i];
        i--;
    }
    delete a;
    return s;
}

 


52、合法IP


#include <iostream>
#include <vector>
using namespace std;
const int mSize=20;
#define M 2
bool IsLigel(char* s);
int main()
{
    char s[mSize];
    cin>>s;
    bool f=IsLigel(s);
    if(f)
        cout<<"YES"<<endl;
    else
        cout<<"NO"<<endl;
    system("pause");
    return 0;
}
bool IsLigel(char* s)
{
    char* p=s;
    vector<int> ip;
    int numPoint=0;
    while(*p!='\0')
    {
        int n=0;
        while(*p!='.'&&*p!='\0')
        {
            if(*p<'0'||*p>'9')
                return false;
            n=n*10+*p-'0';
            p++;
        }
        if(*p=='.')
            numPoint++;
        if(*p=='.'&&*(p+1)=='.')
            return false;
        ip.push_back(n);
        if(*p=='\0')
            break;
        else
            p++;
    }
    if(numPoint!=3||ip.size()!=4)
        return false;
    vector<int>::iterator iter;
    for(iter=ip.begin();iter!=ip.end();iter++)
    {
        if(*iter<0||*iter>255)
            return false;
    }
    return true;
}

 


53、密码强度等级

#include <iostream>
#include <vector>
#include <string>
using namespace std;
int Length(string s);
int Alpha(string s);
int Number(string s);
int Char(string s);
int Award(string s);
int Final(string s);
int main()
{
    string s;
    cin>>s;
    int score=Final(s);
    if(score>=90)
        cout<<"VERY_SECURE"<<endl;
    if(score>=80&&score<90)
        cout<<"SECURE"<<endl;
    if(score>=70&&score<80)
        cout<<"VERY_STRONG"<<endl;
    if(score>=60&&score<70)
        cout<<"STRONG"<<endl;
    if(score>=50&&score<60)
        cout<<"AVERAGE"<<endl;
    if(score>=25&&score<50)
        cout<<"WEAK"<<endl;
    if(score>=0&&score<25)
        cout<<"VERY_WEAK"<<endl;
    system("pause");
    return 0;
}
int Length(string s)
{
    int len=s.length();
    if(len<=4)
        return 5;
    if(len>=5&&len<=7)
        return 10;
    if(len>=8)
        return 25;
}
int Alpha(string s)
{
    int numBig=0,numSmall=0;
    string::iterator iter;
    for(iter=s.begin();iter!=s.end();iter++)
    {
        if(*iter>='a'&&*iter<='z')
            numSmall++;
        if(*iter>='A'&&*iter<='Z')
            numBig++;
    }
    if(numBig==0&&numSmall==0)
        return 0;
    if((numBig>0&&numSmall==0)||(numBig==0&&numSmall>0))
        return 10;
    if(numBig>0&&numSmall>0)
        return 20;
}
int Number(string s)
{
    int num=0;
    string::iterator iter;
    for(iter=s.begin();iter!=s.end();iter++)
    {
        if(*iter>='0'&&*iter<='9')
            num++;
    }
    if(num==0)
        return 0;
    if(num==1)
        return 10;
    if(num>1)
        return 20;
}
int Char(string s)
{
    int num=0;
    string::iterator iter;
    for(iter=s.begin();iter!=s.end();iter++)
    {
        if((*iter>=33&&*iter<=47)||(*iter>=58&&*iter<=64)||(*iter>=91&&*iter<=96)||(*iter>=123&&*iter<=126))
            num++;
    }
    if(num==0)
        return 0;
    if(num==1)
        return 10;
    if(num>1)
        return 25;
}
int Award(string s)
{
    int scoreA,scoreN,scoreC;
    scoreA=Alpha(s);
    scoreN=Number(s);
    scoreC=Char(s);
    if(scoreA==10&&scoreN>0&&scoreC==0)
        return 2;
    if(scoreA==10&&scoreN>0&&scoreC>0)
        return 3;
    if(scoreA==20&&scoreN>0&&scoreC>0)
        return 5;
}
int Final(string s)
{
    int re;
    int s1,s2,s3,s4,s5;
    s1=Length(s);
    s2=Alpha(s);
    s3=Number(s);
    s4=Char(s);
    s5=Award(s);
    re=s1+s2+s3+s4+s5;
    return re;
}

 

 

 

posted on 2015-08-10 14:42  VisualTracker  阅读(610)  评论(0编辑  收藏  举报