+ 6
Challenge "no arrays"
Write a program in any language which reads 100 numbers from keyboard and then writes these numbers in reverse order. You are not allowed to use arrays, lists and other similar containers. As another challenge you are encouraged to use the least number of variables as possible. I did this challenge with just 3 variables.
12 Answers
+ 4
By this challenge, I meant not using advanced containers and advanced tools of programming. So please do it by elemetary tools
+ 4
But somehow you have to use the idea of a stack...
+ 4
I tried it in code blocks maybe I have missed something
+ 4
@kurwius I tried again and it works fine
+ 3
This is a nice academic exercise, but totally useless in practice as the recursion eats much more memory than what's saved by not using an array.
I haven't actually compiled this, forgive me if there are any syntax errors I've missed.
void f(int cnt)
{
int d;
scanf("%d", &d) ;
if (cnt <100)
f(cnt+1);
printf("%d\n", d);
}
void main(int argc, char *argv[])
{
f(1);
}
+ 3
@kurwius I tried your answer. It stops after reading 3 numbers
+ 3
This is an answer in java:
import java.util.Scanner;
public class HundredNumbers {
static Scanner scan=new Scanner(System.in);
public static void main(String[] args) {
String s="";
int beginIndex=0, endIndex=0;
for(int i=0;i<5;i++){
s=s+"#"+scan.nextLine();
}
beginIndex=endIndex=s.length();
for(int i=0; i<5; i++){
do {
beginIndex--;
}while(s.charAt(beginIndex-1)!='#');
System.out.println(Integer.parseInt(s.substring(beginIndex, endIndex)));
beginIndex=endIndex=beginIndex-1;
}
}
}
+ 3
@Matthew Li interesting! but you are using lists
+ 2
python
print(input('Enter 100 numbers:')[::-1])
+ 1
Can i use a stack?just offer 100 numbers in and poll them out. But one question is to implentation stack, I have to use Deque interface and LinkedList or ArrayDeque.