#28187: 請問大家我這樣的寫法如何修改?


ic231542211 (willie)

學校 : 不指定學校
編號 : 44051
來源 : [123.194.107.200]
最後登入時間 :
2022-09-15 15:14:37
g005. 倒置文章 (Inversion) -- TOI練習賽202105新手組第2題 | From: [123.194.107.200] | 發表日期 : 2021-11-17 21:44

#include<stdio.h>

#include<string.h>

int main(){

    char str[301];

    scanf("%s", &str);

    for(int i = 0; i < strlen(str); i++){

        //遇到 "+"

        if(str[i] == '+'){

            continue;

        }

        //遇到 "-"

        else if(str[i] == '-'){

            int temp = i + 1;

            for(int i = temp; i < strlen(str); i++){

                //遇到第二個"-"

                if(str[temp] == '-'){

                    for(int j = temp - 1; j > temp; j--){

                        printf("%c", str[j]);

                    }

                }

            }

        }

        //字串尾端(不含"+"和"-")

        else if(str[i] != '+' && str[i] != '-'){

            printf("%c", str[i]);

        }

    }

}

 

我目前只有對45%而已,不知道問題出在哪裡,請問大家我能從哪裡修改程式碼?

 
#28188: Re:請問大家我這樣的寫法如何修改?


ic231542211 (willie)

學校 : 不指定學校
編號 : 44051
來源 : [123.194.107.200]
最後登入時間 :
2022-09-15 15:14:37
g005. 倒置文章 (Inversion) -- TOI練習賽202105新手組第2題 | From: [123.194.107.200] | 發表日期 : 2021-11-17 22:13

 

現在我的程式碼改寫成這樣,但還是只對45%,請問我中間一段要做"反轉輸出"的程式碼寫法能如何修改?

#include<stdio.h>

#include<string.h>

int main(){

    char str[301];

    scanf("%s", &str);

    for(int i = 0; i < strlen(str); i++){

        //遇到 "+"

        if(str[i] == '+'){

            continue;

        }

        //遇到第一個 "-"

        else if(str[i] == '-'){

            int temp_1 = i + 1;

            for(int i = temp_1; i < strlen(str); i++){

                //遇到第二個"-"

                if(str[i] == '-'){

                int temp_2 = i - 1;

                    for(int j = temp_2; j > i; j--){

                        printf("%c", str[j]);

                    }

                }

            }

        }

        //字串尾端(不含"+"和"-")

        else if(str[i] != '+' && str[i] != '-'){

            printf("%c", str[i]);

        }

    }

}

 
#28190: Re:請問大家我這樣的寫法如何修改?


linlincaleb@gmail.com (臨末之頌)

學校 : 新北市立板橋高級中學
編號 : 132772
來源 : [111.248.111.135]
最後登入時間 :
2023-04-01 22:41:13
g005. 倒置文章 (Inversion) -- TOI練習賽202105新手組第2題 | From: [111.248.99.202] | 發表日期 : 2021-11-17 22:22

 

現在我的程式碼改寫成這樣,但還是只對45%,請問我中間一段要做"反轉輸出"的程式碼寫法能如何修改?

#include

#include

int main(){

    char str[301];

    scanf("%s", &str);

    for(int i = 0; i < strlen(str); i++){

        //遇到 "+"

        if(str[i] == '+'){

            continue;

        }

        //遇到第一個 "-"

        else if(str[i] == '-'){

            int temp_1 = i + 1;

            for(int i = temp_1; i < strlen(str); i++){

                //遇到第二個"-"

                if(str[i] == '-'){

                int temp_2 = i - 1;

                    for(int j = temp_2; j > i; j--){

                        printf("%c", str[j]);

                    }

                }

            }

        }

        //字串尾端(不含"+"和"-")

        else if(str[i] != '+' && str[i] != '-'){

            printf("%c", str[i]);

        }

    }

}

考慮一下---跟-+-的情況

 
ZeroJudge Forum