C + + brush notes

Write in front: mainly for myself. My professional c + + curriculum planning requirements are very low, and I only learn the part of c

You are very welcome to give me a little guidance 55

function

Perfect number

A unit holds a diving competition in a lake. This is a team project. Each team is composed of n people and requires all team members to dive from bank a to bank B. Oxygen cylinders must be used during diving, but each team has only one oxygen cylinder. At most two people use one oxygen cylinder at the same time, but at this time, they must move forward synchronously, so the time to reach the end point is equal to the time of the slower person from a to B alone. Everyone is Nice. Any two people are willing to share an oxygen bottle for swimming. Please arrange a strategy so that the last player can reach the finish line as soon as possible.

Programming requirements: write the program, first input the number of team n (n < 1000), and then the time taken for n team members to swim to the end alone. It is required to output the earliest time for all team members to reach the finish line.

Related knowledge: you can sort arrays through the sort function. The header file you need < algorithm >

#include<iostream>
using namespace std;

void printInt(int n,int base){
    cin>>n>>base;
    if(n<0) {cout<<'-';n=-n;}
    int m=0,a[100];
    while(n){
        a[m]=n%base;
        m++;
        n/=base;
    }
    for(int i=m-1;i>=0;i--){
        if(a[i]>=10){
            printf("%c",'A'+a[i]-10);//If you use cout, you need to redefine a string
        }
        else cout<<a[i];
    }
}
int main()
{
    int n, base;
    cin>>n>>base;
    printInt(n, base);
    
    return 0;
}

get date

Write a program to input a string and a character. If the character exists in the string, the remaining characters in the string will be output from the last position of the character.

Programming requirements

According to the prompt, supplement the code in the editor on the right and enter a string and a character. If the character exists in the string, the remaining characters in the string will be output from the last position of the character. The cstring Library (mystrrchr) must not be used.

		#include<iostream>
		using namespace std;
		bool getdate(int d,int &month,int &date);
		int m[12]={31,29,31,30,31,30,31,31,30,31,30,31};
		int main()
		{
		    int d, month, date, res;
		    cout << "Please enter the number of days:";
		    cin >> d;
		    res = getdate(d, month, date);
		    if(res)
		        cout << endl<<"month=" << month << ",date=" << date << endl;
		    else
		        cout <<endl<< "wrong number!" << endl;
		    return 0;
		}
		bool getdate(int d, int &month, int &date) {//Return address
		    int sum = 0, i;
		    if (d < 1 || d > 366)return false;
		    else {
		        i = 0;
		        do {
		            sum += m[i];
		            ++i;
		            if (sum >= d)break;
		        } while (i <= 12);
		        month = i;
		        date = d - sum + m[i - 1];
		        return true;
		    }
		}

Goldbach conjecture

Goldbach conjecture points out that any sufficiently large even number can be expressed as the sum of two prime numbers. For example: 4 = 2 + 2, 6 = 3 + 3, 8 = 3 + 5... 50 = 3 + 47. This related task: any even number entered is represented by the sum of two prime numbers.

The input of this question is positive even number n, 4 < = n < = 1000.

Programming requirements

According to the prompt, add code in the editor on the right to judge whether an integer is a prime number, and complete it with a function to make it meet the requirements.

There may be very broken notes in this, because I wrote it down a little bit when I slowly learned it. I can be said to be a fool 55

		#include<iostream>
		#Include < iomanip > / / pay attention to the supplementary Library
		#include<cmath>
		using namespace std;
		void divide(int n);//Pre declaration function
		int prime(int a);
		
		int main()
		{
		    int i,m,n;
		    cin>>n;
		    if(n==4) cout<<"4=2+2";//Consider 2 separately
		    else
		    {
		       divide(n);//It can also be merged into the main function
		    }
		    return 0; 
		}
		
		void divide(int n){
		    int m,i;
		     for(i=3;i<n/2;++i)//It can be calculated to n/2 to reduce the amount of cyclic operation
		     {
		        if(prime(i)&&prime(n-i)) //It is known to be an even number and the Goldbach conjecture holds empirically, so find a prime number
		        {
		            cout<<n<<"="<<i<<"+"<<n-i<<endl; 
		            break;//If you do not break, you will get multiple sets of solutions. This program only needs one set
		        }
		    }
		}
		int prime(int a){//Judgement prime
		    int i;
		    for(i=2;i<=sqrt(a);++i){//It can be calculated to sqrtA to reduce the amount of cyclic operation
		        if(a%i==0) return 0;//In addition to 1 and itself, you can find factors, not prime numbers
		    }
		    return 1;//No factor found in the loop, jump out of the loop, return1
		}

Keywords: C++

Added by KC_Geek on Thu, 09 Dec 2021 08:47:34 +0200