[PAT Basic Level] 1018~1022

1018. hammer scissors cloth

Topic analysis:

At first glance it is quite easy to implement but there are still some trouble. The question is no pit needs extra attention, but it needs careful.

In order to determine the outcome of the relatively simple relationship, first determine whether the two characters are equal, if the number is equal to the direct draw plus one; otherwise, need to be judged according to the situation.
I did not use this switch, the switch statement with the words of the judge in each case will inevitably lead to code tedious. In order to facilitate the determination of the outcome, to write a function, enter a character, the character which can win, you need only compare whether the player corresponding to the other characters of the gesture function returns a value equal to the art will be able to win or lose Of course this function itself is determined if needed, but the number of comparisons at most 2 times.
Meanwhile, in order to facilitate sorting, when the solution is not unique alphabetical order, can be stored directly in each number of array elements is set to win gesture alphabetically sorted , i.e. the number of the first element memory B win, the second memory C the number of victories, the third victory kept the number of J, this time directly by the number of victories decide whether you need to update the output point, no longer consider alphabetical order.

Source

#include <stdio.h>

struct result{
    int win=0,equal=0,lose=0;
};

char winGesture(char x);  //返回手势x能胜的手势
void winCount(char x,int *arr);  //统计胜利次数
char findBest(int* arr);  //返回获胜次数最多的手势,次数相同时按字母序最小的输出

int main()
{
    result player[2];
    int won[2][3]={{0,0,0},{0,0,0}};  //甲、乙各手势获胜次数,3个手势按 B C J排列
    char p1,p2;
    int caseNumber;
    scanf("%d",&caseNumber);
    while(caseNumber--){
        getchar();  //读取上一行换行符
        scanf("%c %c",&p1,&p2);
        if(p1==p2) player[0].equal=++player[1].equal;   //平局次数更新
        else if(winGesture(p2)==p1){  //若p2胜p1
            player[1].win++;
            player[0].lose++;
            winCount(p2,won[1]);  //对乙选手各手势获胜次数更新
        }
        else{  //否则甲胜
            player[0].win++;
            player[1].lose++;
            winCount(p1,won[0]);
        }
    }
    printf("%d %d %d\n",player[0].win,player[0].equal,player[0].lose);
    printf("%d %d %d\n",player[1].win,player[1].equal,player[1].lose);
    printf("%c %c",findBest(won[0]),findBest(won[1]));
    return 0;
}
char winGesture(char x)
{
    if(x=='B') return 'C';
    else if(x=='C') return 'J';
    else return 'B'; 
}
void winCount(char x,int*arr)
{
    if(x=='B') arr[0]++;
    else if(x=='C') arr[1]++;
    else arr[2]++;
}
char findBest(int* arr)
{
    int hi=0;
    if(arr[1]>arr[hi]) hi=1;
    if(arr[2]>arr[hi]) hi=2;
    
    if(hi==0) return 'B';
    else if(hi==1) return 'C';
    else return 'J';
}

1019. The digital black hole

Topic analysis:

This question has an important pit, outputs " - "and" = = Before and after the "space required number.
Ideas:
The requirements of the subject, the target body can be substantially determined, a write function, passing a value that the required output according to the subject an equation, or 6174 if the difference is 0 (the value of the incoming everybody is equal), it returns 0, otherwise returns the new poor. so while loop repeatedly calls this function until the return value is 0 can be.

Source

#include <stdio.h>

int subtraction(int num);  //返回传入参数按规则进行一轮计算后得到的差
int main()
{
    int testValue;
    scanf("%d",&testValue);
    while((testValue=subtraction(testValue))!=0)
    ;
    return 0;
}
int subtraction(int num)
{
    int sub[2][4];  //储存被减数和减数
    for(int i=0;i<4;++i){
        sub[0][i]=num%10;
        num/=10;
    }
    int j=1;
    while(j<4){
        int count=j;
        while(count&&sub[0][count]>sub[0][count-1]){  //按非升序排列
            int tmp=sub[0][count];  //互换
            sub[0][count]=sub[0][count-1];
            sub[0][count-1]=tmp;
            count--;
        }
        j++;
    }
    for(int i=0;i<4;++i)  //根据排好序的sub[0],逆置得到sub[1],非降序排列
        sub[1][i]=sub[0][3-i];
    int num1,num2;
    num1=sub[0][0]*1000+sub[0][1]*100+sub[0][2]*10+sub[0][3];
    num2=sub[1][0]*1000+sub[1][1]*100+sub[1][2]*10+sub[1][3];
    int diff=num1-num2;
    if(diff==0||diff==6174){  //若差为0,或6174,返回0,终止循环
        printf("%04d - %04d = %04d",num1,num2,diff);  //服了,到底时要加空格的,也不说。。
        return 0;
    }
    printf("%04d - %04d = %04d\n",num1,num2,diff);
    return diff;
}

1020. The moon cake

Topic analysis:

The actual subject of the request is in accordance with the weight of the unit value of each commodity to sell non-ascending order. The idea is relatively simple, but there are many details that need careful attention, after WA times, it gradually improved procedures.

Note: In addition to several types of moon cake can be stored int, double the other variables are needed, from the test situation, some of the test point is the point given double value. In addition, taking into account the need to supply an amount less than demand, to avoid entering an infinite loop (Test Point 3 is the case).

Source

#include <stdio.h>
struct moonCake{
    double storage;
    double totalPrice;
    double aveProfit;
};

int main()
{
    int classNum; //月饼种类数
    double marketDemand;   //市场需求量
    scanf("%d %lf",&classNum,&marketDemand);
    moonCake *warehouse=new moonCake[classNum];
 
    for(int i=0;i<classNum;++i)
        scanf("%lf",&warehouse[i].storage);
    for(int i=0;i<classNum;++i){
        scanf("%lf",&warehouse[i].totalPrice);
        warehouse[i].aveProfit=warehouse[i].totalPrice/warehouse[i].storage;
    }
    int j=1;
    while(j<classNum){   //按平均收益非升序排列
        int count=j;
        while(count&&warehouse[count].aveProfit>warehouse[count-1].aveProfit){
            moonCake tmp=warehouse[count];   
            warehouse[count]=warehouse[count-1]; 
            warehouse[count-1]=tmp;
            count--;
        }
        j++;
    }
    double totProfit=0;
    int i=0;
    while(i<classNum){  //很重要,可能出现需求大于库存的情况
        if(marketDemand==0) break;  //需求降为0,终止循环
        else if(marketDemand<warehouse[i].storage){ //市场需求量小与此类,更新收益后停止循环
            totProfit+=marketDemand*warehouse[i].aveProfit;
            break;
        }
        else{  //否则需求比当前种类储存量大
            totProfit+=warehouse[i].totalPrice;
            marketDemand-=warehouse[i].storage;
            i++;
        }
    }
    printf("%.2f",totProfit);
    delete []warehouse;
    return 0;
}

1021. Statistical digit

Topic analysis:

This question is no special considerations, there is no special data measuring point, relatively easy, when programming their attention can be smoothly AC.
Not more than 1000, it opened a bit length int array 1001, according to the characters read into an array. According to updated each digital digits of the number of occurrences. Finally, the number will not appear sequentially output to zero.

Source

#include <stdio.h>

int main()
{
 
    int num[1001];
    int count=0;  //记录N的位数
    int digit[10];
    for(int i=0;i<10;++i)
        digit[i]=0;
    while((num[count]=getchar())!=EOF){
        num[count]-='0';  //将ASCII码转换成数字
        count++;
    }

    for(int i=0;i<count;++i)
        digit[num[i]]++;
    int first=0;  //第一次输出标记
    for(int i=0;i<10;++i)
        if(digit[i]){ //若数字i出现次数不为0
            if(first) printf("\n");  //不是第一次输出,则先换行
            printf("%d:%d",i,digit[i]);
            first++;
        }
    return 0;
}

1022. D A + B in hexadecimal

Topic analysis:

This is the main problem is a simple application stack, using the LIFO stack characteristics, the result of the division into a short stack, then the output can. Here stack it requires only a very simple structure, create an array to a pointer. Also, according to the given data range subject easy to know, and may be stored int, decimal digits after the conversion is not more than 31.
It should be noted that, given the number of said only non-negative, then the input is the case of 0,0 needs to be taken into account.

Source

#include <stdio.h>

int main()
{
 
    int num[1001];
    int count=0;  //记录N的位数
    int digit[10];
    for(int i=0;i<10;++i)
        digit[i]=0;
    while((num[count]=getchar())!=EOF){
        num[count]-='0';  //将ASCII码转换成数字
        count++;
    }

    for(int i=0;i<count;++i)
        digit[num[i]]++;
    int first=0;  //第一次输出标记
    for(int i=0;i<10;++i)
        if(digit[i]){ //若数字i出现次数不为0
            if(first) printf("\n");  //不是第一次输出,则先换行
            printf("%d:%d",i,digit[i]);
            first++;
        }
    return 0;
}
Published 11 original articles · won praise 1 · views 50

Guess you like

Origin blog.csdn.net/weixin_44452361/article/details/104695141