题46:求1+2+3+...+n
【题目描述】求1+2+3+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int num1=sc.nextInt();
int num2=sc.nextInt();
System.out.println( add(num1,num2));
}
public static int add(int num1 ,int num2){
int sum ,carry;
do {
sum=num1^num2;
carry=(num1&num2)<<1;
num1=sum;
num2=carry;
}while (carry!=0);
return num1;
}
}