zoukankan      html  css  js  c++  java
  • 正逆向思维-编程轶事-遁地龙卷风

    (0) 问题介绍

    商家下面有很多会员等级,等级的评定是根据用户的积分来决定的,假设等级信息如下

    等级:0   1    2   3

    积分:20  100  210  350

    会员A的积分为:300,小于最低等级为最低等级,大于最高等级为最高等级,其他的,大于等于等级积分才是当前等级。

    会员A的等级应为2,现在写一个程序来解决这个问题。

    (1)复杂的实现

    public static void main(String[] args)
    	{
    		ArrayList<Integer> sortList = new ArrayList<Integer>()
    		{
    			{
    				add(20);
    				add(100);
    				add(210);
    				add(350);
    			}
    		};
    		Integer value = 300,
    		size = sortList.size(),
    		level = 0;
    		boolean flag =false;
    		for(int i=0;i<size;i++)
    		{
    			if(sortList.get(i) > value)
    			{
    				if(i == 0)//小于最低等级的情况
    				{
    					level = 0;
    				}
    				else
    				{
    					level = i-1;		
    				}
    				flag = true;
    				break;		
    			}	
    		}
    		if(!flag)//大于最大等级的情况
    		{
    			level = size - 1;
    		}
    		System.out.println("他的等级是:"+level);
    	}
    

      

    (2)简单的实现

    public static void main(String[] args) 
    	{
    		ArrayList<Integer> sortList = new ArrayList<Integer>()
    		{
    			{
    				add(20);
    				add(100);
    				add(210);
    				add(350);
    			}
    		};
    		Integer value = 300,
    				size = sortList.size(),
    				level = 0;
    		
    		for(int i=1;i<size;i++)
    		{
    			if(sortList.get(i) <= value)
    			{
    				level = i;
    			}
    			else break;
    		}
    		System.out.println("他的等级是:"+level);
    	}
    

      

    (3) 感触

    有时候><=就代表了正向思维和逆向思维。

  • 相关阅读:
    面向对象的继承关系体现在数据结构上时,如何表示
    codeforces 584C Marina and Vasya
    codeforces 602A Two Bases
    LA 4329 PingPong
    codeforces 584B Kolya and Tanya
    codeforces 584A Olesya and Rodion
    codeforces 583B Robot's Task
    codeforces 583A Asphalting Roads
    codeforces 581C Developing Skills
    codeforces 581A Vasya the Hipster
  • 原文地址:https://www.cnblogs.com/resolvent/p/6081549.html
Copyright © 2011-2022 走看看