好的,以示例为目标:执行此操作的命令行应用程序:
倒计时.exe 7
打印 7 6 5 4 3 2 1
不允许任何形式的减法(包括使用减号)或字符串反转。
waaaaay 显然太容易了 :-) 答案概述(至少是原则)
- 通过添加和递归
- 通过使用模
- 通过推动和弹出,(也许是最明显的?)
- 通过使用溢出
- 通过反复试验(也许是最不明显的?)
好的,以示例为目标:执行此操作的命令行应用程序:
倒计时.exe 7
打印 7 6 5 4 3 2 1
不允许任何形式的减法(包括使用减号)或字符串反转。
waaaaay 显然太容易了 :-) 答案概述(至少是原则)
x = param;
while (x > 0) {
print x;
x = (x + param) mod (param + 1);
}
添加和递归怎么样?
public void Print(int i, int max) {
if ( i < max ) {
Print(i+1, max);
}
Console.Write(i);
Console.Write(" ");
}
public void Main(string[] args) {
int max = Int32.Parse(args[0]);
Print(1, max);
}
这是您错过的一种方法,反复试验:
import java.util.Random;
public class CountDown
{
public static void main(String[] args)
{
Random rand = new Random();
int currentNum = Integer.parseInt(args[0]);
while (currentNum != 0)
{
System.out.print(currentNum + " ");
int nextNum = 0;
while (nextNum + 1 != currentNum) {
nextNum = rand.nextInt(currentNum);
}
currentNum = nextNum;
}
}
}
将 1-7 压入堆栈。一个一个地弹出堆栈。打印 7-1。:)
使用 2 的恭维,毕竟这是计算机处理负数的方式。
int Negate(int i)
{
i = ~i; // invert bits
return i + 1; // and add 1
}
void Print(int max)
{
for( int i = max; i != 0; i += Negate(1) )
{
printf("%d ", i);
}
}
将数字添加到字符串缓冲区中。
String out = "";
for (int i = 0; i < parm; i++)
{
out = " " + (i+1) + out;
}
System.out.println(out);
c/c++,有点算术溢出:
void Print(int max)
{
for( int i = max; i > 0; i += 0xFFFFFFFF )
{
printf("%d ", i);
}
}
我注意到没有人发布最愚蠢的答案,所以我会继续分享它:
int main (int argc, char **argv) {
if ( ( argc < 1 ) || ( atoi(argv[1]) != 7 ) ) {
printf("Not supported.\n");
} else {
printf("7 6 5 4 3 2 1\n");
}
}
不要恨我:看到了吗?我承认这是愚蠢的。:)
使用舍入误差:
void Decrement(int& i)
{
double d = i * i;
d = d / (((double)i)+0.000001); // d ends up being just smaller than i
i = (int)d; // conversion back to an int rounds down.
}
void Print(int max)
{
for( int i = max; i > 0; Decrement(i) )
{
printf("%d ", i);
}
}
这并不难。使用模运算符。
for (int n = 7; n <= 49; n += 7) {
print n mod 8;
}
按位算术
常数空间,没有加法、减法、乘法、除法、模数或算术否定:
#include <iostream>
#include <stdlib.h>
int main( int argc, char **argv ) {
for ( unsigned int value = atoi( argv[ 1 ] ); value; ) {
std::cout << value << " ";
for ( unsigned int place = 1; place; place <<= 1 )
if ( value & place ) {
value &= ~place;
break;
} else
value |= place;
}
std::cout << std::endl;
}
一个python版本:
import sys
items = list(xrange(1, int(sys.argv[1])+1))
for i in xrange(len(items)):
print items.pop()
这是作弊吧?
#!/usr/bin/env python
def countdown(n):
for i in range(n):
print n
n = n + (n + ~n)
只是为了好玩,它的递归兄弟:
def tune_up(n):
print n
if n == 0:
return
else:
return tune_up(n + (n + ~n))
从包含从降序到您感兴趣的最大值的文件开始:
7 6 5 4 3 2 1
然后...这仅适用于 9999
#!/bin/sh
MAX_NUM=9999
if [ ! -e descendingnumbers.txt ]; then
seq -f%04.0f -s\ $MAX_NUM -1 1 > descendingnumbers.txt
fi
tail descendingnumbers.txt -c $[5 * $1]
Scala 中快速而肮脏的版本:
sealed abstract class Number
case class Elem(num: Number, value: Int) extends Number
case object Nil extends Number
var num: Number = Nil
for (i <- 1 until param)
num = Elem(num, i)
while (num != null)
num match {
case Elem(n, v) => {
System.out.print(v + " ")
num = n
}
case Nil => {
System.out.println("")
num = null
}
}
增加一个传递给 max_int 的有符号整数,然后将其“添加”到计数器......或者这是否考虑非法减法?
public void print (int i)
{
Console.Out.Write("{0} ", i);
int j = i;
while (j > 1)
{
int k = 1;
while (k+1 < j)
k++;
j = k;
Console.Out.Write("{0} ", k );
}
}
有点讨厌,但它确实有效
public class CountUp
{
public static void main(String[] args)
{
int n = Integer.parseInt(args[0]);
while (n != 0)
{
System.out.print(n + " ");
n = (int)(n + 0xffffffffL);
}
}
}
// count up until found the number. the previous number counted is
// the decremented value wanted.
void Decrement(int& i)
{
int theLastOneWas;
for( int isThisIt = 0; isThisIt < i; ++isThisIt )
{
theLastOneWas = isThisIt;
}
i = theLastOneWas;
}
void Print(int max)
{
for( int i = max; i > 0; Decrement(i) )
{
printf("%d ", i);
}
}
我们在打这个吗?
import sys
for n in reversed(range(int(sys.argv[1]))):print n+1,
#!/usr/bin/env ruby
ARGV[0].to_i.downto(1) do |n|
print "#{n} "
end
puts ''
哈斯克尔:
import System.Environment (getArgs)
func :: Integer -> [String]
func 0 = []
func n@(x+1) = show n:func x
main = putStrLn . unwords . func . read . head =<< getArgs
称为 n+k 模式的“功能”允许这样做:在两个数字相加时进行模式匹配。一般不使用。一个更惯用的方法是使用这个版本的 func:
func n = foldl (flip $ (:) . show) [] [1..n]
或者,每行一个数字:
import System.Environment (getArgs)
import Data.Traversable
main = foldrM (const . print) () . enumFromTo 1 . read . head =<< getArgs
这算不算?仅使用添加指令...
int _tmain(int argc, _TCHAR* argv[])
{
int x = 10;
__asm mov eax,x;
__asm mov ebx,0xFFFFFFFF;
while (x > 0)
{
__asm add eax,ebx;
__asm mov x,eax;
__asm push eax;
printf("%d ",x);
__asm pop eax;
}
return 0;
}
珀尔:
$n = $ARGV[0];
while ($n > 0) {
print "$n ";
$n = int($n * ($n / ($n+1)));
}
减法无论如何都是一种错觉
我喜欢 Dylan Bennett 的想法 - 简单、务实,而且它遵循 KISS 原则,恕我直言,这是我们在开发软件时应始终牢记的最重要概念之一。毕竟,我们编写代码主要是为了让其他人维护它,而不是让计算机读取它。Dylan 在旧 C 中的解决方案:
#include <stdio.h>
int main(void) {
int n;
for (n = 7; n <= 49; n += 7) {
printf("%d ", n % 8);
}
}
在 C 中,使用旋转内存块(注意,这不是我引以为豪的东西......):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_MAX 10
void rotate_array (int *array, int size) {
int tmp = array[size - 1];
memmove(array + 1, array, sizeof(int) * (size - 1));
array[0] = tmp;
}
int main (int argc, char **argv) {
int idx, max, tmp_array[MAX_MAX];
if (argc > 1) {
max = atoi(argv[1]);
if (max <= MAX_MAX) {
/* load the array */
for (idx = 0; idx < max; ++idx) {
tmp_array[idx] = idx + 1;
}
/* rotate, print, lather, rinse, repeat... */
for (idx = 0; idx < max; ++idx) {
rotate_array(tmp_array, max);
printf("%d ", tmp_array[0]);
}
printf("\n");
}
}
return 0;
}
以及将列表视为整数的常见 lisp 解决方案:
(defun foo (max)
(format t "~{~A~^ ~}~%"
(maplist (lambda (x) (length x)) (make-list max))))
把它变成可执行文件可能是最难的部分,留给读者作为练习。
从 7 开始倒数(使用递归,或者像这里一样,使用loop
and downto
):
(loop for n from 7 downto 1 do (print n))
或者,也许是一个更有趣的解决方案。使用复数,我们只需重复添加 i 平方:
(defun complex-decrement (n)
"Decrements N by adding i squared."
(+ n (expt (complex 0 1) 2)))
(loop for n = 7 then (complex-decrement n)
while (> n 0) do (print n))
我喜欢递归
function printCountDown(int x, int y) {
if ( y != x ) printCountDown(x, y++);
print y + " ";
}
你也可以使用乘法
function printNto1(int x) {
for(int y=x*(MAXINT*2+1);y<=(MAXINT*2+1);y++) {
print (y*(MAXINT*2+1)) + " ";
}
}
另一种 perl 版本可能是:
#!/usr/local/bin/perl 打印反向连接(" ",1 .. $ARGV[0]) 。"\n";
C:
char buf[2][50];
int buf_no, i;
buf_no = buf[0][0] = buf[1][0] = 0;
for (i = 1; i <= 7; ++i, buf_no = !buf_no)
sprintf(buf[buf_no], "%d %s", i, buf[!buf_no]);
printf(buf[!buf_no]);
PHP
<?=implode(",", array_reverse( range(1, $_GET['limit']) ) )?>
现在滥用字符串函数!
using System;
public class Countdown {
public static void Main(string[] args){
int start = 10;
string buffer;
if (args.Length > 0) start = int.Parse(args[0]);
buffer = "".PadRight(start, 'z');
while (buffer.Length > 0){
Console.Write(buffer.Length.ToString() + " ");
buffer = buffer.Substring(1);
}
Console.WriteLine();
}
}
另一个 Scala 实现
class Countdown(countFrom: Int, countTo: Int) {
def printListInReverse() = {
val standardCount = for (i <- countFrom to countTo) yield i
println(standardCount.reverse.mkString(" "))
}
}
加负1怎么样?
从 -7 开始计数,不要打印减号:
#!/usr/bin/env python
for i in range(-7, 0): print str(i)[1],
输出到临时字符串,然后反转它,然后反转单个数字:
string ret;
for(int i=0;i<atoi(argv[1]);i++)
ret += " " + itoa(i);
for(int i=0;i<ret.length()/2;i++)
exchange(ret[i],ret[ret.length()-i-1]);
for(const char* t=&ret[0];t&&strchr(t,' ');t=strchr(t,' '))
for(int i=0;i<(strchr(t,' ')-t)/2;i++)
exchange(t[i],t[strchr(t,' ')-t-1]);
printf(ret.c_str());