zoukankan      html  css  js  c++  java
  • 成绩转换

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    using namespace std;
    
    int main()
    {
    	int score;
    	while(~scanf("%d", &score))
    	{
    		if(score > 100 || score < 0)
    		{
    			printf("Score is error!");
    		}
    		else {
    			switch(score/10)
    			{
    				case 9 ... 10:
    //				case 9:
    					printf("A");
    					break;
    				case 8:
    					printf("B");
    					break;
    				case 7:
    					printf("C");
    					break;
    				case 6:
    					printf("D");
    					break;
    				case 0 ... 5:
    //				case 4:
    //				case 3:
    //				case 2:
    //				case 1:
    //				case 0:
    					printf("E");
    					break;
    					
    				default:
    					printf("Score is error!");
    			}
    		}
    		printf("
    ");
    	}
    	
    	return 0;
    }
    

    第一次遇见case 0 ... 5 这种写法, 这样写使得使用case写代码简化了不少, 0...5只能升序, 不能写成5...0

    一定要注意, 不能直接写一个default来处理其他情况, 加入输入负数的话, switch中得到了0, 则会输出E, 所以要在输入是判断输入的数字是否在0~100之间

    当然还有比较简洁的写法

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    using namespace std;
    
    int main()
    {
    	char map[12] = "EEEEEEDCBAA";
    	int score;
    	
    	while(~scanf("%d", &score))
    	{
    		if(score > 100 || score < 0)
    		{
    			printf("Score is error!
    ");
    		}
    		else
    		{
    			printf("%c
    ", map[score/10]);
    		}
    	}
    	
    	return 0;
    } 
    

      

  • 相关阅读:
    Java学习之--List和ArrayList
    Linux
    Linux
    Docker 容器管理
    Docker 镜像管理
    如何理解 AWS VPC
    Linux 内核版本
    Windows 快速切换到当前目录下的dos环境
    Git 整理
    TCP/IP
  • 原文地址:https://www.cnblogs.com/mjn1/p/11232305.html
Copyright © 2011-2022 走看看