9-19笔试

今天笔试三道编程题记录一下

1.求的斐波那契数列的第n位值

比较简单通过递归实现

using System;

namespace 兔子数列
{
    class Program
    {
        static void Main(string[] args)
        {
            string str;
            int n=0;
            while (true)
            {
                str = Console.ReadLine();
                if (int.TryParse(str, out n))
                {
                    Console.WriteLine(get_FibonacciN(n));
                }
            }
        }
        public static int get_FibonacciN(int i)
        {
            if (i < 3)
                return 1;
            return get_FibonacciN(i - 1) + get_FibonacciN(i - 2);
        }
    }
}

2.顺时针打印矩阵

例如矩阵

4,4

1,2,3,4

5,6,7,8

9,10,11,12

13,14,15,16

打印结果是1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10

这个题在剑指offer上有,但是之前没看过哭考试的时候想到别的办法实现

首先考虑矩阵的行和列已知所以需要打印的个数也知道n*m

所以当时就想通过循环n*m次,每次打印一个数,然后就是要考虑每次打印的是哪个数呢

第一个数肯定是[0][0]然后后面的无非四种情况:行+1,行-1,列+1,列-1

using System;

namespace 顺时针打印数组
{
    class Program
    {
        static void Main(string[] args)
        {
            string str_readline;
            string []char_readlines;
            int n, m;
            str_readline = Console.ReadLine();
            char_readlines = str_readline.Split(' ');
            n = int.Parse(char_readlines[0]);
            m = int.Parse(char_readlines[1]);

            int [][] in_array = new int [n][];
            for(int i =0;i<n;i++)
            {
                in_array [i] = new int[m];
            }
            int readline =0;
            while (readline < n)
            {
                str_readline = Console.ReadLine();
                char_readlines = str_readline.Split(' ');
                for(int j=0;j<m;j++)
                {
                    in_array[readline][j] = int.Parse (char_readlines[j]);
                }
                readline++;
            }
            OutResult(in_array, n, m);
            Console.ReadLine();
        }
        public static void OutResult(int[][] a, int n, int m)
        {
            int row=0;
            int line=0;
            for (int i = 0; i < n * m; i++)
            {
                Console .Write (a[row][line]+" ");
                a[row][line]=0;
                if (line + 1 < m && a[row][line + 1] != 0 && !(row - 1 >= 0 && a[row - 1][line] != 0))
                { line++; }
                else if (row + 1 < n && a[row + 1][line] != 0)
                { row++; }
                else if (line - 1 >= 0 && a[row][line - 1] != 0)
                { line--; }
                else if (row - 1 >= 0 && a[row - 1][line] != 0)
                { row--; }
            }
        }
    }
}
等回学校了再研究下剑指offer上的做法。

3.有序数列中找出和为m的两个数.

时间太紧张了,当时就想出来个不太好的解法

依次遍历数列,每找到一个数后再从它开始看后面的数有没有和它加起来等于m的。

using System;

namespace 有序数列找两个数
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] a = { 1, 2, 3, 4, 5, 6, 7, 11, 13, 18 };
            Console.WriteLine("1, 2, 3, 4, 5, 6, 7, 11, 13, 18");
            int m = 0;
            while (true)
            {
                m = int.Parse(Console.ReadLine());
                for (int i = 0; i < a.Length; i++)
                {
                    if (isInArray(m - a[i], a, i + 1, a.Length - 1))
                    {
                        Console.WriteLine(a[i] + " "+ (m - a[i]));
                    }
                }
                Console.WriteLine("完成");
            }
        }
        public static bool isInArray(int n, int[] a, int start, int end)
        {
            if (start >= end)
                if (a[end] == n)
                    return true;
                else
                    return false;
            if (a[(end + start) / 2] == n)
                return true;
            if (a[(end + start) / 2] > n)
                return isInArray(n, a, start, (end + start) / 2 - 1);
            else
                return isInArray(n, a, (end + start) / 2 +1, end);

        }
    }
}
后来在网上看到一个比较快的方法,数组收尾两个标志向中间逼近,如果和大于m,就让尾部的左移,如果和小于m就让头部的右移。

 public static void A(int sum, int[] num, int start, int end)
        {
            int tmp;
            while (start < end)
            {
                tmp = num[start] + num[end];
                if (tmp == sum)
                {
                    Console.WriteLine(num[start] + " " + num[end]);
                    break;
                }
                else if (tmp < sum)
                {
                    start ++;
                }
                else
                {
                    end--;
                }
            }

        }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值