zoukankan      html  css  js  c++  java
  • 华为机试题七

    /*   合唱队
    #include <stdio.h>  
    int main()  
    {  
    	int n;  
    	int i,j;  
    	int a[100];  
    	int f1[100];//存放合唱队的人数(从左向右)  
    	int f2[100];//存放合唱队的人数(从右向左)  
    	int ans = 0;  
    	
    	//cin>>n;  
    	scanf("%d",&n);  
    	for(i=1;i<=n;i++)//第0个位置不存放数据,符合平常的思维习惯。  
            //cin>>a[i];  
            scanf("%d",&a[i]);  
    	
    	for(i=1;i<=n;i++)//由左向右依次遍历   
    	{   
            f1[i] = 1;//至少有一个人符合条件,就是他自己。所以赋初值1.  
            for(j=1;j<i;j++)   
            {  
    			if(a[i]>a[j]&&f1[i]<f1[j]+1) //f1[i]<f1[j]+1很关键的条件,动态问题  
    				f1[i]= f1[j]+1;  
            }          
    	}  
    	
    	for(i=n;i>=1;i--)//由右向左依次遍历  
    	{  
            f2[i] = 1;//至少有一个人符合条件,就是他自己。所以赋初值1.      
            for (j=i+1;j<=n;j++)   
            {         
    			if(a[i]>a[j]&&f2[i]<f2[j]+1)          
    				f2[i]=f2[j]+1;    
            }  
    	}  
    	
    	for(i=1;i<=n;i++)  
            if(ans<f1[i]+f2[i]-1)   
    			ans=f1[i]+f2[i]-1;//最佳解  
    		//cout<<n-ans<<endl;//需要出列的人数  
            printf("%d",n-ans);  
    		return 0;  
    } 
    */
    /*    坐标移动
    #include <iostream>
    using namespace std;
    void main()
    {
    	int x=0,y=0;
    	int i,j,k;
    	char a[100],b[30][5];
    	j=0;
    	k=0;
        gets(a);
    	for (i=0;i<strlen(a);i++)
    	{
            if (a[i] != ';')
            {
               b[j][k] = a[i];
    		   k++;
            } 
            else
            {
    			b[j][k]='';
    			j++;
    			k=0;
            }
    	}
        for (i=0;i<j;i++)
        {
    		k=strlen(b[i]);
    		if (k==2)
    		{
               if (b[i][0]=='A'&& (b[i][1]>='0'&&b[i][1]<='9'))
               {
                   x = x-(b[i][1]-'0');
    
               }
    		   if (b[i][0]=='D'&& (b[i][1]>='0'&&b[i][1]<='9'))
               {
    			   x = x+(b[i][1]-'0');
               }
    		   if (b[i][0]=='W'&& (b[i][1]>='0'&&b[i][1]<='9'))
               {
    			   y = y+(b[i][1]-'0');
               }
    		   if (b[i][0]=='S'&& (b[i][1]>='0'&&b[i][1]<='9'))
               {
    			    y = y-(b[i][1]-'0');
               }
    		} 
    		else if(k==3)
    		{
    			if (b[i][0]=='A'&& (b[i][1]>='0'&&b[i][1]<='9')&&(b[i][2]>='0'&&b[i][2]<='9'))
    			{
    				x = x-((b[i][1]-'0')*10+b[i][2]-'0');
    			}
    			if (b[i][0]=='D'&& (b[i][1]>='0'&&b[i][1]<='9')&&(b[i][2]>='0'&&b[i][2]<='9'))
    			{
    				x = x+((b[i][1]-'0')*10+b[i][2]-'0');
    			}
    			if (b[i][0]=='W'&& (b[i][1]>='0'&&b[i][1]<='9')&&(b[i][2]>='0'&&b[i][2]<='9'))
    			{
    				y = y+((b[i][1]-'0')*10+b[i][2]-'0');
    			}
    			if (b[i][0]=='S'&& (b[i][1]>='0'&&b[i][1]<='9')&&(b[i][2]>='0'&&b[i][2]<='9'))
    			{
    				y = y-((b[i][1]-'0')*10+b[i][2]-'0');
    			}
    		}
        }
    	cout<<x<<","<<y<<endl;
    }
    */
    /*   报数
    #include <iostream>
    using namespace std;
    struct Node
    {
    	int Data;
    	Node *next;
    };
    class list
    {
    public:	
    	Node *head;
    	list() {head = NULL;}
    	void InsertNode(int a);
    };
    void list::InsertNode(int a)
    {
        Node *p,*s,*q;
        s = (Node *)new(Node);
    	s->Data = a;
    	p = head;
    	q = head;
    	if (p == NULL)
    	{
    		head = s;
    		s->next = s;
    	} 
    	else
    	{
    		while (q->next!=p)
    		{
    			q = q->next;
    		}
    		q->next = s;
    		s->next = p;
    	}
    	
    }
    void main()
    {
    	int n,i;
        char s;
    	list slist;
    	Node *p,*q;
        do
    	{
    		scanf("%d",&n);
    		p = NULL;
    		q = NULL;
            slist = list();
    		for (i=1;i<=n;i++)
    		{
    			slist.InsertNode(i);
    		}
    		p = slist.head;
    		while (p!=q)
    		{
    			for (i=1;i<3;i++)
    			{
    				q = p;
                    p = p->next;
    			}
                q->next = p->next;
    			delete p;
    			p = q->next;
    		}
    		cout<<p->Data<<endl;	
    	}
    	while(scanf("%c",&s) != EOF);
    }
    */
    /*  数字基ROOT*/
    /*
    #include <iostream>
    using namespace std;
    int fun1(char *a)
    {
    	int i;
    	int sum = 0;
    	for (i=0;i<strlen(a);i++)
    	{
            if (a[i]>='0'&&a[i]<='9')
            {
    			sum = sum + a[i] -'0';
            }
    		else
    			return -1;
    	}
    	return sum;
    }
    int fun2(int a)
    {
    	if (a<10)	return a;
        int temp;
    	int sum = 0;
    	while (a)
    	{
    		temp = a%10;
    		a=a/10;
    		sum = sum + temp;
    	}
       return sum;
    }
    void main()
    {
        char a[100];
    	int sum;
    	while (gets(a))
    	{
    		sum = fun1(a);
    		while(sum>9)
    		{
    		  sum = fun2(sum);	
    		}
    		cout<<sum<<endl;
    	}	
    }
    */
    /*识别有效IP地址和掩码
    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    static int result[7]={0};   //保存最后结果的数组
    string Chang(string str)    //转换为二进制
    {
    	int num=atoi(str.c_str());
    	string tmp;
    	char tmp1[10]={0};
    	itoa(num,tmp1,2);
    	tmp=string(tmp1);
    	if (tmp.size()<8)
    	{
    		string tmp2(8-tmp.size(),'0');
    		str=tmp2+tmp;
    	}
    	else
    		str=tmp;
    	return str;
    }
    void checkNetSegment(string mask,string ip1)
    {	
    
    	int count1=0,count2=0;
    	int count4=0,count5=0;
    	string str1[4],str3[4];
    	for(int i=0; i<mask.size(); i++)  //掩码的点个数(不连续的点等于3个)
    		if(mask[i]=='.' && mask[i+1]!='.')
    			count1++;
    	for(int i=0; i<ip1.size(); i++)  //ip1的点个数
    		if(ip1[i]=='.' && ip1[i+1]!='.')
    			count2++;
    
    	for(int i=0; i<mask.size(); i++)  //掩码的点个数(所有的点等于3个)
    		if(mask[i]=='.')
    			count4++;
    	for(int i=0; i<ip1.size(); i++)  //ip1的点个数
    		if(ip1[i]=='.')
    			count5++;
    
    	if(count1==count2&&count1==3
    		&& count4==count5&&count4==3)  //如果格式正确
    	{
    		int i=0;                         //ip1先分解为4段
    		int flag1=0;
    		int k=0;
    		while(i<ip1.size())
    		{
    			int tmp=i;
    			while(isdigit(ip1[tmp]))
    				tmp++;
    			str1[k]=ip1.substr(i,tmp-i);
    			k++;
    			i=tmp+1;
    		}
    		i=0;                   //掩码分为4段 		
    		int flag3=0,flagmk=0; 	
    		k=0; 
    		string test;
    		while(i<mask.size()) 	
    		{ 		
    			int tmp=i; 	
    			while(isdigit(mask[tmp])) 		
    				tmp++; 		
    			str3[k]=mask.substr(i,tmp-i); 	
    			k++; 	
    			i=tmp+1; 	
    		} 		
    		for(int i=0; i<4; i++)         //判断每一段的值是否在0-255之间 	
    			if(atoi(str1[i].c_str())<256 &&atoi(str1[i].c_str())>=0) 	
    				flag1++; 	
    
    		for(int i=0; i<4; i++) 	
    			if(atoi(str3[i].c_str())<256 &&atoi(str3[i].c_str())>=0)
    				flag3++; 
    
    		for (int i=0;i<4;i++)        //判掩码是不是1在0前面
    		{
    			string temp=Chang(str3[i]);
    			test+=temp;
    		}
    
    		for (int j=1;j<32-1;j++)
    			if (test[j-1]>test[j] && test[j]<test[j+1])
    			{
    				flagmk=1;
    				break;
    			}
    
    			if(flag1==flag3 &&flag3==4 &&flagmk==0)      //格式正确		
    			{ 
    
    				if (str1[0]>="1" &&str1[0]<="126" )
    					result[0]++;
    				else if (str1[0]>="128" &&str1[0]<="191")
    					result[1]++;
    				else if (str1[0]>="192"&&str1[0]<="223")
    					result[2]++;
    				else if (str1[0]>="224"&&str1[0]<="239")
    					result[3]++;
    				else if (str1[0]>="240"&&str1[0]<="255")
    					result[4]++;
    				else if (str1[0]=="127")
    					result[5]++;
    
    				if (str1[0]=="10"||         //私有IP
    					(str1[0]=="172"&&str1[1]>="16"&&str1[1]<="31")||
    					(str1[0]=="192"&&str1[1]=="168"))
    					result[6]++;
    			}  		
    			else 	
    				result[5]++; 	
    	} 	
    	else                                         //格式不正确 	
    		result[5]++;
    }  
    int main() 
    { 	
    	string s1,ms,ip;
    	while (getline(cin,s1))
    	{
    		int position=s1.find("~",0);
    		ip=s1.substr(0,position);
    		ms=s1.substr(position+1,s1.size()-1);
    		checkNetSegment(ms,ip);
    	}
    	for (int i=0;i<7;i++)
    	{
    		cout<<result[i];
    		if (i==6)
    			cout<<endl;
    		else
    			cout<<" ";
    	}
    	return 0;
    }
    */
    /*mimajiaoyan*/
    #include <iostream>
    using namespace std;
    int LRS(char *a)
    {
    	int leng=strlen(a);
    	int Max=0,count=0;
    	int start1,start2;
    	for(int i=0;i<leng;i++)
    		for(int j=i+1;j<leng;j++)
    		{
    			start1=i;start2=j;
    			while(a[start1]==a[start2] && start1<leng && start2<leng)
    			{
    				count++;
    				start1++;
    				start2++;
    			}
    			if(count>Max)
    				Max=count;
    			count=0;
    		}
    		return Max;
    }
    int check(char *a)
    {
    	int flag[4]={0};
    	int i,sum=0;
    	if (strlen(a) < 8)
    	{
    		return 0;
       }
    	for(i=0;i<strlen(a);i++)
    	{
          if (a[i]>='A'&&a[i]<='Z')
          {
    		  flag[0] = 1;
          } 
          else if(a[i]>='a'&&a[i]<='z')
          {
    		  flag[1] = 1;
          }
    	  else if(a[i]>='0'&&a[i]<='9')
    	  {
    		  flag[2] = 1;
    	  }
    	  else
    		  flag[3] = 1;
    	}
    	for(i=0;i<4;i++)
    	{
          sum = sum + flag[i];
    	}
    	if (sum>=3)
    	{
            if (LRS(a)>2)
                 return 0;
            else
    			 return 1;
    	}  
    	else
    	   return 0;
    }
    void main()
    {
       char a[20];
       while (gets(a))
       {
    	   if (check(a) == 0)
    		   cout<<"NG"<<endl;
    	   else
    		   cout<<"OK"<<endl;  
       }   
    }
    

     中级题和部分初级题

  • 相关阅读:
    【leetcode】二叉搜索树中的众数
    【leetcode】和为零的N个唯一整数
    【leetcode】统计位数为偶数的数字
    【leetcode】寻找数组的中心索引
    【leetcode】找出给定方程的正整数解
    【leetcode】寻找比目标字母大的最小字母
    【leetcode】找出井字棋的获胜者
    输出0~N之间的素数
    判断正整数是否是素数(数据结构课后练习题2.13a)
    052-85
  • 原文地址:https://www.cnblogs.com/zlj830/p/4717159.html
Copyright © 2011-2022 走看看