C++ Practice Workbook (C++ Basic Grammar Advanced Exercises)

Level 1: The sum of different digits of the same number

mission details

Task for this level: Write a small program that can calculate the sum of different digits of the same number.

programming requirements

Find the value of s=a+aa+aaa+aaaa+aa...a, where a is a number and the last item has n digits.

For example, when the user enters a=1, n=3, it means that 1+11+111 is required.

The effect is as follows: Input: 1 2Output:12


Let's start your mission, I wish you success!

code:

#include <iostream>
using namespace std;

size_t Sum(size_t a, size_t n)
{
	size_t sum = a, tmp = a;
	size_t i = 0;
	for (i = 1; i < n; i++)
	{
		tmp = tmp * 10 + a;
		sum += tmp;
	}
	return sum;
}

int main()
{
	int a = 0, n = 0;
	cin >> a >> n;
	cout << Sum(a, n);
	return 0;
}

Level 2: Finding the Average

mission details

Task for this level: Write a small program that can calculate the average of n integers.

programming requirements

Enter n integers from the keyboard, and write a program to calculate the average of all integers among them. The input consists of 2 lines: the first line contains an integer representing the number n of integers entered, and the second line contains n integers separated by spaces.

The effect is as follows: Input:6 -1 3 6 0 -5 4

output:1.16667


Let's start your mission, I wish you success!

code:

#include <iostream>
using namespace std;

int main()
{

    //获取参数方式 cin
    //int x =0;
    //cin >> x;

    //结果输出使用 cout
    //cout<<"1";

     // 请在此添加你的代码
    /********** Begin ********/
    int a,b;        
    float sum,s;    
    cin>>a;    
    for(int i=1;i<=a;i++)
    {        
	    cin>>b;        
	    sum=sum+b;           
    }    
    s=(sum)/a;    
    cout<<s;
    return 0;
    /********** End **********/
}

Level 3: Generate ISBN13 code

mission details

The task of this level: Write a small program that can generate ISBN13 codes.

related information

The SBN13 number was officially launched in 2007. The ISBN13 number of each book consists of five parts:

  • 978 (or 979);
  • Publishing country or language code;
  • publisher code;
  • book code;
  • checksum.

For example: the ISBN13 number of "C++ Programming (Second Edition)" is 978-7-302-21897-5, where 5 is the check code.

algorithm

Demonstration data: 7-5080-2710-8

  • Remove the check code 8 at the end, and add 978 (China) in front, which is: 978750802710
  • Starting from code position number 2, the sum of all even-numbered digital codes is a, 7 + 7 + 0 + 0 + 7 + 0 = 21
  • Multiply a by 3 to get a, 21 * 3 = 63
  • Starting from code position number 1, the sum of all odd-numbered digital codes is b, 9 + 8 + 5 + 8 + 2 + 1 = 33
  • Add a and b to c, 63 + 33 = 96
  • Take the single digit d of c (if the last calculated single digit d is 0, the check value value is 0). Units: 6
  • Subtracting d from 10 is the check digit value, and put it in the last digit. 10 - 6 = 4
  • The barcode is: 9 7 8 7 5 0 8 0 2 7 1 0 4

programming requirements

According to the first four parts of the input ISBN13 and the verification algorithm, a complete ISBN13 code is generated and output.

Note : Because it is automatically judged by the machine, we agree that each number in the first four parts of the input is separated by a space.

For example, input 9 7 8 7 3 0 2 2 1 8 9 7 and output 9787302218975.

The effect is as follows: Input: 9 7 8 7 3 0 2 2 1 8 9 7Output:9787302218975


Let's start your mission, I wish you success!

code:

#include <iostream>
using namespace std;

int main()
{

    //获取参数方式 cin
    //int x =0;
    //cin >> x;

    //结果输出使用 cout
    //cout<<"1";

     // 请在此添加你的代码
    /********** Begin ********/
    int a,b,c,d,e,f,g,h,i,j,k,l,s,m,n;                      
    cin>>a>>b>>c>>d>>e>>f>>g>>h>>i>>j>>k>>l;
    s=a*1+b*3+c*1+d*3+e*1+f*3+g*1+h*3+i*1+j*3+k*1+l*3;     
    m=s%10;   
    n=10-m;   
    if(n==10)       
        cout<<a<<b<<c<<d<<e<<f<<g<<h<<i<<j<<k<<l<<'0';   
    else 
        cout<<a<<b<<c<<d<<e<<f<<g<<h<<i<<j<<k<<l<<n;    
    return 0;  
    /********** End **********/
}

Level 4: Conversion of ISBN13 and ISBN10

mission details

The task of this level: Write a small program that can convert ISBN13 and ISBN10.

related information

Before 2007, the ISBN10 code was used for the International Standard Book Number, and the ISBN10 code consisted of 4 parts: publishing country or language code, publishing house code, book code, and check code. Moreover, the verification algorithm of ISBN10 is different from that of ISBN13.

algorithm

The calculation formula of the check code of 10-digit ISBN:

  • The rightmost digit of ISBN is the check digit, which is calculated by the formula from the first 9 digits. At the same time, it can be used to check whether the ISBN number is correct or not. A simple and easy-to-remember check digit calculation formula is given below, from left to right. 9 digits, each 1 digit is weighted and summed with 9 numbers from 10 to 2, then calculate the remainder of the sum and 11, and finally find the difference between 11 and the remainder, the difference is the check  ISBN: 7802253217  11-((7*10+8*9+0*8+2*7+2*6+5*5+3*4+2*3+1*2)%11)=7  code 10; The check code is X; if the difference is 11, the check code is 0.

The formula for calculating the check code of a 13-digit ISBN:

  • The rightmost digit of ISBN is the check digit, which is calculated by the formula from the first 12 digits. At the same time, it can be used to check whether the ISBN number is correct or not. A simple and easy-to-remember check digit calculation formula is given below, from left to right. 12 digits, each digit is weighted and summed with the two numbers 1 and 3 in turn, then calculates the remainder of the sum and 10, and finally finds the difference between 10 and the remainder, and the difference is the check code; For example: if the difference is  ISBN: 9787802253216  10-((9*1+7*3+8*1+7*3+8*1+0*3+2*1+2*3+5*1+3*3+2*1+1*3)%10)=6  , 10then The check code is 0.

programming requirements

Requirements: On the basis of the exercises in the previous level, input an ISBN13 code, first judge whether it is legal through the verification algorithm, and output the corresponding ISBN10 code (with no space in the middle), otherwise output "Error".

See the attached Wikipedia documentation for details and validation algorithms. Note : Because it is automatically judged by the machine, we agree that each number in the first four parts of the input is separated by a space.

The effect is as follows:

input: 9 7 8 7 3 0 2 2 1 8 9 7 5output:7302218974


Let's start your mission, I wish you success!

code:

#include <iostream>
using namespace std;

int main()
{

    //获取参数方式 cin
    //int x =0;
    //cin >> x;

    //结果输出使用 cout
    //cout<<"1";

     // 请在此添加你的代码
    /********** Begin ********/
    int a,b,c,d,e,f,g,h,i,j,k,l,m,S,N,M;           
    cin>>a>>b>>c>>d>>e>>f>>g>>h>>i>>j>>k>>l>>m;    
    int checksum = 10-(a*1+b*3+c*1+d*3+e*1+f*3+g*1+h*3+i*1+j*3+k*1+l*3)%10;    
    if(checksum == m)    
        {        
int finnal_bit = 11-(10*d+9*e+8*f+7*g+6*h+5*i+4*j+3*k+2*l)%11;        
        if(finnal_bit == 10)                    
        cout<<d<<e<<f<<g<<h<<i<<j<<k<<l<<'x';        
        else if(finnal_bit == 11)               
        cout<<d<<e<<f<<g<<h<<i<<j<<k<<l<<0;        
        else          
        cout<<d<<e<<f<<g<<h<<i<<j<<k<<l<<finnal_bit;          
        }    
    else    
    {       
    cout<<"Error";    
    }    
    return 0;
    /********** End **********/
}

Guess you like

Origin blog.csdn.net/weixin_62174595/article/details/127140441