String for learning algorithms

Keywords: Mobile

2072 Count different words

#include<set>
#include<string>
#include<iostream>
using namespace std;
int main(){
	char c='0';
	string s;
	set<string> st;
	while((c=getchar())!='#')
	{
		s="";//Ah Ah Ah Ah Ah is really gassy 
		while(c!=' '&&c!='\n'&&c!='#'){
			s+=c;
			c=getchar();
		}
		if(c=='#') return 0;
		if(s.length())  st.insert(s);
		if(c=='\n'){
			printf("%d\n",st.size());
			st.clear(); 
		}
		
	}
	return 0;
} 

Because I learned about set, the elements inside the set are automatically de-duplicated and value-added sorting, so as long as the word insert is in, size() can be used to output directly. One of the long-standing and crashing errors I found is that the initial assignment of S is written as==, which becomes a judgment function, so the values inside s are not emptied, so they are kept before each time, so they will be different every time..

2081 Mobile Short

#include<string>
#include<iostream>//40 
#include<stdio.h>
#include<string.h>
using namespace std;
int main(){
    int n;
    string s,re;
    scanf("%d",&n);
    getchar();
    while(n--){
   	   re="6"; 
       getline(cin,s);
   	   re+=s.substr(6,5);
   	   cout<<re<<endl;
	}
    
    return 0;
}

6min, it's not difficult at all.

2093 Test Rank


#include<string>
#include<iostream>//52 
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
using namespace std;
struct student{
	string name;
	int num_pro;
	int num_time;
}stu[1000]; 

bool cmp (student a,student b){
	if(a.num_pro!=b.num_pro)  return a.num_pro>b.num_pro;
	else if(a.num_time!=b.num_time)  return a.num_time<b.num_time;
	else return a.name<b.name;
}
int main(){
    int n,f;
    char re[12];
    scanf("%d%d",&n,&f);
    int i=0;
   while(cin>>stu[i].name){
    	stu[i].num_pro=0;
		stu[i].num_time=0; 
  	   for(int j=0;j<n;j++){
   	   	  scanf("%s",re); 
   	   	  int len=strlen(re);
   	   	  int flag=0;//Determine if there are parentheses 
   	   	  int index;//The position of brackets; 
   	   	  if(re[0]=='-'||re[0]=='0') continue;
   	   	  else{
   	   	  	stu[i].num_pro++;//A question has been made 
   	   	  	for(int k=0;k<len;k++){
   	   	  		if(re[k]=='(')//Encounter parentheses 
   	   	  	    { flag=1;
   	   	  	     index=k; 
   	   	  	     break;
				}
   	   	  	   
              }
            if(flag){
            	for(int k=0;k<index;k++){
          			stu[i].num_time+=(re[k]-'0')*pow(10,index-k-1);
				} 
            	for(int k=index+1;k<len-1;k++)//Last one is) 
            	{
            		stu[i].num_time+=((re[k]-'0')*pow(10,len-k-2))*f;
				} 
			}
			else{//No parentheses 
			    for(int k=0;k<len;k++)
				  stu[i].num_time+=(re[k]-'0')*pow(10,len-k-1);
			}
   	   	  
       }
   
	}
	i++;
   }
	 sort(stu,stu+i,cmp);
	 for(int k=0;k<i;k++){
	 	printf("%-10s %2d %4d\n",stu[k].name.c_str(),stu[k].num_pro,stu[k].num_time);//Default Right Alignment 
	 }
    
    return 0;
}

It was written for an hour and it may have changed for a couple of minutes, but I really learned a lot.The idea is also simple, mainly the implementation is a bit cumbersome.This topic gives me the following points to learn

  • Writing of Structures
  • The sort function header file is #include and has cmp notation
  • In the output format, string type printf output with==.c_str() function==, which returns the first address of a string object
  • Output format, character format ==%-10s%2d%4d==, default is right alignment, left alignment is preceded by a negative sign

2091 Hollow Triangle

#include<iostream>
#include<string.h>
#include<string>
#include<cmath>
#include<algorithm> //49 
using namespace std;
int main(){
	int n;
    char c;
    int ca=1;
    scanf("%c",&c);
	while(c!='@' ){
		scanf("%d",&n);
		if(ca!=1) printf("\n");
		for(int i=0;i<n-1;i++){//Line i 
		    int emp=n-1-i;
		    //cout<<"1"<<endl;
			while(emp--)  printf(" ");
			//cout<<"2"<<endl;
			printf("%c",c);
		//	cout<<"3"<<endl;
			if(i==0) printf("\n");//Line 1 Wrap
			else{
				int emp2=2*i-1;//Number of middle spaces 
				while(emp2--) printf(" ");
				printf("%c\n",c);
			} 
			 
		}
		//Last line
		for(int i=0;i<2*n-1;i++){
			printf("%c",c);
		} 
		ca++;
		printf("\n");
		getchar();
		scanf("%c",&c);
	}

	return 0;
} 

Write ten minutes changed for half an hour, the problem is still the format problem????Is there something wrong with my language?Or did you change it by looking at someone else's format, which is to add a blank line before output instead of after it?Even if there is no empty line between the end of the previous group and the input of the next group, that's too ridiculous
1. Input format: each group ends with'@';
2. Output format, one line should be blank before printing a triangle, the first group is not blank; there is no blank line between the previous triangle and the next group of inputs;
3. Note the spaces in the title. No spaces are output on the left side of the triangle, no spaces are output in the middle of the triangle, and no spaces are output on the right side of the triangle.
4. Each input uses the scanf() function, so the getcharr() function is used to absorb line breaks.
Also, getchar() is required before each input except for the first time.

1004 Let the Balloon Rise

#include<iostream>
#include<string.h>
#include<string>
#include<cmath>
#include<algorithm> //39 
#include <map>
using namespace std;
int main(){
	int n,i=0;
    string s,maxcolor;
    int ca=1;
    scanf("%d",&n);
    while(n){
  	  map<string,int>hash;
  	  hash.clear();
  	  for(int i=0;i<n;i++){
  	  	cin>>(s);
  	    hash[s]++;
	  }
	  map<string,int>::iterator it;
	  int max=0;
	  for(it=hash.begin();it!=hash.end();it++){
	  	 if(it->second>max){
	  	 	max=it->second;
	  	 	maxcolor=it->first;
		   }
	  }
  	  cout<<maxcolor<<endl;
  	  scanf("%d",&n);
  	    
	}
    
	

	return 0;
} 

Because I've been looking at STL recently, it's easy to think about map implementation, but since I haven't trained in map, I still can't remember some of the writings when I look at it once. This is just a map exercise.

2057 a+b again

#include<iostream>
#include<string.h>
#include<string>
#include<cmath>
#include<algorithm> //10 
#include <map>
using namespace std;
void print(__int64 sum);//Convert to 16-bit output 
int main(){
	int n,i=0;
    char s1[16],s2[16];
    while(scanf("%s",s1)!=EOF){
   	  int len=strlen(s1);
   	  //cout<<len<<endl;
   	  __int64 sum=0;
   	  if(s1[0]=='-'){
   	  	 for(int i=1;i<len;i++){
   	  	 	if(s1[i]>='0'&&s1[i]<='9'){
   	  	 		sum-=(s1[i]-'0')*pow(16,len-i-1);
		    }
		    else if(s1[i]>='A'&&s1[i]<='F'){
		    	sum-=(s1[i]-'A'+10)*pow(16,len-i-1);
			}
			else if(s1[i]>='a'&&s1[i]<='f'){
		    	sum-=(s1[i]-'a'+10)*pow(16,len-i-1);
			}
		    
		  }
   	  	 
       }
       else{
   	     for(int i=0;i<len;i++){
   	  	 	if(s1[i]>='0'&&s1[i]<='9'){
   	  	 		sum+=(s1[i]-'0')*pow(16,len-i-1);
		    }
		    else if(s1[i]>='A'&&s1[i]<='F'){
		    	sum+=(s1[i]-'A'+10)*pow(16,len-i-1);
			}
		    else if(s1[i]>='a'&&s1[i]<='f'){
		    	sum+=(s1[i]-'a'+10)*pow(16,len-i-1);
			}
		  }
	   }
	   //cout<<sum<<endl;
	   scanf("%s",s2);
	   int len2=strlen(s2);
	   //cout<<len2<<endl;
	   if(s2[0]=='-'){
   	  	 for(int i=1;i<len2;i++){
   	  	 	if(s2[i]>='0'&&s2[i]<='9'){
   	  	 		sum-=(s2[i]-'0')*pow(16,len2-i-1);
		    }
		    else if(s2[i]>='A'&&s2[i]<='F'){
		    	sum-=(s2[i]-'A'+10)*pow(16,len2-i-1);
			}
		    else if(s2[i]>='a'&&s2[i]<='f'){
		    	sum-=(s2[i]-'a'+10)*pow(16,len2-i-1);
			}
		  }
   	  	 
       }
       else{
   	     for(int i=0;i<len2;i++){
   	  	 	if(s2[i]>='0'&&s2[i]<='9'){
   	  	 		sum+=(s2[i]-'0')*pow(16,len2-i-1);
		    }
		    else if(s2[i]>='A'&&s2[i]<='F'){
		    	sum+=(s2[i]-'A'+10)*pow(16,len2-i-1);
			}
		    else if(s2[i]>='a'&&s2[i]<='f'){
		    	sum+=(s2[i]-'a'+10)*pow(16,len2-i-1);
			}
		  }
	   }
	   print(sum);
  	    
	}
  
	return 0;
} 
void print(__int64 sum){
	if(sum<0){
		sum=-sum;
		printf("-%I64X\n",sum);
	}
	
	else
	printf("%I64X\n",sum);
} 

Again, there are several points to note. One.Numbered speech ranges are large, long or uInt64, 2. Output is in uppercase, direct X uppercase is OK.After writing for a long time, I found that other people are directly judging emmm. The original hexadecimal input directly includes the plus and minus sign. I learned

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std;
 
int main ()
{
//	__int64 a,b;
//	while(scanf("%I64X%I64X",&a,&b)!=EOF)
//	{
//		if(a+b<0)
//		{
//			printf("-%I64X\n",-a-b);
//		}
//		else
//		{
//			printf("%I64X\n",a+b);
//		}
//	}
    long long a,b;
	while(scanf("%llx%llx",&a,&b)!=EOF){
		if(a+b<0)  printf("-%llX\n",-a-b);
		else printf("%llX\n",a+b);
	} 
	return 0;
}

This is done with long long and uTwo ways of writing int64, it is worth noting that negative numbers should be picked out to judge

Posted by kritro on Sat, 06 Jun 2020 20:22:13 -0700