Uva10494 如果我们重返童年

udebug没有问题,但是上传OJ后显示输出格式错误。看了正确的代码,每一行后都有换行,包括最后一行。暂时不知道为什么

题:

“Oooooooooooooooh!If I could do the easy mathematics like my school days!!I can guarantee, that I’d not make any mistake this time!!”Says a smart university student!!But his teacher even smarter – “Ok! I’d assign you suchprojects in your software lab. Don’t be so sad.”“Really!!” – the students feels happy. And he feels sohappy that he cannot see the smile in his teacher’s face.The first project for the poor student was to make a calculatorthat can just perform the basic arithmetic operations.But like many other university students he doesn’t like todo any project by himself. He just wants to collect programsfrom here and there. As you are a friend of him, he asks youto write the program. But, you are also intelligent enough to tackle this kind of people. You agreed towrite only the (integer) division and mod (% in C/C++) operations for him.

Input

Input is a sequence of lines. Each line will contain an input number. One or more spaces. A sign(division or mod). Again spaces. And another input number. Both the input numbers are nonnegativeinteger. The first one may be arbitrarily long. The second number n will be in the range(0 < n < 231).

Output

A line for each input, each containing an integer. See the sample input and output. Output should notcontain any extra space.

代码

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
//#define LOCAL
#define MAXI 1001 
int getLen(int divs){
	int n=0;
	while(divs!=0){
		divs/=10;
		n++;
	}
	return n;
}
long long getNum(int div[],int start,int len){
	long long num=0,i;
	for(i=0;len>0;len--,i++){
		num+=div[start+i]*pow(10.0,(double)(len-1));
	}
	return num;
}

int main(){
#ifdef LOCAL
	freopen("data.in","r",stdin);
	freopen("data.out","w",stdout);
#endif
	int div[MAXI],res[MAXI],divs,divlen=0,divslen,reslen=0,resflag=0,firstflag=0,lineflag=0;
	int i,divi=0;
	long long mod,tempnum=0;
	char tempc,opr;
	while(scanf("%c",&tempc)!=EOF){
		if(isdigit(tempc)){
			div[divlen++]=(int)(tempc-'0');
		}else if(tempc!=' '&&divlen){
			
			opr=tempc;
			//numflag=2;
			scanf("%d",&divs);
			
			divslen=getLen(divs);
//			for(i=0;i<divlen;i++){
//					printf("%d",div[i]);
//			}
//			printf("%c%d(%d)=",opr,divs,divslen);
			//
			if(divlen<divslen) {
				res[reslen++]=0;
				mod=getNum(div,0,divlen);
			}else{
				while(divi<divlen){
//				printf("divi:%d ",divi);
				if(firstflag==0){//first第一次取被除数 
					tempnum=getNum(div,divi,divslen);
//					printf("len%d-new:%I64d ",divslen,getNum(div,divi,divslen)) ;
					divi+=divslen;
					firstflag=1;
				}else{
					tempnum=getNum(div,divi,1)+mod*10;
//					printf("lastmod:%I64d divlen%d-new:%I64d ",mod*10,1,getNum(div,divi,1)) ;
					divi++;
				}
				res[reslen]=(int)floor(tempnum/(double)divs);
				mod=tempnum%divs;
//				modlen=getLen(mod);
//				printf("tempnum:%I64d res:%d mod:%I64d\n",tempnum,res[reslen],mod);
				reslen++;
				}	
			}
				//
//			if(!lineflag){
//				lineflag=1;
//			}else{
//				printf("\n");
//			}
			if(opr=='/'){
				resflag=0;
				while((res[resflag]==0)&&(resflag<reslen-1))resflag++;
				for(i=resflag;i<reslen;i++){
				    printf("%d",res[i]);
				}
					printf("\n");
			} else{
				printf("%I64d",mod);
				printf("\n");
			}
			memset(div,0,sizeof(div));
			memset(res,0,sizeof(res));
			divlen=0;
			reslen=0;
			//numflag=0;
			resflag=0;
			firstflag=0;
			tempnum=0;
			divi=0;
		}
	} 
	return 0;
}

上传后结果


猜你喜欢

转载自blog.csdn.net/lagoon_lala/article/details/80217849