字符串-10361 Automatic Poetry

題目大意:

s1<s2>s3<s4>s5

c...

轉化爲:

s1s2s3s4s5

cs4s3s2s5

解題過程:開始時WA2次,而且沒找到原因。。。只能歸結於程序寫得太繁瑣,可能某個地方輸出不對了,後來改成平鋪直敘的寫法,一次AC。

注意:

1、正確代碼中使用了fstream庫函數

2、在使用cin.getline()時,連續使用cin.getline,轉行會統一處理,但是當從cin轉到cin.getline()時,需要清除緩衝區裏的轉行

正確代碼:

# include <cstdio>
# include <ctime>
# include <cmath>
# include <iostream>
# include <fstream>
# include <cstring>
#define fin cin
#define fout cout

using namespace std;

//ifstream fin("in.txt");
//ofstream fout("out.txt");

char l1[200],l2[200];
char s1[200],s2[200],s3[200],s4[200],s5[200];
	
int main()
{
	int n;
	fin>>n;
	fin.getline(l1,200);//清除緩衝區中的換行 
	
	while(n){
		n--;
		strcpy(l1,"");
		strcpy(l2,"");
		strcpy(s1,"");
		strcpy(s2,"");
		strcpy(s3,"");
		strcpy(s4,"");
		strcpy(s5,"");
		fin.getline(l1,200); 
		fin.getline(l2,200);
		int times;
		int i,j;
		for(i=0;i<strlen(l1);i++){
			if(l1[i]=='<') break;
			s1[i]=l1[i];
		}
		s1[i++]='\0';
		for(j=0;i<strlen(l1);i++,j++){
			if(l1[i]=='>') break;
			s2[j]=l1[i];
		}
		s2[j]='\0';
		i++;
		for(j=0;i<strlen(l1);i++,j++){
			if(l1[i]=='<') break;
			s3[j]=l1[i];
		}
		s3[j]='\0';
		i++;
		for(j=0;i<strlen(l1);i++,j++){
			if(l1[i]=='>') break;
			s4[j]=l1[i];
		}
		s4[j]='\0';
		i++;
		for(j=0;i<strlen(l1);i++,j++){
			s5[j]=l1[i];
		}
		s5[j]='\0';
	/* 
		fout<<"s1= "<<s1<<endl;
		fout<<"s2= "<<s2<<endl;
		fout<<"s3= "<<s3<<endl;
		fout<<"s4= "<<s4<<endl;
     	        fout<<"s5= "<<s5<<endl;
		fout<<"end ABCD"<<endl<<endl;
//*/
		fout<<s1<<s2<<s3<<s4<<s5<<endl;
		for(int j=0;j<strlen(l2);j++){
			if(l2[j]=='.') break;
			fout<<l2[j];
		}
		fout<<s4<<s3<<s2<<s5<<endl;
	}
	
//	fout<<clock()*1.000/CLOCKS_PER_SEC<<endl;
	return 0;
 } 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章