不用数学运算求和

/**
 * 题目描述:
 * 编写一个程序,不使用任何数学运算完成两数相加*/
public class Main {
public static  int add1(int A,int B){
     int sum;
     int carry;
     do{
         sum=A^B;
         carry=(A&B)<<1;
         A=sum;
         B=carry;
     }while(B!=0);
     return A;
 }
public static void main(String[] args) {
 Scanner sc = new Scanner(System.in);
     int n = sc.nextInt();
     int e=sc.nextInt ();
 System.out.println (add1 ( n,e));
}
}
发布了129 篇原创文章 · 获赞 4 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/beststudent_/article/details/96467822