Length of the last word of the string
#include<stdio.h> #include<string.h> const int maxn=5010; int main(){ char str[maxn]; gets(str); int len=strlen(str); int num=0; for(int i=len;i>=0;i--){ if(str[i]!=' '&& str[i]!='\0') num++; else if(str[i]=='\0') continue; else break; } printf("%d",num); return 0; }
- The code must be completed in the c environment, and the c + + environment cannot use gets ()
- strlen() function
- The function of getnfs () can be used to receive the input data in a variety of languages. Both functions will be considered as the end of input when they encounter a carriage return, but gets() can receive spaces, but scanf() cannot
- The getline() function is a function of C + +, and the header file iostream is required to use this function h. By default, the function stops reading when it encounters a carriage return, but the carriage return key is masked at this time. The following code
string str_input;
getline(cin, str_input);
input_str = input().split(' '); print(len(input_str[-1]))
- str.split(str="", num=string.count(str)). split() slices the string by specifying a separator. If the parameter num has a specified value, it separates num+1 substrings
- len(): used to measure the length of Strings / tuples in variables
- str[-1] output the last character
Calculate the number of occurrences of a character
#include<stdio.h> #include<string.h> int main(){ char str[1010]; gets(str); char a; scanf("%c",&a); int i=0,num=0; while(str[i]!='\0'){ if(a>='a'&& a<='z'){ if(str[i]==a||str[i]==a-32) num++; } else if(a>='A'&& a<='Z'){ if(str[i]==a||str[i]==a+32) num++; } else{ if(str[i]==a) num++; } i++; } printf("%d",num); // printf("%d",i); return 0; }
- ascII A65 a97 A+32==a
info = input().lower() char = input().lower() print(info.count(char))
- The lower() method converts all uppercase characters in a string to lowercase.
Explicit random number
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; const int maxn=1010; int main(){ int n; int a[maxn]; while(cin){ cin>>n; for(int i=0;i<n;i++){ scanf("%d",&a[i]); } sort(a,a+n); for(int i=0;i<n;i++){ if(a[i]!=a[i+1]&&a[i]!=0){ cout<<a[i]<<endl; } a[i]=0; } } return 0; }
- Header file cin cout needs
- Using sorting requires
- Sort(start,end,cmp)
bool compare(int a,int b)
{
return a>b;
}
- Key point of this question: empty the array operation to avoid the impact of the last data
- The input does not exist 0, so you can take advantage of this
string delimiter
!!! Universal header file * * #include < bits / STDC + + h> **
- getline(cin, inputLine);
This function reads the entire line, including leading and embedded spaces, and stores it in a string object.
#include<bits/stdc++.h> using namespace std; int main() { string str1; while(getline(cin,str1)){ while(str1.size()>8){ cout<<str1.substr(0,8)<<endl; str1=str1.substr(8,str1.size()-8); } int len=str1.size(); if(len==8){ cout<<str1<<endl; } else if(len<8){ for(int i=0;i<8;i++){ if(i<str1.size()) cout<<str1[i]; else cout<<"0"; } cout<<endl; } } return 0; }
- str.substr(0,8) function
Binary conversion
#include<bits/stdc++.h> using namespace std; int main(){ string s; cin>>s; int len=s.size(); int k=0.; int sum=0; for(int i=len-1;i>1;i--){ int t; switch(s[i]){ case '0':t=0;break; case '1':t=1;break; case '2':t=2;break; case '3':t=3;break; case '4':t=4;break; case '5':t=5;break; case '6':t=6;break; case '7':t=7;break; case '8':t=8;break; case '9':t=9;break; case 'A':t=10;break; case 'B':t=11;break; case 'C':t=12;break; case 'D':t=13;break; case 'E':t=14;break; case 'F':t=15;break; } sum+=t*pow(16,k); k++; } cout<<sum<<endl; return 0; }
- stoi() means to convert a string to an integer. It is a standard library function in C ++ STL, which is used to convert a given string in various formats (such as binary, octal, hexadecimal or simple numbers in string format) to an integer.
- stoi (string, starting position, n-ary)
Simple method:
#include<iostream> #include<string> using namespace std; int main(){ string str; while(cin>>str){ cout << stoi(str,0,16) << endl; }
Prime factor
Timeout timeout, my idea is to find all prime numbers less than n and try from small to large, but when it comes to too large numbers, it is not feasible and cannot pass all use cases
Reference ideas
- The prime number can be searched according to the search under the square root, sqrt(n)
- If all prime numbers within the square root cannot be divided, that is, after the for loop, n is still equal to the old n, then n can be determined to be prime and output n
#include<bits/stdc++.h> using namespace std; int main(){ int n,old; cin>>n; int q=sqrt(n); while(n!=1){ old=n;//Must be here! for(int i=2;i<=q;i++){ while(n%i==0){ cout<<i<<' '; n=n/i; } if(old!=n)break; } if(old==n){ cout<<n<<' '; break; } } return 0; }
- Starting from 2, divide it if you can divide it, divide it if you can't divide it, and divide it until it reaches the number, which ensures that the division is always a prime number. Because if it was a composite number, such as 4, it would have been removed at 2.
- If you only use the idea of 1 to remove, the time complexity is O(n), which will timeout, so another law is adopted: for example, 200000014, there must be at most one factor greater than the root 200000014. Therefore, after dividing the number from 0 to the root, there will only be a large prime number, which can be output directly. At this time, the time complexity is reduced to O(n^(1/2)).
rounding
- Floor() is not greater than the maximum integer of the argument
- Ceil() is not less than the maximum integer of the argument
- Round() round to the nearest integer
What a clever idea:
#include <stdio.h> int main(void) { double num; scanf("%lf",&num); printf("%d",(int)(num + 0.5)); return 0; }
Consolidated table record
! sort function on the sorting of structures
Rewrite cmp function
bool cmp(table a,table b){ return a.index<b.index; }
Extract non duplicate integers
- memset(k,-1,sizeof(k));