C语言timTam修改

我这里出现的问题是游戏规则上是讲当数字有特定的关系是得到的分数不同,然后优先选择得分高的公式呈现,比如当输入1 5 5 5 8 9时,会使用Rule match-3(5)然后得到score 36。但是我输入之后会使用Rule total,并且得分不是最高的。我发现我的问题应该出现在设置未知数上面,电脑默认a就是第一个数,我想要的是当出现像1 5 5 5 8 9 时,a可以显示成5而不是1,还有优先计算得分最高我好像也没有弄好,能不能帮我做1下更改。帮我编程1下。如有疑问联系我。

img

img

void main()
{
    int a,b,c,d,e,f;
    int score;
    scanf("%d %d %d %d %d %d",&a,&b,&c,&d,&e,&f);
    if(a>b||b>c||c>d||d>e||e>f){
       printf("Invalid input: 6 integers 1..9 in sorted order must be supplied.");
    }else if(a>10||b>10||c>10||d>10||e>10||f>10){
        printf("Invalid input: 6 integers 1..9 in sorted order must be supplied.");
    return;
}
    if(a==b && a==c && a==d && a==e && a==f){
        score = 6*a+27;    
        printf("Rule match-6(%d)-score%d.",a,score);
    }else if(a==b && a==c && a==d && a==e){
        score = 5*a+25;
        printf("Rule match-5(%d)-score%d.",a,score);
    }else if(a==b && a==c && a==d){
        score = 4*a+23;
        printf("Rule match-4(%d)-score%d.",a,score);
    }else if(a==b && a==c){
        score = 3*a+21;
        printf("Rule match-3(%d)-score%d.",a,score);
    }else if(a==b){
        score = 2*a+19;
        printf("Rule match-2(%d)-score%d.",a,score);
    }else if(a+1==b){
        score = 2*b+17;
        printf("Rule sequence-2(%d,%d)-score%d.",a,b,score);
    }else if(a+2==b+1&& a+2==c){
        score = 3*c+18;
        printf("Rule sequence-3(%d,%d,%d)-score%d.",a,b,c,score);
    }else if(a+3==b+2 && a+3==c+1 && a+3==d){
        score = 4*d+19;
        printf("Rule sequence-4(%d,%d,%d,%d)-score%d.",a,b,c,d,score);
    }else if(a+4==b+3 && a+4==c+2 && a+4==d+1 && a+4==e){
        score = 5*e+20;
        printf("Rule sequence-5(%d,%d,%d,%d,%d)-score%d.",a,b,c,d,e,score);
    }else if(a+5==b+4 && a+5==c+3 && a+5==d+2 && a+5==e+1 && a+5==f){
        score = 6*f+21;
        printf("Rule sequence-2(%d,%d,%d,%d,%d,%d)-score%d.",a,b,c,d,e,f,score);
    }else if(a+b==c){
        score = a+c+22;
        printf("Rule sum-2(%d+%d=%d) -score%d.",a,b,c,score);
    }else if(a+b+c==d){
        score = a+d+29;
        printf("Rule sum-3(%d+%d+%d=%d) -score%d.",a,b,c,d,score);
    }else if(a+b+c+d==e){
        score = a+e+38;
        printf("Rule sum-4(%d+%d+%d=%d=%d) -score%d.",a,b,c,d,e,score);
    }else if(a+b+c+d+e==f){
        score = a+f+49;
        printf("Rule sum-5(%d+%d+%d+%d+%d=%d) -score%d.",a,b,c,d,e,f,score);
    }else if(a+b==c && d+e==f){
        score = a+2*b+3*c+4*d+5*e+6*f;    
        printf("Rule tim-tam(%d+%d=%d,%d+%d=%d) -score%d.",a,b,c,d,e,f,score);
    }else{
        score = a+b+c+d+e+f;
        printf("Rule total -score %d.",score);
    }
    
    
}

你这代码差的也不是一星半点啊。中间的一些判断可不好做啊。Xa并不是第一个输入值的意思,是符合规则的第一个数的意思。必须逐个条件去找输入的数是否符合规则才行。

It was not difficult for you to write this program.

Firstly, we are talking about how to make rules in program without judging the number sequence

wheather is sorted.

img

You can make sure that the rules for number sequence marked in red rectangle should be judged only once.

For Example,

it is not only matched with(sequence-6) when the number sequence of input is '9 9 9 9 9 9';

but also matched with(the rules of sequence-5) when the number sequence of input is '9 9 9 9 9 9';

So that judge the sequence-6 firstly

it is matched with(sequence-5) when the number sequence of input is '8 9 9 9 9 9';

it is also matched with(sequence-5) when the number sequence of input is '9 9 9 9 9 10';

So that the rules for number sequence marked in green rectangle should be judged twice.

SO JUST WRITE THE RULES.

The code as follow is a part of program.



#pragma warning(disable:4996)
#include <algorithm>
#include <stdio.h>
using namespace std;
int match_6(int arrs[]) {
    bool flag = true;
    int a = 0;
    a = arrs[0];
    for (int i = 1; i < 6; i++) {
        if (arrs[i] == a) {
            continue;
        }
        else {
            flag = false;
            break;
        }
    }
    if (flag) {
        printf("Rule match-6(%d) - score %d.", a, 6 * a + 27);
        return 1;
    }
    else {
        return 0;
    }
}
int match_5(int arrs[]) {
    int a = 0; bool flag;
    for (int j = 0; j < 2; j++) {
        flag = true;
        a = arrs[j];
        for (int i = 1 + j; i < 4 + j; i++) {
            if (arrs[i] == a) {
                continue;
            }
            else {
                flag = false;
                break;
            }
        }
    }
    
    if (flag) {
        printf("Rule match-5(%d) - score %d.", a, 5 * a + 25);
        return 1;
    }
    else {
        return 0;
    }
}
int sequence_6(int arrs[]) {
    bool flag = true;
    int f = 0;
    f = arrs[5];
    for (int i = 0; i < 6; i++) {
        if (arrs[i] + 5 - i == f) {
            continue;
        }
        else {
            flag = false;
            break;
        }
    }
    if (flag) {
        printf("Rule sequence-6(%d) - score %d.", f, 6 * f + 21);
        return 1;
    }
    else {
        return 0;
    }
}
int tim_tam(int arrs[]) {
    bool flag = false;
    int a = -1, b = -1, c = -1, d = -1, e = -1, f = -1;
    int max = 0;
    do {
        if (arrs[0] + arrs[1] == arrs[2] && arrs[3] + arrs[4] == arrs[5]) {
            int sum = arrs[0] + 2 * arrs[1] + 3 * arrs[2] + 4 * arrs[3] + 5 * arrs[4] + 6 * arrs[5];
            max = max > sum ? max : sum;
            a = arrs[0];
            b = arrs[1];
            c = arrs[2];
            d = arrs[3];
            e = arrs[4];
            f = arrs[5];
            flag = true;
        }
    }
    while (next_permutation(arrs, arrs + 6));
    if (flag) {
        if (c > f) {
            swap(a, d);
            swap(b, e);
        }
        if (a > b) {
            swap(a, b);
        }
        if (d > e) {
            swap(d, e);
        }
        printf("Rule tim-tam(%d+%d=%d,%d+%d=%d) - score %d.", a, b, c, d, e, f, max);
    }
    return max;
}
int main() {
    int res = 0;
    // to define a sequence.
    int arrs[6];
    // to get the sequence.
    for (int i = 0; i < 6; i++) {
        scanf("%d", &arrs[i]);
    }
    if (!match_6(arrs)) {
        if (!match_5(arrs)) {

        }
    }
    if (!sequence_6(arrs)) {

    }
    if (!tim_tam(arrs)) {

    }
    return 0;
    

}

All of rules is shown in picture.You can finish it on your own.

Secondly,how to judge the input is a question.

Use string to get the line of the input;
Use atoi to convert the arrays of char to int.
You can use sort function to judge the number sequence of input wheather is sorted.
Enjoy it!