题解 | #汽水瓶#

汽水瓶

https://www.nowcoder.com/practice/fe298c55694f4ed39e256170ff2c205f

方法1 找规律
方法2 递归(注释为使用字典记录)
using System;
// using System.Collections.Generic;

namespace HJ22{
    class Solution{

        //方法一:找规律 3-2 4-2 5-2 6-3 7-3 8-4 9-4 ....

        public static void Main(){
            int n = 0;
            while((n = int.Parse(Console.ReadLine())) !=0){
                if(n != 0){
                    Console.WriteLine(n/2);
                }            
            }            
        }


        //方法二:递归 f(n)=n/3 + f(n%3 + n/3)
        
        public static void Main(){
            int n = 0;
            while((n = int.Parse(Console.ReadLine())) !=0){
                if(n != 0){
                    n = calNum(n);
                    Console.WriteLine(n);
                }    
            }    
        }
//         static Dictionary<int, int> map = new Dictionary<int, int>();
        public static int calNum(int num){
            int res;
            if(num == 1){
                return 0;            
            }else if(num == 2){
                return 1;
            }
            
//             if(map.ContainsKey(num)){
//                 map.TryGetValue(num, out res);
//                 return res;
//             }
            res = num/3 + calNum(num%3+num/3);
//             map.Add(num, res);         
            return res;                                 
        }
    }
}


全部评论

相关推荐

牛客583549203号:腾讯还好,况且实习而已,实习生流动性很大,属于正常现象,记得和HR委婉解释
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务