现在的位置: 首页 > 综合 > 正文

hdu 1228

2018年01月12日 ⁄ 综合 ⁄ 共 1974字 ⁄ 字号 评论关闭

背景;看了好久动态分布,感觉思维有点累了,就随便找了道水题练练,结果水了快两个小时,各种出错尴尬

学习:

1.别人用scanf读取字符串,用strcmp对比字符串,而我却用getchar一个又一个地处理了也算是把scanf函数读取字符串自己定义了一次。

2.strcmp:    实际上,字符串的比较是比较字符串中各对字符的ASCII码。首先比较两个串的第一个字符,若不相等,则停止比较并得出大于或小于的结果;如果相等就接着 比较第二个字符然后第三个字符等等。如果两上字符串前面的字符一直相等,像"disk"和"disks"   那样,   前四个字符都一样,   然后比较第 五个字符,   前一个字符串"disk"只剩下结束符'/0',后一个字符串"disks"剩下's','/0'的ASCII码小于's'的ASCII
码,所以得出了结果。因此无论两个字符串是什么样,strcmp函数最多比较到其中一个字符串遇到结束符'/0'为止,就能得出结果。

注意:字符串是数组类型而非简单类型,不能用关系运算进行大小比较。  

         if("ABC">"DEF")   /*错误的字符串比较*/

         if(strcmp("ABC","DEF")   /*正确的字符串比较*/(strcpy(字符串变量,字符串常量)

2.上次说好的每写一段就回头去检查一遍,没有用好,比如我写了Reada,忘了写zero的情况,是后来单步调试才发现的。

#include<stdio.h>
int Reada(void);/*函数读取a*/ 
int Readb(void);/*函数读取b*/ 
 int Reada(void)
 {
 	
 	int x,y;
 	char a,b;
 	a=getchar();
 	b=getchar();
	 	if(a=='o') x=1;
	 	else if(a=='e') x=8;
	 	else if(a=='n') x=9;
	 	else if(a=='z') x=0;
	 	else if(a=='t'&&b=='w') x=2;
	 	else if(a=='t'&&b=='h') x=3;
	 	else if(a=='f'&&b=='o') x=4;
	 	else if(a=='f'&&b=='i') x=5;
	 	else if(a=='s'&&b=='i') x=6;
	 	else x=7;
	 	while(getchar()!=' ');
	 	if((a=getchar())!='+')
	 	{
	        	b=getchar();
	 			if(a=='o') y=1;
			 	else if(a=='e') y=8;
			 	else if(a=='z') y=0;
			 	else if(a=='n') y=9;
			 	else if(a=='t'&&b=='w') y=2;
			 	else if(a=='t'&&b=='h') y=3;
			 	else if(a=='f'&&b=='o') y=4;
			 	else if(a=='f'&&b=='i') y=5;
			 	else if(a=='s'&&b=='i') y=6;
			 	else y=7;
			 	while(getchar()!='+');
			 	getchar();
			 	return x*10+y;
	 	}
	 	else 
		 {
		 	getchar();
		 return x;
		 
	     }

 } 
   int Readb(void)
 {
 	
 	int x,y;
 	char a,b;
 	a=getchar();
 	b=getchar();
	 	if(a=='o') x=1;
	 	else if(a=='z') x=0;
	 	else if(a=='e') x=8;
	 	else if(a=='n') x=9;
	 	else if(a=='t'&&b=='w') x=2;
	 	else if(a=='t'&&b=='h') x=3;
	 	else if(a=='f'&&b=='o') x=4;
	 	else if(a=='f'&&b=='i') x=5;
	 	else if(a=='s'&&b=='i') x=6;
	 	else x=7;
	 	while(getchar()!=' ');
	 	if((a=getchar())!='=')
	 	{
	        	b=getchar();
	 			if(a=='o') y=1;
			 	else if(a=='e') y=8;
			 	else if(a=='z') y=0;
			 	else if(a=='n') y=9;
			 	else if(a=='t'&&b=='w') y=2;
			 	else if(a=='t'&&b=='h') y=3;
			 	else if(a=='f'&&b=='o') y=4;
			 	else if(a=='f'&&b=='i') y=5;
			 	else if(a=='s'&&b=='i') y=6;
			 	else y=7;
			    while(getchar()!='=');
			 	getchar();
			 	return x*10+y;
	 	}
	 	else 
	 	{
	 		getchar();
	 		return x;
	 		
	 	}
 } 
 int main(void)
 {
 	int a,b;
l1: a=Reada();
	b=Readb();
	if(a==0&&b==0) ; /*a,b同时为0就停止读取*/ 
	else
	{
	printf("%d\n",a+b);	
	goto l1;
	}
 	return 0;
 }

【上篇】
【下篇】

抱歉!评论已关闭.