求解 这个题用c语言怎么做?

2025-05-02 08:01:48
推荐回答(3个)
回答1:

//#include "stdafx.h"//vc++6.0加上这一行.
#include "stdio.h"
#include "string.h"
int main(void){
    char a[9][201],tmp[21];
    int i,j,k,ln,T;
    while(1){
        printf("How many times to do(<10)?\nT=");
        if(scanf("%d",&T),T<10 && T>0)
            break;
        printf("Error, redo: ");
    }
    fflush(stdin);
    printf("Input %d string(s)...\n",T);
    for(i=0;i    printf("\n");
    for(i=0;i        printf("Case %d: ",i+1);
        for(ln=strlen(a[i]),j=0;j            sscanf(a[i]+j,"%s%n",tmp,&k);
            printf("%s ",strrev(tmp));
            j+=k;
        }
        printf("\n");
    }
    return 0;
}

回答2:

#include 
#include 

int DecomposeSentence(char source[],char target[][36]) {
int i = 0,j,n = 0;
while(source[i]) {
j = 0;
while(source[i] == ' ') ++i; // 滤除空格
while(source[i] && source[i] != ' ')
target[n][j++] = source[i++];
target[n][j] = '\0';
++n;
}
return n;
}

int main() {
char line[136],dest[100][36];
int T,n,i;
scanf("%d",&T);
while(T--) {
fflush(stdin);
gets(line);
n = DecomposeSentence(line,dest);
for(i = n - 1; i >= 0; --i)
printf("%s ",dest[i]);
printf("\n");
}
return 0;
}

回答3:

思路:

  1. 用gets读取整行

  2. 用strtok将句子的每个单词分开

  3. 逆序打印每个单词

具体代码实现如下:

#include 
#include 
#include 

int main()
{
    int t, i, j;
    char tmp[200+1] = {0};
    char *ptmp[200+1] = {NULL};
    
    scanf("%d\n", &t);
    
    for(j=0;j    {
        i = 0;
        gets(tmp);/* 获取整个句子 */ 
        ptmp[i++] = strtok(tmp, " ");
        while ((ptmp[i]=strtok(NULL, " "))!=NULL) i++;/* 以空格拆分单词 */ 
        printf("Case %d:", j);
        while(--i >= 0) /* 逆序打印 */ 
        {
            printf("%s%c", ptmp[i], i>0?' ':'\n');
        }
    }
    
    return 0;
}