/*Java 进位问题和一些感想

练习编程,不能只是得出结果,完成要求的功能就完事,要多学习优秀的算法思想,改进自己的算法。

题目是判断进位数,给两个数字,看这两数相加一共有几次进位。

图1是一个非常冗繁的代码,他的主体思想其实占不了几行,但是一些基础操作和舍近求远将其变得拖沓。

好比说:

1. 我想更不费脑子地取到两个数字的各位数,虽然一开始就想到了之前C语言中用的%取位法(在这里也是十分实用了,正好从低位到高位),但是还是绕了大弯子,先是把int转换成string,得到位数和各位数字,又转换回int,属于是用工具大于用算法了,十分不明智。

2. 上面提到,转化为string之后取长度len,这就又引入了复杂情况。两位数字位数不同?在for循环里会导致位数小的数-字符串取不出当前位上数字,还要用三个if判断,好不复杂!

3. 如果一开始就用%,不纠结两数字位数,用while循环取最后位,上面复杂通通没有。第二段代码是我从已提交作业中找来的行数最少的(21行,如下图3),我写的有45行,直接差了一倍,下面简单分析一下前者功能实现上的不同。

package homework3;
import java.util.*;
public class AB_IV {

	public static void main(String[] args) {
		Scanner s=new Scanner(System.in);
		while(true) {
			int i, j;
			long a = s.nextLong();
			long b = s.nextLong();
			int cnt=0, carry=0;
			int o, p;
			
			if(a==0&&b==0)
				break;
			String stra=Long.toString(a);
			String strb=Long.toString(b);
			int lena=stra.length();
			int lenb=strb.length();
			int len=(lena>lenb)?lena:lenb;
			for(i=0;i<len;i++) {
				if(lena-i-1>=0&&lenb-i-1>=0) {
					o=stra.charAt(lena-i-1)-'0';
					p=strb.charAt(lenb-i-1)-'0';
				}	
				else if(lena-i-1>=0&&lenb-i-1<0) {
					o=stra.charAt(lena-i-1)-'0';
					p=0;
				}
				else {
					o=0;
					p=strb.charAt(lenb-i-1)-'0';
				}
				if(carry+o+p>=10) {
					cnt++;
					carry=1;
				}
				else if(carry+o+p<10)
					carry=0;
			}
			if(cnt==0)
				System.out.println("No carry operation.");
			else if(cnt==1)
				System.out.println(cnt+" carry operation.");
			else if(cnt>1)
				System.out.println(cnt+" carry operations.");
		}
	}
}

4. 主要功能部分还是很相近的,都是取两数字当前位数与进位记数的和作为进位标准。

5. 进位数最大值为位数最高者位数dmax(也即判断是否进位的循环最多进行dmax次),最低位0

一开始我以为最大值是位数最低者位数在我的代码中,由于位数是提前求出的,(而非伴随while

循环,根本用不着位数),在进行是否进位的判断时,因循环须顾及最大循环次数,有时候会短位

数数字取不出当前位,根据这些不同情况写成 if-else,好麻烦! (如下图2)

而用while循环则只需要将while循环条件设置为(m!=0||n!=0), 即只要有一个数还没取完位数

就接着。

//就是这里
if(lena-i-1>=0&&lenb-i-1>=0) {
					o=stra.charAt(lena-i-1)-'0';
					p=strb.charAt(lenb-i-1)-'0';
				}	
				else if(lena-i-1>=0&&lenb-i-1<0) {
					o=stra.charAt(lena-i-1)-'0';
					p=0;
				}
				else {
					o=0;
					p=strb.charAt(lenb-i-1)-'0';
				}
import java.util.Map;
import java.util.Scanner;
import java.util.HashMap;
import java.util.Iterator;
class Main {
	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		while(scan.hasNext()) {
			int m=scan.nextInt(),n=scan.nextInt(), s=0,sum=0;
			if(m==0&&n==0) break;
			while(m!=0||n!=0) {
				if((m%10+n%10+s)>=10) {
					s=1;sum++;
				}
				else s=0;
				m/=10;n/=10;
			}
			String[] arr=new String[]{"No carry operation.","1 carry operation.","2 carry operations.","3 carry operations.","4 carry operations.","5 carry operations.","6 carry operations.","7 carry operations.","8 carry operations.","9 carry operations.","10 carry operations."};
			System.out.println(arr[sum]);
		}
		scan.close();
	}
}

6. 就先说这么多吧

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
补全代码如下: ``` #include <iostream> #include <cstring> using namespace std; #define MAX_LENGTH 100 //相关函数的原型 string add(string a, string b); string sub(string a, string b); int main() { string a, b, result; char op; cin >> a >> op >> b; if (op == '+') { result = add(a, b); } else { result = sub(a, b); } cout << result << endl; return 0; } //相关函数的具体定义 string add(string a, string b) { string result = ""; int len1 = a.length(), len2 = b.length(); int len = max(len1, len2); int carry = 0; for (int i = 0; i < len; i++) { int num1 = i < len1 ? a[len1 - 1 - i] - '0' : 0; int num2 = i < len2 ? b[len2 - 1 - i] - '0' : 0; int sum = num1 + num2 + carry; carry = sum / 10; result = char(sum % 10 + '0') + result; } if (carry > 0) { result = char(carry + '0') + result; } return result; } string sub(string a, string b) { string result = ""; int len1 = a.length(), len2 = b.length(); bool negative = false; if (len1 < len2 || (len1 == len2 && a < b)) { swap(a, b); swap(len1, len2); negative = true; } int borrow = 0; for (int i = 0; i < len1; i++) { int num1 = a[len1 - 1 - i] - '0'; int num2 = i < len2 ? b[len2 - 1 - i] - '0' : 0; int diff = num1 - num2 - borrow; if (diff < 0) { diff += 10; borrow = 1; } else { borrow = 0; } result = char(diff + '0') + result; } while (result.length() > 1 && result[0] == '0') { result.erase(0, 1); } if (negative) { result = '-' + result; } return result; } ``` 主要思路是先将字符串对齐,然后按位进行加或减,并且考虑进位和借位的情况。对于减法,需要注意被减数和减数的大小关系,并且最后要判断结果是否为负数并且去掉前导0。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李 董

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值