switch case与while语句练习

switch case 选择

  • 假设用1,2。。。。7分别表示星期一。。。。星期天,现输入一个数字,输出对应的星期几。比如:输入3,则输出“星期三”
    #define _CRT_SECURE_NO_WARNINGS 1
    #include 
    int main()
    {
    	int day = 0;
    	scanf("%d", &day);
    	switch (day)
    	{
    		case 1:
    			printf("星期一\n");
    			break;
    		case 2:
    			printf("星期二\n");
    			break;
    		case 3:
    			printf("星期三\n");
    			break;
    		case 4:
    			printf("星期四\n");
    			break;
    		case 5:
    			printf("星期五\n");
    			break;
    		case 6:
    			printf("星期六\n");
    			break;
    		case 7:
    			printf("星期天\n");
    			break;
    	}
    	return 0;
    }

  • 从键盘上输入学生成绩,成绩等级如下:60分及其以上为通过,60分以下为不通过
    #define _CRT_SECURE_NO_WARNINGS 1
    #include 
    int main()
    {
    	int d = 0;
    	scanf("%d", &d);
    	int n;
    	n = d > 60 ? 0 : 1;
    	switch (n)
    	{
    		case 0:
    			printf("合格\n");
    			break;
    		case 1:
    			printf("不合格\n");
    			break;
    	}
    	return 0;
    }
  • 从键盘上输入学生成绩,成绩等级如下:
    90~100   “优”
    80~90   “良”
    70~80   “中”
    60~70   “合格”
    60以下   “不合格”
    #define _CRT_SECURE_NO_WARNINGS 1
    #include 
    int main()
    {
    	int d = 0;
    	scanf("%d", &d);
    	int n;
    	n = d > 60 ? 1: 0;
    	if (n)
    	{
    		d /= 10;
    		switch (d)
    		{
    		case 6:
    			printf("合格\n");
    			break;
    		case 7:
    			printf("中\n");
    			break;
    		case 8:
    			printf("良\n");
    			break;
    		case 9:
    			printf("优\n");
    			break;
    		case 10:
    			printf("优\n");
    			break;
    		}
    	}
    	else
    		printf("不合格");
    	return 0;
    }

while循环 

  1. 求1到10之间奇数的和与偶数的和
    #define _CRT_SECURE_NO_WARNINGS 1
    #include 
    int main()
    {
    	int a = 0, b = 0, c = 1, d;
    	printf("请输入一个数字求 1~这个数字 范围内的偶数和奇数的和\n");
    	scanf("%d", &d);
    	while (c <= d)
    	{
    		if (c % 2 == 0)
    			a = a + c;
    		else
    			b = b + c;
    		c++;
    	}
    	printf("偶数和是 %d,奇数和是 %d\n", a, b);
    	return 0;
    }

  2. 求1/2+1/4+1/6+……的和,最后一项的值小于0.0001为止。

    #define _CRT_SECURE_NO_WARNINGS 1
    #include 
    int main()
    {
    	int a = 2;
    	double sum = 0;
    	while (a <= 10002)
    	{
    		sum = sum + 1.0 / a;
    		a += 2;
    	}
    	printf("%lf\n", sum);
    	return 0;
    }
    
  3. 求和1/2-1/3+1/4-1/5+1/6……+1/100

    #define _CRT_SECURE_NO_WARNINGS 1
    #include 
    int main()
    {
        double b = 1.0, sum = 0, a;
        int i = 1;
       while(i <= 100)
        {
            a = b / i;
            sum += a;
            b = -b;
            i++;
        }
        printf("sum = %.2f\n", sum);
        return 0;
    }
  4. 、求10!(求10的阶乘)

    #define _CRT_SECURE_NO_WARNINGS 1
    #include 
    int main()                
    {                            
        int p = 1;
        int n;
        int i = 2;
        printf("请输入所求阶乘数:\n");
        scanf("%d", &n);
        while(i <= n) 
        {
            p = p * i;
            ++i;
        }
        printf("%d! = %d", n, p);
        return  0;
    }                              

你可能感兴趣的:(浴火重生,算法,蓝桥杯,c++)