# Try to solve PTA 20211122 - Basic Practice of function, which involves high-precision addition and conversion from decimal system to 2-hexadecimal system. Students in need can have a look;

catalogue

1, Foreword

2, Example part

2.1 binary conversion

2.1.2 problem solving 2

2.2,   Function returns the inverse of an integer (20 minutes)

2.2.2   Problem solution

2.3,   Large integer A+B (10 points)

2.3.1 problem solving

two point four   Find the number of combinations (16 points)

2.4.1   Problem solution

3, Epilogue

# 1, Foreword

Hello, I'm Xia MI. Call me "Xia" or "Xiao Mi".

This time, we will introduce the routine PTA test every week.

Let's look at today's five questions.

# 2, Example part

## 2.1 binary conversion

Converts A decimal integer n (− 231 ≤ n ≤ 231 − 1) to A k (2 ≤ k ≤ 16) hexadecimal number. Note that 10 ~ 15 are represented by letters A, B, C, D, E and F respectively.

### Input format:

First, enter a positive integer T to represent the number of groups of test data, and then T groups of test data. Enter two integers n and k for each set of test data.

### Output format:

For each group of tests, first output n, then output a space, and finally output the corresponding k-ary number.

```4
5 3
123 16
0 5
-12 2
```

```5 12
123 7B
0 0
-12 -1100```

### 2.1.1 problem solving 1

Since 11 ~ hexadecimal conversion involves A, B, C, D, E and F letters, we need to use string to solve this problem:

First, we compile a universal binary conversion function, which is of type string.

We use t=n% hexadecimal number to obtain the last digit T, such as 3 for decimal 123;

n / = hexadecimal number    To destroy the last digit, such as decimal 123, destroy 3, and become 12;

Small tips: t is int type. To convert it to char type, you can do this

t+‘0’   For example, t=1, 1 + '0' = '1';

ans+= '1'；；

If t > 10, t-10 + 'A'; For example, 10 - 10 + 'A' = 'A';

ans+= 'A'；

```string intToA(int n,int radix)
{
string ans="";	//Define an empty string ans;
do{
int t=n%radix;	//Take the last digit of the decimal system, such as 3 in 123 of the decimal system;
if(t>=0&&t<=9)    ans+=t+'0';//If the last digit of the base is 0 ~ 9, add ans directly; T is int type, t+'0' is transformed into char type;
else ans+=t-10+'A';//If the last digit of the hexadecimal is greater than 9, it is converted to the corresponding hexadecimal representation, such as 10 to A,11 to B, etc;
n/=radix;//Destroy the last bit, e.g. 123 destroy 3 to 12;
}while(n!=0);//Use do while to ensure that ans="0" is returned when the input is 0;
reverse(ans.begin(),ans.end());//The subfunction of the string class reverses the elements of the string, such as 321 to 123;
return ans; //Returns ans;
}```

Then, it is specially judged that the input value is less than 0:

Just add a '-' sign before the absolute value result;

```#include<bits/stdc++.h>
using namespace std;
{
string ans="";	//Define an empty string ans;
do{
int t=n%radix;	//Take the last digit of the decimal system, such as 3 in 123 of the decimal system;
if(t>=0&&t<=9)    ans+=t+'0';//If the last digit of the base is 0 ~ 9, add ans directly; T is int type, t+'0' is transformed into char type;
else ans+=t-10+'A';//If the last digit of the hexadecimal is greater than 9, it is converted to the corresponding hexadecimal representation, such as 10 to A,11 to B, etc;
n/=radix;//Destroy the last bit, e.g. 123 destroy 3 to 12;
}while(n!=0);//Use do while to ensure that ans="0" is returned when the input is 0;
reverse(ans.begin(),ans.end());//The subfunction of the string class reverses the elements of the string, such as 321 to 123;
return ans; //Returns ans;
}
int main(){
int n=0;
scanf("%d",&n);
for(int i=0;i<n;i++){
int num,k;
scanf("%d %d",&num,&k);
if(num>=0)cout<<num<<" "<<intToA(num,k)<<endl;
else cout<<num<<" -"<<intToA(num*-1,k)<<endl;
}
}```

### 2.1.2 problem solving 2

Look at this function!

It is included in this header file #include < stdlib. H >

itoa(num,s,k)；// Num is the integer int or long, s is the character array, and K is the hexadecimal number to be converted;

The return type is string;

```#include<bits/stdc++.h>
using namespace std;
int main(){
int num,k;
char s;
cin>>num>>k;
cout<<itoa(num,s,k);//num is the integer int or long, s is the character array, and k is the hexadecimal number to be converted
}```

However, when it outputs A number such as base 11, the output 11 is' A 'instead of' A ';

So we write a small loop to replace case;

```#include<bits/stdc++.h>
using namespace std;
{
string ans="";
do{
if(t>=0&&t<=9)    ans+=t+'0';
else ans+=t-10+'a';
}while(n!=0);
reverse(ans.begin(),ans.end());
return ans;
}
int main(){
int n=0;
scanf("%d",&n);
for(int i=0;i<n;i++){
int num,k;
char s;
scanf("%d %d",&num,&k);

switch(k){
case 2:{
itoa(num,s,2);
printf("%s",s);
break;
}
case 3:{
itoa(num,s,3);
printf("%s",s);
break;
}
case 4:{
itoa(num,s,4);
printf("%s",s);
break;
}
case 5:{
itoa(num,s,5);
printf("%s",s);
break;
}

case 6:{
itoa(num,s,6);
printf("%s",s);
break;
}
case 7:{
itoa(num,s,7);
printf("%s",s);
break;
}
case 8:{
itoa(num,s,8);
printf("%s",s);
break;
}
case 9:{
itoa(num,s,9);
printf("%s",s);
break;
}

case 10:{
itoa(num,s,10);
printf("%s",s);
break;
}
case 11:{
itoa(num,s,11);
for(int i=0;s[i];i++){
if(s[i]>='a'&&s[i]<='z')s[i]+=('A'-'a');
}
printf("%s",s);
break;
}
case 12:{
itoa(num,s,12);
for(int i=0;s[i];i++){
if(s[i]>='a'&&s[i]<='z')s[i]+=('A'-'a');
}
printf("%s",s);
break;
}
case 13:{
itoa(num,s,13);
for(int i=0;s[i];i++){
if(s[i]>='a'&&s[i]<='z')s[i]+=('A'-'a');
}
printf("%s",s);
break;
}
case 14:{
itoa(num,s,14);
for(int i=0;s[i];i++){
if(s[i]>='a'&&s[i]<='z')s[i]+=('A'-'a');
}
printf("%s",s);
break;
}
case 15:{
itoa(num,s,15);
for(int i=0;s[i];i++){
if(s[i]>='a'&&s[i]<='z')s[i]+=('A'-'a');
}
printf("%s",s);
break;
}
case 16:{
itoa(num,s,16);
for(int i=0;s[i];i++){
if(s[i]>='a'&&s[i]<='z')s[i]+=('A'-'a');
}
printf("%s",s);
break;
}
}
}
}```

## 2.2,   Function returns the inverse of an integer (20 minutes)

Write a function that returns the inverse of a formal parameter (a positive integer). The main function is to input an integer N and output the inverse number of N.

### Input example:

`21000`

No blank lines at the end

### Output example:

`12`

No blank lines at the end

### Input example:

`1234`

No blank lines at the end

### Output example:

`4321`

No blank lines at the end

### 2.2.2   Problem solution

```#include<bits/stdc++.h>
using namespace std;
int rev(int x){
int a,i=0,sum=0;
while(x!=0)
{
a[i]=x%10;
i++;
x/=10;
}
for(int j=0;j<i;j++){
sum+=a[j];
sum*=10;
}
sum/=10;
return sum;
}

int main(){
int n;
cin>>n;
cout<<rev(n);
}```

Refer to the solution in 2.1. Note that because we record the array in reverse order, we can write it in this method

```for(int j=0;j<i;j++){
sum+=a[j];
sum*=10;
}
sum/=10;
return sum;```

Come and see vector

```#include<bits/stdc++.h>
using namespace std;
int main(){
long long int sum=0,n;
vector<int> a;
cin>>n;
while(n!=0){
a.push_back(n%10);
n/=10;
}
for(auto x:a){
sum+=x;
sum*=10;
}
cout<<sum/10;
} ```

After that, I will talk about vector for interested students;

## 2.3,   Large integer A+B (10 points)

Enter two integers A and B to find A + B.

### Input format:

First, enter A positive integer T to represent the number of groups of test data, and then T groups of test data. Enter 2 positive integers A and B for each test group. Integers may be large, but the number of bits per integer will not exceed 1000.

### Output format:

Output two lines of data for each group of tests; The first line outputs "Case #:", # represents the test group number, the second line outputs "A+B = Sum", and Sum represents the result of A+B. There is a blank line between each two sets of test data.

### Input example:

```2
1 2
88888888888888888888888 11111111111111111111111
```

### Output example:

```Case 1:
1 + 2 = 3

Case 2:
88888888888888888888888 + 11111111111111111111111 = 99999999999999999999999
```

HDOJ 1002

### 2.3.1 problem solving

```#include<bits/stdc++.h>
using namespace std;
string P  (string & num,string add){
int g=0;
string t=num;
}//If the length of num is less than the length of add, exchange the contents of num and add;
add= t+add;//Complete the length of add in the digital head to make it as long as num; If 123 is completed as 000123;
for(int i=len1-1;i>=0;i--){
num[i]=t%10+'0';//Calculate from the low position;
g=t/10;//Whether the record base is empty;
}
if(g!=0){
num.insert(0,string(1,(char)g+'0'));
}//Judge the system of the head;
return num;
}
int main(){
int n,n1=1,p=0;
cin>>n;
for(int i=0;i<n;i++){
string a,b,c;
cin>>a>>b;
c=a;
if(p!=0)cout<<endl<<endl;
p=1;
printf("Case %d:\n",n1);
n1++;
cout<<c<<" + "<<b<<" = "<<P(a,b);
}
} ```

You can use the same idea to write high-precision subtraction, high-precision multiplication and high-precision division;

help!

## two point four   Find the number of combinations (16 points)

This problem requires writing a program according to the formula Cnm = m!(n−m)!n! Calculate the combination number of M elements (m ≤ n) taken from n different elements.

It is recommended to define and call the function fact(n) to calculate n!, Where the type of n is int and the function type is double.

### Input format:

The input gives two positive integers m and n (m ≤ n) on one line, separated by spaces.

### Output format:

Output according to the format "result = calculation result of combination number". The title ensures that the result is within the range of double type.

### Input example:

`2 7`

No blank lines at the end

### Output example:

`result = 21`

No blank lines at the end

### 2.4.1   Problem solution

```#include<bits/stdc++.h>
using namespace std;
double fact(int n){
if(n>=1)return fact(n-1)*n;
else return 1;
}
int main(){
int n,m;
double sum=0;
cin>>m>>n;
sum=(fact(n))/((fact(m))*fact(n-m));
printf("result = %.lf",sum);
}```

Water problem, let's play by ourselves!