close

UVa  11192 - Group Reverse

廢話不多說,這個題目的意思就是1.你要知道這字串有幾個字2.它要分成幾組,最後3.每一組字把它反轉(倒序輸出)。

我寫的程式需要注意的,只有scanf("%d", &a) != EOF和strlen(str)。

前者是判斷有沒有輸入, by the way,EOF是-1,也就是-1這個值,主要是處理程式的錯誤,詳細請見其他參考。

後者是算出這個字串有多長,10個字,length就是10,且使用strlen這個函式,必須include一個<string.h>函式,才能使用。

程式碼 :

#include <stdio.h>
#include <string.h>
using namespace std;

int main(){
    int a;
    char str[50]="";
    
    while(scanf("%d", &a) != EOF && a != 0)
    {
        scanf("%s", str);
        for(int i=0;i<a;i++)    //分組,第i組 
        {
            for(int j=strlen(str)/a-1;0<=j;j--)    //倒數(倒序)     strlen(str)/a為一組幾個字 
            {
                printf("%c",str[i*strlen(str)/a+j]);    // 
            }
        }
        printf("\n");
    }
}

// ex: 輸入5,有30個字,  意思為輸入30個字分為5組,一組6個字。反轉每組的6個字 。 

arrow
arrow
    全站熱搜

    Tars 發表在 痞客邦 留言(0) 人氣()