杭电ACM 1062
2014-12-11 21:33
239 查看
废话不说,先上题:
Total Submission(s): 17744 Accepted Submission(s): 6730
[align=left]Problem Description[/align]
Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.
[align=left]Input[/align]
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single line with several words. There will be at most 1000 characters in a line.
[align=left]Output[/align]
For each test case, you should output the text which is processed.
[align=left]Sample Input[/align]
3
olleh !dlrow
m'I morf .udh
I ekil .mca
[align=left]Sample Output[/align]
hello world!
I'm from hdu.
I like acm.
Hint
Remember to use getchar() to read '\n' after the interger T, then you may use gets() to read a line and process it.
这道题初看感觉不难,但是提交的时候 各种报PE错误 擦 无奈了
经过N次测试后 发现测试数据 各种数据之间有多个空格,数据起始可能有多个空格 基本解决好这两个问题就能提交了
先上未优化前的代码:
还有用数组的:
Text Reverse
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17744 Accepted Submission(s): 6730
[align=left]Problem Description[/align]
Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.
[align=left]Input[/align]
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single line with several words. There will be at most 1000 characters in a line.
[align=left]Output[/align]
For each test case, you should output the text which is processed.
[align=left]Sample Input[/align]
3
olleh !dlrow
m'I morf .udh
I ekil .mca
[align=left]Sample Output[/align]
hello world!
I'm from hdu.
I like acm.
Hint
Remember to use getchar() to read '\n' after the interger T, then you may use gets() to read a line and process it.
这道题初看感觉不难,但是提交的时候 各种报PE错误 擦 无奈了
经过N次测试后 发现测试数据 各种数据之间有多个空格,数据起始可能有多个空格 基本解决好这两个问题就能提交了
先上未优化前的代码:
#include <stdio.h> #include <string.h> char a[1002]; void print(char *q) { while((q+1)!=a) { printf("%c", *q--); if (*q==' ') {break;} } } int main() { int n,i; char *p; while(scanf("%d",&n)!=EOF) { getchar(); while(n--){ memset(a,'\0',sizeof(a)); gets(a); p=a; while(*p==' '){putchar(' ');p++;}; while(1) { while(*p!=' ') { if(*(p+1)=='\0'){print(p);break;} if (*(p+1)==' ') {print(p);putchar(' ');break;} p++; }p++; p++; if(*p=='\0')break; else while(*p==' '){putchar(' ');p++;}; } putchar('\n'); } } return 0; }
还有用数组的:
#include <stdio.h> int main() { int n,i,j,k; char array[1001],t; while(scanf("%d",&n)!=EOF) { getchar(); while(n--) { i=0; gets(array); while(array[i]!='\0') { if(array[i]==' ') { i++;continue; } j=i; while((array[j+1]!=' ')&&(array[j+1]!='\0')) j++; k=j; while(i<j) { t=array[i]; array[i]=array[j]; array[j]=t;i++;j--; } i=k+1; } puts(array); } } return 0; }
相关文章推荐
- 杭电acm刷题(3):1062,Text Reverse 标签: 杭电acm 2017-05-15 08:26 126人阅读 评论(0)
- 杭电acm刷题(3):1062,Text Reverse
- 杭电ACM 1062: Text Reverse
- 杭电ACM 1062: Text Reverse
- 杭电_ACM_Big Number
- 【ACM】杭电1144:Prerequisites
- 杭电ACM 第1000题
- 杭电ACM 1176题
- 杭电ACM 第2010题
- 杭电ACM 1219 AC Me
- 杭电ACM 第2026题
- 杭电ACM 第2044题
- 杭电ACM 第2061题
- 杭电ACM 第2088题
- 杭电ACM 2010 水仙花数
- 杭电 acm 2024 C语言合法标识符
- 杭电 ACM 1.2.2
- 杭电acm 4268 Alice and Bob set的运用
- 杭电 ACM 1012
- 杭电acm--2043