斐波那契系列扩展

时间:2014-09-16 05:11:12

标签: c# sequence fibonacci

如何在C#中编写代码以查找字母总和

如果A = 0; B = 1,C = A + B,D = B + C,E = C + D ......

示例CD = 1 + 2 = 3,

我试过这种方式,输入是字符串,输出是字母的总和

using System;

public class Test
{
    public static (int output1)
    public static void Main(string input1)
    {
        // your code goes here
    }
}

1 个答案:

答案 0 :(得分:1)

不使用字典列表的答案

class Program
    {
        static void Main(string[] args)
        {
            string test = "abcdef";
            int sum = 0;  
            foreach (char c in test)
            {
                int letterNumber = char.ToUpper(c) - 64;  
                sum += rtnDegint(letterNumber);
            }  
            Console.WriteLine(sum);
        }

        int rtnDegint(int n)
        {
            int first = 0, second = 1, next = 0, c;  
            for (c = 0; c < n; c++)
            {
                if (c <= 1)
                    next = c;
                else
                {
                    next = first + second;
                    first = second;
                    second = next;
                }
            }
            return next;
        }
    }