ACM_杨延玺
代码:
#include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <vector> #include <queue> #include <stack> #include <map> #include <string> #include <algorithm> using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char** argv) { int n; cin>>n; getchar(); while(n--){ char input[101]; gets(input); int length=strlen(input); for(int i=0;i<length;i++){ printf("%c",(input[i]>=95)?(input[i]-32):(input[i]+32)); } printf("\n"); } return 0; }优秀代码: 01. #include<stdio.h> 02. int main() 03. { 04. int a,b,c,n; 05. char x; 06. scanf("%d",&n); 07. getchar(); 08. while(n--) 09. { 10. while(scanf("%c",&x)&&x!='\n') 11. { 12. if(x>=97&&x<=122) printf("%c",x-32); 13. else if(x<=90&&x>=64) printf("%c",x+32); 14. } 15. printf("\n"); 16. } 17. } 对比分析:优秀代码是一个字符一个字符的进行处理,这样一方面节省了直接将一行存放在一个字符数组中所用的空间,另一方面也节省了求字符串长度的时间。
但我改了没效果,时间和大小和没改一样啊。
int main(int argc, char** argv) { int n; cin>>n; getchar(); while(n--){ char c=getchar(); while(c != '\n'){ printf("%c",(c>=95)?(c-32):(c+32)); c=getchar(); } printf("\n"); } return 0; }