求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。

来源:互联网 发布:淘宝开店钱盾认证 编辑:程序博客网 时间:2024/05/18 03:44


题目来源: http://ac.jobdu.com/problem.php?pid=1506

题目描述:

求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。

输入:

输入可能包含多个测试样例。
对于每个测试案例,输入为一个整数n(1<= n<=100000)。

输出:

对应每个测试案例,
输出1+2+3+…+n的值。

样例输入:
35
样例输出:
615
//java
 
public static long multi(int n, int m){long result = 0;long temp = 0;boolean flag = (m&1)==1;boolean test;test=flag && ((result+=m) > 0)&&((n -= 1)>0);test=( n > 0) && ((m <<=1) > 0);
n >>= 1;
test=( n > 0) && (temp=multi(n, m)) != 0;return result+temp;}public static void main(String[] args) {Scanner scan = new Scanner(System.in);while(scan.hasNextInt()){int n=scan.nextInt();long answer = multi(n,n+1);answer >>=1 ;System.out.println(answer);}scan.close();}

</pre><pre class="java" style="white-space: pre-wrap;" name="code"><span id="_xhe_cursor">typedef unsigned long long UINT64;</span>

UINT64 multi(UINT64 n, UINT64 m, UINT64 *p){    ((n&1) == 1)&& ((*p +=m) && (n -= 1));     (n >0)&& ((m<<=1));
     (n >>=1);    (n>0) && multi(n,m,p);    return (*p);}
int main(){    int n;    while(cin >> n)    {        UINT64 pathCount=0;        multi(n,n+1,&pathCount);        pathCount >>=1;        cout << pathCount << endl;    }    return 0;}

0 0
原创粉丝点击