代码改变世界

百度之星资格赛 E题 C++ 与Java

2012-05-29 15:27  javaspring  阅读(205)  评论(0编辑  收藏  举报

题意:中文题,不说了

解法:这道题我没有什么好的方法,就是单个判断,话说,这道题考虑的情况好多,wa了7次才过,各种纠结。。。

ac代码:

#include <iostream>
#include <string.h>
#include <string>
#include <cstdio>

using namespace std;

int main(){
	//freopen("1.txt","r",stdin);
	int cnt = 1;
	string ss;
	char ch[110];
	while(gets(ch)){
	  //scanf("%s",ch);
		//puts("ch");
	  int flag1 = 0,flag2 = 0,flag3 = 0;
	  int len = strlen(ch);
	  for(int i = 0;i < len;++i){
	     if(ch[i] == '_')
			 flag1 = 1;
		 if(flag1 && ch[i] >= 'A' && ch[i] <= 'Z')
			 flag2 = 1;
		 if(ch[i] == '_' && ch[i+1] == '_'){
		   flag3 = 1;
		   break;
		 }
		 if(flag1 == 1 && flag2 ==1){
		   flag3 = 1;
		   break;
		 }
		 if(!((ch[i] >= 'a' && ch[i] <= 'z') || (ch[i] >= 'A' && ch[i] <= 'Z') || (ch[i] == '_') )){
		   flag3 = 1;
		   break;
		 }
	  }
	  if(!(ch[0] >= 'a' && ch[0] <= 'z'))
		  flag3 = 1;
	  if(!((ch[len-1] >= 'a' && ch[len-1] <= 'z') || (ch[len-1] >= 'A' && ch[len-1] <= 'Z')))
		  flag3 = 1;
	 // printf("输出样例%d:\n",cnt++);
	  if(flag3 == 1){
	    puts("Error!");
		continue;
	  }
	  int b1 = 0,b2 = 0;
	  for(int i = 0; i < len;++i){
		  if(ch[i] == '_'){
		    b1 = 1;
			break;
		  }
	  }
	  if(!b1) b2 = 1;
	  if(b1 == 1){
		  for(int i = 0;i < len;++i){
			  if(ch[i] == '_'){
			    ch[i+1] = ch[i+1] - 32;
			  }
		  }
		  for(int i = 0;i < len;++i){
		    if(ch[i] == '_')
				continue;
			printf("%c",ch[i]);
		  }
		  printf("\n");
	  }
	  char c;
	  int flag4 = 0;
	  if(b2 == 1){
		  for(int i = 0;i < len;++i){
			  if(ch[i+1] >= 'A' && ch[i+1] <= 'Z'){
			     c = '_';
				 flag4 = 1;
				 ch[i+1] = ch[i+1] + 32;
			  }
			  printf("%c",ch[i]);
			  if(flag4){
			    printf("%c",c);
				flag4 = 0;
			  }
		  }
		  printf("\n");
	  }
	}
	return 0;
}