閱讀855 返回首頁    go 阿裏雲 go 技術社區[雲棲]


長噓一口氣。。。

#include "stdio.h"
#include "string.h"

const int Len=102;

typedef struct
{
	char name[Len];
	char value[Len];
	int flag;
}Keys;


Keys oldstr[Len],newstr[Len];


int deal(Keys *str)
{
	int count=0;
	char ch;
	int i=0;													//i 字母計數指針
	
	while(ch!='}')
	{
		scanf("%c",&ch);
		if(ch<='z' && ch>='a')
			str[count].name[i++]=ch;

		else if(ch==':')
			i=0;

		else if(ch<='9' && ch>='0')
			str[count].value[i++]=ch;

		else if(ch==',')
		{
			i=0;
			count++;
		}
	}
	
	return count+1;
}



void testprint(Keys *str,int n)
{
	int i;
	for(i=0;i<n;i++)
	{
		printf("%s.",str[i].name);
		printf("%s ",str[i].value);
	}

	printf("\n");
}


int main()
{
	int t;
	scanf("%d",&t);
	
	int i,j;
	int counto,countn;
	int sum;
	while(t--)
	{
		//init
		for(i=0;i<Len;i++)
		{
			for(j=0;j<Len;j++)
			{
				oldstr[i].name[j]='\0';
				oldstr[i].value[j]='\0';
				newstr[i].name[j]='\0';
				newstr[i].value[j]='\0';
				oldstr[i].flag=0;							//flag初始化為0,value不變置1,改變置2
				newstr[i].flag=0;
			}
		}
		
		counto=deal(oldstr);
		//testprint(oldstr,counto);

		countn=deal(newstr);
		//testprint(newstr,countn);

		for(i=0;i<counto;i++)
		{
			for(j=0;j<countn;j++)
			{
				if(strcmp(oldstr[i].name,newstr[j].name)==0)
				{
					if(strcmp(oldstr[i].value,newstr[j].value)==0)
					{
						oldstr[i].flag=1;
						newstr[j].flag=1;
					}

					else
					{
						oldstr[i].flag=2;
						newstr[j].flag=2;
					}
				}
			}
		}

		//搜索old串
		for(i=0;i<counto;i++)
			if(oldstr[i].flag!=1)
				break;

		for(j=0;j<countn;j++)
			if(newstr[j].flag!=1)
				break;

		if(i==counto && j==countn)
			printf("No changes\n");

		else
			//printf("have\n");
		{
			sum=0;
			for(i=0;i<countn;i++)
			{
				if(newstr[i].flag==0)
				{
					sum++;
					if(sum==1)
						printf("+");
					printf("%s ",newstr[i].name);
				}

				if(sum!=0 && i==countn-1)
					printf("\n");
			}

			sum=0;
			for(i=0;i<counto;i++)
			{
				if(oldstr[i].flag==0)
				{
					sum++;
					if(sum==1)
						printf("-");
					printf("%s ",oldstr[i].name);
				}

				if(sum!=0 && i==countn-1)
					printf("\n");
			}

			sum=0;
			for(i=0;i<counto;i++)
			{
				if(oldstr[i].flag==2)
				{
					sum++;
					if(sum==1)
						printf("*");
					printf("%s ",oldstr[i].name);
				}

				if(sum!=0 && i==countn-1)
					printf("\n");
			}
		}

		printf("\n");
	}
	
	return 0;
}

現場賽的C題終於寫出來了。。。終於感覺好點了,雖然打碼不一定精簡,不過也算解了心結了,就因為這題沒得獎啊。。。花了1個多小時。。。



最後更新:2017-04-02 15:15:07

  上一篇:go android關於實現EditText中加多行下劃線的的一種方法
  下一篇:go SQL從基礎到高級