SXKDZ
  • Home
  • About Me
  • Links
Subscribe
Tagged

Arithmetic Sequence

A collection of 1 post

Programming

趣题:不用乘除法、循环、分支语句计算等差数列的和

2老师分享了一道有趣的题目: 求\( 1+2+\dots+n \),要求不能使用乘除法、for、while、if else和switch case等关键字以及条件判断运算符 (A ? B : C)。 一开始看到这个题确实是蒙的,只想到内嵌asm然后可以通过jmp语句实现跳转。 经过samhjn的提醒,可以使用递归,但是还是不会解决跳转的问题。继续提醒在这里需要利用短路求值的特性,以下代码来自swx: #include <iostream> using namespace std; int sum = 0; bool calc(int i) { sum += i; return i && calc(i - 1); // when i reaches 0

  • SXKDZ
SXKDZ Jul 13, 2016 • 1 min read
SXKDZ © 2023
Powered by Ghost