问题1
预测以下程序的输出。
C++
#include
using namespace std;
void fun(int x)
{
if(x > 0)
{
fun(--x);
cout << x <<" ";
fun(--x);
}
}
int main()
{
int a = 4;
fun(a);
return 0;
}
// This code is contributed by SHUBHAMSINGH10
C
#include
void fun(int x)
{
if(x > 0)
{
fun(--x);
printf("%d\t", x);
fun(--x);
}
}
int main()
{
int a = 4;
fun(a);
getchar();
return 0;
}
Java
import java.io.*;
class GFG {
static void fun(int x)
{
if(x > 0)
{
fun(--x);
System.out.print(x + " ");
fun(--x);
}
}
public static void main (String[] args)
{
int a = 4;
fun(a);
}
}
// This code is contributed by SHUBHAMSINGH10
Python3
def fun(x):
if(x > 0):
x -= 1
fun(x)
print(x , end=" ")
x -= 1
fun(x)
# Driver code
a = 4
fun(a)
# This code is contributed by SHUBHAMSINGH10
C#
using System;
class GFG{
static void fun(int x)
{
if(x > 0)
{
fun(--x);
Console.Write(x + " ");
fun(--x);
}
}
static public void Main ()
{
int a = 4;
fun(a);
}
}
// This code is contributed by SHUBHAMSINGH10
Javascript
C++
#include
using namespace std;
int fun(int a[],int n)
{
int x;
if(n == 1)
return a[0];
else
x = fun(a, n - 1);
if(x > a[n - 1])
return x;
else
return a[n - 1];
}
int main()
{
int arr[] = {12, 10, 30, 50, 100};
cout << " " << fun(arr, 5) <<" ";
getchar();
return 0;
}
// This code is contributed by shubhamsingh10
C
#include
int fun(int a[],int n)
{
int x;
if(n == 1)
return a[0];
else
x = fun(a, n-1);
if(x > a[n-1])
return x;
else
return a[n-1];
}
int main()
{
int arr[] = {12, 10, 30, 50, 100};
printf(" %d ", fun(arr, 5));
getchar();
return 0;
}
Java
import java.io.*;
class GFG {
static int fun(int a[],int n)
{
int x;
if(n == 1)
return a[0];
else
x = fun(a, n - 1);
if(x > a[n - 1])
return x;
else
return a[n - 1];
}
public static void main (String[] args)
{
int arr[] = {12, 10, 30, 50, 100};
System.out.println(" "+fun(arr, 5)+" ");
}
}
// This code is contributed by shubhamsingh10
Python3
def fun( a, n):
if(n == 1):
return a[0]
else:
x = fun(a, n - 1)
if(x > a[n - 1]):
return x
else:
return a[n - 1]
# Driver code
arr = [12, 10, 30, 50, 100]
print(fun(arr, 5))
# This code is contributed by shubhamsingh10
C#
using System;
public class GFG{
static int fun(int[] a,int n)
{
int x;
if(n == 1)
return a[0];
else
x = fun(a, n - 1);
if(x > a[n - 1])
return x;
else
return a[n - 1];
}
static public void Main ()
{
int[] arr = {12, 10, 30, 50, 100};
Console.Write(" "+fun(arr, 5)+" ");
}
}
// This code is contributed by shubhamsingh10
C++
#include
using namespace std;
int fun(int i)
{
if (i % 2) return (i++);
else return fun(fun(i - 1));
}
int main()
{
cout << " " << fun(200) << " ";
getchar();
return 0;
}
// This code is contributed by Shubhamsingh10
C
int fun(int i)
{
if ( i%2 ) return (i++);
else return fun(fun( i - 1 ));
}
int main()
{
printf(" %d ", fun(200));
getchar();
return 0;
}
Java
import java.io.*;
class GFG {
static int fun(int i)
{
if (i % 2 == 1) return (i++);
else return fun(fun(i - 1));
}
public static void main (String[] args) {
System.out.println(" " + fun(200) + " ");
}
}
// This code is contributed by Shubhamsingh10
Python3
def fun(i) :
if (i % 2 == 1) :
i += 1
return (i - 1)
else :
return fun(fun(i - 1))
print(fun(200))
# This code is contributed by divyeshrabadiya07
C#
using System;
class GFG{
static int fun(int i)
{
if (i % 2 == 1) return (i++);
else return fun(fun(i - 1));
}
// Driver code
static public void Main ()
{
Console.WriteLine(fun(200));
}
}
// This code is contributed by Shubhamsingh10
Javascript
输出:
0 1 2 0 3 0 1
fun(4);
/
fun(3), print(3), fun(2)(prints 0 1)
/
fun(2), print(2), fun(1)(prints 0)
/
fun(1), print(1), fun(0)(does nothing)
/
fun(0), print(0), fun(-1) (does nothing)
问题2
预测以下程序的输出。以下fun()通常做什么?
C++
#include
using namespace std;
int fun(int a[],int n)
{
int x;
if(n == 1)
return a[0];
else
x = fun(a, n - 1);
if(x > a[n - 1])
return x;
else
return a[n - 1];
}
int main()
{
int arr[] = {12, 10, 30, 50, 100};
cout << " " << fun(arr, 5) <<" ";
getchar();
return 0;
}
// This code is contributed by shubhamsingh10
C
#include
int fun(int a[],int n)
{
int x;
if(n == 1)
return a[0];
else
x = fun(a, n-1);
if(x > a[n-1])
return x;
else
return a[n-1];
}
int main()
{
int arr[] = {12, 10, 30, 50, 100};
printf(" %d ", fun(arr, 5));
getchar();
return 0;
}
Java
import java.io.*;
class GFG {
static int fun(int a[],int n)
{
int x;
if(n == 1)
return a[0];
else
x = fun(a, n - 1);
if(x > a[n - 1])
return x;
else
return a[n - 1];
}
public static void main (String[] args)
{
int arr[] = {12, 10, 30, 50, 100};
System.out.println(" "+fun(arr, 5)+" ");
}
}
// This code is contributed by shubhamsingh10
Python3
def fun( a, n):
if(n == 1):
return a[0]
else:
x = fun(a, n - 1)
if(x > a[n - 1]):
return x
else:
return a[n - 1]
# Driver code
arr = [12, 10, 30, 50, 100]
print(fun(arr, 5))
# This code is contributed by shubhamsingh10
C#
using System;
public class GFG{
static int fun(int[] a,int n)
{
int x;
if(n == 1)
return a[0];
else
x = fun(a, n - 1);
if(x > a[n - 1])
return x;
else
return a[n - 1];
}
static public void Main ()
{
int[] arr = {12, 10, 30, 50, 100};
Console.Write(" "+fun(arr, 5)+" ");
}
}
// This code is contributed by shubhamsingh10
输出:
100
fun()返回大小为n的输入数组a []中的最大值。
问题3
预测以下程序的输出。以下fun()通常做什么?
C++
#include
using namespace std;
int fun(int i)
{
if (i % 2) return (i++);
else return fun(fun(i - 1));
}
int main()
{
cout << " " << fun(200) << " ";
getchar();
return 0;
}
// This code is contributed by Shubhamsingh10
C
int fun(int i)
{
if ( i%2 ) return (i++);
else return fun(fun( i - 1 ));
}
int main()
{
printf(" %d ", fun(200));
getchar();
return 0;
}
Java
import java.io.*;
class GFG {
static int fun(int i)
{
if (i % 2 == 1) return (i++);
else return fun(fun(i - 1));
}
public static void main (String[] args) {
System.out.println(" " + fun(200) + " ");
}
}
// This code is contributed by Shubhamsingh10
Python3
def fun(i) :
if (i % 2 == 1) :
i += 1
return (i - 1)
else :
return fun(fun(i - 1))
print(fun(200))
# This code is contributed by divyeshrabadiya07
C#
using System;
class GFG{
static int fun(int i)
{
if (i % 2 == 1) return (i++);
else return fun(fun(i - 1));
}
// Driver code
static public void Main ()
{
Console.WriteLine(fun(200));
}
}
// This code is contributed by Shubhamsingh10
Java脚本
输出:
199