Exam2002B

1.打印所有不超过 n( n < 256)的,其平方具有对称性质的数。如 11*11=121。

C++

#include <iostream>
#include <cmath>
using namespace std;

bool HuiWen(int n)
{
	n=n*n;
	int a[10]={0};
	int t=n;
	int i=0;
	while(t!=0)
	{
		a[i]=t%10;
		t/=10;
		i++;
	}
	i--;
	int j=0;
	while(i>=0)
	{
		t+=a[i]*pow(10,j);
		i--;
		j++;
	}
	if(t==n)
	{
		return true;
	}
	return false;
}

int main()
{
	for(int i=0;i<256;i++)
	{
		if(HuiWen(i))
		{
			cout<<i<<" ";
		}
	}
	return 0;
}

Go

package exam2002b

import "fmt"

//Huiwen is used to output the number of replies
func Huiwen() {
	var a [10]int
	for i := 0; i < 256; i++ {
		n := i * i
		t := n
		j := 0
		for ; t != 0; j++ {
			a[j] = t % 10
			t /= 10
		}
		t = 0
		for k := 0; k < j; k++ { //反向组数
			t = t*10 + a[k]
		}
		if t == n {
			fmt.Printf("%d ", i)
		}
	}
}
  1. 编写一个求斐波那契数列的递归函数,输入 n 值,使用该递归函数,输出如下图形。例如:当 n = 6 时。
               0
            0  1  1
         0  1  1  2  3
      0  1  1  2  3  5  8
   0  1  1  2  3  5  8  13  21
0  1  1  2  3  5  8  13  21  34  55

C++

#include <iostream>
using namespace std;

int Fibonacci(int j)
{
	if(j==1)
	{
		return 0;
	}
	else if(j==2)
	{
		return 1;
	}
	else
	{
		return Fibonacci(j-1)+Fibonacci(j-2);
	}
}

int main()
{
	int n,t;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		for(int k=1;k<=n-i;k++)
		{
			cout<<"   ";
		}
		for(int j=1;j<=2*i-1;j++)
		{
			t=Fibonacci(j);
			cout<<t<<"  "	;
		}
		cout<<endl;
	}
}

Go

package exam2002b

import "fmt"

//Fibonacci is used to calculate the number
func Fibonacci(j int) int {
	if j == 1 {
		return 0
	} else if j == 2 {
		return 1
	}
	return Fibonacci(j-1) + Fibonacci(j-2)
}

//Triangle is used to print Triangle of Fibonacci
func Triangle(n int) {
	for i := 1; i <= n; i++ {
		for k := 1; k <= n-i; k++ {
			fmt.Print("   ")
		}
		for j := 1; j <= 2*i-1; j++ {
			t := Fibonacci(j)
			fmt.Printf("%d  ", t)
		}
		fmt.Println()
	}
}
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy