This is the 10th day of my participation in the August More Text Challenge. For details, see:August is more challenging

C++ basic review series — sun bujian 1208

C++ basic review series 01 (input/output class, calling mathematical function class)

C++ basics review series 2 (print graphics (loop), classic problems)

C++ basic review series 3 (recursive algorithm) {Fibonacci function, Hanoi issues}

C++ basics review series 4 (summary of scattered data)

C++ basics review series 5

Seven, c++ examples

Bubble sort

for(int i=0; i<n- 1; i++) {for (int j=0; j<n- 1-i; j++) {if(a[j]<a[j+1])
		a[j]^=a[j+1]^=a[j]^=a[j+1]; // Compare and exchange adjacent elements}}Copy the code

sort

The #include < algorithm > sort function defaults to less< datatype >() // larger < datatype >() // larger < datatype >() // larger < datatype >(

#include<iostream>
#include<algorithm>
using namespace std;
int main(a){
 char a[11] ="asdfghjklk";
 for(int i=0; i<10; i++) cout<<a[i]<<endl;sort(a,a+10,greater<char> ());for(int i=0; i<10; i++) cout<<a[i]<<endl;return 0;
}
Copy the code

10 to hexadecimal

#include<iostream>using namespace std;
char exchange(int n){     
    switch(n){     
        case 0:    return '0';break;
  		case 1:    return '1';break;
		case 2:    return '2';break;
        case 3:    return '3';break;
        case 4:    return '4';break;
        case 5:    return '5';break;
		case 6:    return '6';break;
		case 7:    return '7';break;
        case 8:   return '8';break;
      	case 9:  return '9';break;
      	case 10: return 'A';break;
		case 11:return 'B';break;
      	case 12:return 'C';break;
      	case 13:return 'D';break;
      	case 14: return 'E';break;
      	case 15:return 'F';break; }}int main(a){
int m,note,i=0,s=0;
    cin>>m;
    note=m;
    while(m/16! =0){
        i++;  m=m/16;  
    }
	char a[i];
 	while(note/16! =0){
        a[s]=exchange(note%16);
        s++;
        note=note/16; 
    }
    a[i]=exchange(note);
    for(intj=i; j>=0; j--){ cout<<a[j]; } cout<<endl;return 0;
}
Copy the code

Output intact: as a string

#include < iomanip> To keep n decimal places: cout<<fixed<< setPrecision (n)<<; #include < iomanip> the width setw(n) setw() function defaults to right-align the output, but if you want to left-align the output, you need to add setiosFlags (ios::left) to do so. To use a special symbol placeholder, let’s use @ as an example. To use a special symbol placeholder, you need to setfill(‘ x ‘) — where ‘x’ is a character, as follows: cout<<setw(6)<<setfill(” @ “)<<1234 @@1234 4. Bool sb(int n) {

​ int i; for(i = 2; i <= sqrt(n); i++) {

​ if((n % i) == 0) return false;

​ } return true;

}

Any integer gets the value of each digit

#include <iostream>
using namespace std;
void sb(int n){
	if(n>9) {sb(n/10);       // Delete the last bit of the input on each call
	cout<<n%10<<"";     // Print the first digit for each call
	}else
	cout<<n<<"";// Print all numbers less than 10
	}
int main(a){
    int n=0;
    cin>>n; 
	sb(n);
    cout<<endl;
	return 0;
}
Copy the code

Leap year: is divisible by 4 but not by 100, or is divisible by 400 if(y % 4 == 0 && y % 100! = 0 | | y % = = 0 400) factorial

long Fac(int n){
	if (n == 0)
		return 1;
	else
		return n * Fac(n - 1);
 }
Copy the code

The value ranges from 0 to 255

#include  <iostream>
#include  <bitset>
using namespace std;
int main(a){
    bitset<8>b;
    for(int i = 0; i < 256; i++){
        b = i;
        cout<<b<<endl;
    }
    return 0;
}
Copy the code

The recursive method

If (n <= 2){return 1; Return f(n-1) + f(n-2); return f(n-1) + f(n-2); }

The Tower of Hannautum question

#include <iostream>
using namespace std;
long count = 0;// Record the number of moves
void hanoi(int n,char a,char b,char c) {   // For n plates, move a to C and use B as a temporary tower
      if (1 == n){
          cout<<"The first"<<++count<<"Time."<<a<<"The tower -- - >"<<c<<"The tower"<<endl;
      }else{
          hanoi(n- 1,a,c,b);// Make a recursive call where a moves to B and C makes a temporary tower
          cout<<"The first"<<++count<<"Time."<<a<<"The tower -- - >"<<c<<"The tower"<<endl;
          hanoi(n- 1,b,a,c); }}int main(a){
	int n;
	cout<<"Enter the number of Tower of Hannot disks:";
	cin>>n;
	hanoi(n,'A'.'B'.'C');	
	return 0;
}
Copy the code

Fish problem

#include<iostream>
using namespace std;
int fish(int n, int x){
    if((x- 1) %5= =0) {if(n == 1)
            return 1;  
        else
            return fish(n- 1, (x- 1) /5*4); 
    }
    return 0;  Return 0 if x is not a valid solution
}
int main(a){
	int i=0, flag=0, x;
	do{   
      i=i+1;
      x=i*5+1;  		// The minimum value of x is 6, and then each increment is 5
    if(fish(5, x)){    // Pass x into the partition recursive function to verify
        flag=1;  // Set the flag bit to 1 when the first x is found
        cout<<"The total number of fish caught by a team of five is<<x; }}while(! flag);// If x is not found, continue the loop, otherwise exit the loop
	return 0;
}
Copy the code

Description: Enter two positive integers, a and b, and find the result of a/b to 20 decimal places. Input: two positive integers, a and B output: pay attention to the end of the line output enter, pay attention to the last digit rounded.

#include <iostream>
#include <cmath>
using namespace std;
int main(a){
    int a, b, i;
	cin >> a >> b;
	a = a % b;
	for(i = 0; i < 19; i ++){
		a = a * 10;
		cout << a / b;
		a = a - a / b * b;
	}
	a = a * 10;
	i = a / b;
	a = a - a / b * b;
	a = a * 10;
	if(a > 4)
		cout << (i + 1) << endl;
	else
		cout << i << endl;
	return 0;
}
Copy the code

Define a structure to describe student information, including name, student number, age, gender, and cell phone number, where age is an int, gender is a char, and the rest of the members are strings of length less than 20 (without Spaces). Enter n (n < 20) and the information of n students, and then enter the phone number of the student you want to find. If the student is Found, output the information, use tabs to separate the items, otherwise output Not Found.

#include <iostream>
#include <string.h> 
using namespace std;
struct student{
    char name[27];
    char no[27];
    int age;
    char gender;
    char phone[27];
}; 
int main(a){
    student a[27];
    int  n,i;
    char phone[27];
    cin>>n;
    for(i = 0; i < n; i ++)
    cin >> a[i].name >> a[i].no >> a[i].age >> a[i].gender >> a[i].phone; 
    cin >> phone;
    for(i = 0; i < n; i ++)
    if(!strcmp(a[i].phone, phone)) {
    cout << a[i].name << "\t" << a[i].no << "\t" << a[i].age << "\t" << 		a[i].gender << "\t"<< a[i].phone << endl; 
    return 0; 
    }
    cout << "Not Found" << endl; 
}
Copy the code

Define a structure to describe student information, including name, student number, age, gender, and cell phone number, where age is int, gender is char, and the rest of the members are strings less than 20 in length (without Spaces). Input n (n < 20) and the information of N students, and output the information of students in descending order of age. If the age is equal, output the information in ascending order of name.

#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
struct student{
    char name[27];
    char no[27];
    int age;
    char gender;
    char phone[27];
	}a[27]; 
bool cmp(student a, student b) {
    if(a.age == b.age){
    if(strcmp(a.name, b.name) < 0)
        return true;
        else
        return false;
    }
    return a.age > b.age;
    }
    int main(a){
    int n, i;
    cin >> n;
    for(i = 0; i < n; i ++) {
    cin >> a[i].name >> a[i].no >> a[i].age >> a[i].gender >> a[i].phone;
    }
    sort(a, a + n, cmp); 
    for(i = 0; i < n; i ++) {
    cout << a[i].name << "\t" << a[i].no << "\t" << a[i].age << "\t" << a[i].gender << "\t" <<
    a[i].phone << endl;
    }
    return 0;
    }
Copy the code

Enter today’s date, output yesterday’s date.

#include <iostream>
#include <cmath>
using namespace std;
int day[] = {0.31.28.31.30.31.30.31.31.30.31.30.31}; 
bool f(int a) {
if(a % 4= =0 && a % 100! =0 || a % 400= =0)
return true;
return false;
}
int main(a){
int y, m, d;
6
cin >> y >> m >> d;
if(d == 1) {if(m == 1)
cout << y- 1 << 31 "12" << endl;
else if(m == 3) {if(f(y))
cout << y << 29 "2" << endl; 
else
cout << y << 28 "2" << endl;
}
else
cout << y << "" << m - 1 << "" << day[m- 1] << endl;
}
else
cout << y << "" << m << "" << d - 1 << endl; 
return 0;
}
Copy the code

Given the difference c between two adjacent prime numbers A and B, output a b, where a and B are guaranteed to be the smallest of all possible solutions. For example, if the input c value is 2, 3 5 Carriage return should be printed instead of 5 7 Carriage return. Notice the return at the end of the line. C is less than 100, and they’re guaranteed to have a solution

#include <iostream>
using namespace std;
bool p(int a)
{
	for(int i=2; i<a; i++) {if(a%i==0)
		return false;
	}
	return true;
}
int main(a)
{
	int c,x=0;
	cin>>c;
	int a=2,b=3;

for(inti = a;; i ++) {if(a - b == c)
		{
			cout << b << "" << a << endl;
			return 0;
		}
		if(p(i)) { b = a; a = i; }}return 0;	
}
Copy the code

Write a program that calculates the most frequent alphanumeric characters in a string. If two or more letters appear the same number of times and the same is the highest frequency, the letter with the smaller dictionary order is output. Note that the ASCII value for uppercase letters is smaller than that for lowercase letters.

#include <stdio.h>#include <string.h>#include <iostream>
#include <iomanip>#include <cmath>#include <string.h>
using namespace std;
bool f(char a)
{
if(a >= 'a' && a <= 'z')
	   return true;
	   return false;
}

bool g(char a)
{
	if(a >= 'A' && a <= 'Z')
		 return true;
	 return false;
}

int main(a)
{
	char a[1007];
	int i, b[52] = {0}, max = - 1;
	cin.get(a, 1007);
	for(i = 0; a[i]; i ++)
	{
		if(f(a[i]))
		{
			b[a[i] - 'a' + 26] + +; }else if(g(a[i]))
		{
			b[a[i] - 'A'] ++;
		}
	}
	for(i = 0; i < 52; i ++)
	{
		if(b[i] > max)
			max = b[i];
	}
	for(i = 0; i < 52; i ++){
		if(b[i] == max)
		{
		if(i < 26)
		{
			cout << (char) ('A' + i) << endl;
			break;
		}
		else
		{
			cout << (char) ('a' + i - 26) << endl;
			break; }}}return 0;
}
Copy the code

Write a program that calculates the most frequent alphanumeric characters in a string. If there are more than two letters that appear the same number of times, and the same is the highest frequency, the first letter in the original string is printed.

#include <stdio.h>#include <string.h>#include <iostream>
#include <iomanip>#include <cmath>#include <string.h>
using namespace std;
bool f(char a)
{
	if(a >= 'a' && a <= 'z')
		return true;
	return false;
}bool g(char a)
{  if(a >= 'A' && a <= 'Z')
return true;
	return false;
}

int main(a)
{   char a[1007];
	int i, b[52] = {0}, max = - 1;
	cin.get(a, 1007);
	for(i = 0; a[i]; i ++)
	{    if(f(a[i]))
		{ b[a[i] - 'a' + 26] + +; }else if(g(a[i]))
		{b[a[i] - 'A'] + +; }}for(i = 0; i < 52; i ++)
	{
		if(b[i] > max)  max = b[i];
	}
	for(i = 0; a[i]; i ++)
	{
		if(f(a[i]) && b[a[i] - 'a' + 26] == max)
		{   cout << a[i] << endl;
			break;
		}
		else if(g(a[i]) && b[a[i] - 'A'] == max)
		{	cout << a[i] << endl;
			break;	}}	return 0; }Copy the code