PTA rental (20 points)

Export GPLT

(Article Directory)

Topic restatement

7-8 Rental (20 points)
Below is a picture that was once popular on Sina Weibo:

Insert picture description here

Suddenly there was a cry for help on the Internet, asking how to break this. In fact, this code is very simple, the index array is the subscript of the arr array, index[0]=2 corresponds to arr[2]=1, index[1]=0 corresponds to arr[0]=8, index[2]=3 Corresponding to arr[3]=0, and so on... It is easy to get the phone number as 18013820100.

This question requires you to write a program to generate this code for any phone number-in fact, as long as the first two lines are generated, the content after that is unchanged.

Input format:

Enter a mobile phone number consisting of 11 digits in one line.

Output format:

Generate the first two lines of the code for the entered number, where the numbers in arr must be given in descending order.

Input sample:

18013820100

Sample output:

int[] arr = new int[]{8,3,2,1,0};
int[] index = new int[]{3,0,4,3,1,0,2,4,3,4,4};

Problem analysis and solution ideas

1. First, we use a string variable to catch the entered phone number.
2. Count which numbers appear in our phone number (array a).
3. According to the format order of the output sample, output from largest to smallest. At the same time, use another array c to hold the numbers that appeared in our phone number, also from largest to smallest.
4. After constructing c, compare each letter in the string s with c one by one to determine the subscript, and then output it.
5. Note: follow the output example "int[] arr = new int[]{8, 3,2,1,0};" There is no comma after the last number between output {}.
The idea is relatively simple, the blogger has just started to write the solution of the problem, and many places have been incompletely considered. I hope you can correct me!

code

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    
    
    string s;
    int a[10]={
    
    0,0,0,0,0,0,0,0,0,0},b[11],c[10];
    cin>>s;
    for(int i=0;i<11;i++)
    {
    
    
        if(s[i]=='0') a[0]=1;
        else if(s[i]=='1') a[1]=1;
        else if(s[i]=='2') a[2]=1;
        else if(s[i]=='3') a[3]=1;
        else if(s[i]=='4') a[4]=1;
        else if(s[i]=='5') a[5]=1;
        else if(s[i]=='6') a[6]=1;
        else if(s[i]=='7') a[7]=1;
        else if(s[i]=='8') a[8]=1;
        else if(s[i]=='9') a[9]=1;
    }
    int k=0,n=0;
    for(int l=0;l<10;l++)
        if(a[l]==1) n++;

    cout<<"int[] arr = new int[]{";
    for(int j=9;j>=0;j--)
    {
    
    if(a[j]==1&&k<n-1) {
    
    cout<<j<<",";c[k]=j;k++;}
    else if(a[j]==1){
    
    cout<<j;c[k]=j;k++;}
    }
    cout<<"};"<<endl;
    cout<<"int[] index = new int[]{";
    for(int p=0;p<s.length();p++)
        for(int t=0;t<k;t++)
        {
    
    
            if(s[p]-'0'==c[t]&&p<10) cout<<t<<",";
            else if(s[p]-'0'==c[t]) cout<<t;
        }
    cout<<"};"<<endl; 
}

Guess you like

Origin blog.csdn.net/weixin_44108271/article/details/109413183