zoukankan      html  css  js  c++  java
  • 大正数相加

    #include<stdio.h> 
    #include<string.h> 
    int len;
    void plus(int a[],int b[])
    { 
    	int i;
    	len=a[0]>b[0]?a[0]:b[0]; 
    	for(i=1;i<=len;i++) 
    	{
    		a[i]+=b[i]; 
    		if(a[i]>=10) 
    		{ 
    			 a[i+1]++; 
    			 a[i]=a[i]%10;
    		} 
    	} 
    	if(a[len+1]!=0)
    		len++;//a[0]是加数的长度 a[0]=len; 
    }
    int main() 
    { 
    	int i;
    	char str1[401],str2[401];
    	int a[401]={0},b[401]={0},c[403]={0}; 
    	while(1) 
    	{ 
    		memset(str1,0,sizeof(str1));
    		memset(str2,0,sizeof(str2));
    		if(gets(str1)==NULL||gets(str2)==NULL)//不是EOF 
    			break;
    		a[0]=strlen(str1); 
    		for(i=a[0];i>=1;i--) 
    			a[i]=str1[a[0]-i]-'0';
    		/*
    		for(i=1;i<=a[0];i++)
    			printf("%d   ",a[i]);
    		putchar('\n');
    		*/
    		for(i=0;i<=a[0];i++) 
    			c[i]=a[i];
    		b[0]=strlen(str2); 
    		//逆序,也就是说,最低位在 a[1] 
    		for(i=b[0];i>=1;i--) 
    			b[i]=str2[b[0]-i]-'0';
    		plus(a,b); 
    		for(i=c[0];i>=1;i--)//用c[i]不用a[i]是因为a[i]已经变化 
    			printf("%d",c[i]); 
    		printf(" + "); 
    		for(i=b[0];i>=1;i--) 
    			printf("%d",b[i]);//b[i]未变,此处可以继续用,不用一开始设置临时变量
    		printf(" = ");
    		for(i=len;i>=1;i--)
    			printf("%d",a[i]); 
    		putchar('\n'); 
    		memset(a,0,sizeof(a));
    		memset(b,0,sizeof(b));
    		memset(c,0,sizeof(c));
    	}
    	return 0;
    } 
    				  
    
  • 相关阅读:
    【转】Testing, Thought, and Observations
    【转】Yet another software testing pyramid
    【转】Automated Testing and the Test Pyramid
    Environment setting up troubleshoot.
    [转] Spring相关
    流水账
    Abbreviation
    chrome中文本框样式问题
    Intellij IDEA 使用总结
    限制
  • 原文地址:https://www.cnblogs.com/hxsyl/p/2580541.html
Copyright © 2011-2022 走看看