C语言 百炼成钢11

//题目31:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续
//判断第二个字母。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

//分析:通过输入的字母判定星期几,可以使用if()else

void main(){
    char str[7] = { 0 };
    scanf("%s",str);
    switch ((int)str[0])
    {
    case 102:
        //f
        printf("\n今天是星期五");
        break;
    case 109:
        //m
        printf("\n今天是星期一");
        break;
    case 115:
        //s
        if (str[1]==97)
        {
            printf("\n今天是星期六");
        }
        else{
            printf("\n今天是星期日");
        }
        break;
    case 116:
        //t
        if (str[2] == 101)
        {
            printf("\n今天是星期二");
        }
        else{
            printf("\n今天是星期四");
        }
        break;
    case 119:
        //w
        printf("\n今天是星期三");
        break;
    default:
        break;
    }
    system("pause");
}

 

 

 

//题目32:Press any key to change color, do you want to try it. Please hurry up!
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<Windows.h>

//分析:cmd指令color的值有2个十六进制构成,范围是0-f,Press any key说明按下任意键变色,借助getchar()函数,
//函数名:kbhit()功能及返回值: 检查当前是否有键盘输入,若有则返回一个非0值,否则返回0;用 法:int _kbhit(void);其头文件是conio.h


void main(){
    //定义时间类型
    time_t ts;
    //定义随机数种子
    srand((unsigned int)time(&ts));
    //定义cmd中color的值
    char strcolor[30] = { 0 };
    while (1){
        getchar();
        sprintf(strcolor, "color %x%x", (int)(rand() % 16), (int)(rand() % 16));
        system(strcolor);
        
    }
    system("pause");
}

 

 

//题目33:求100之内的素数 

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
//分析:写一个函数,判断该数是否是素数

int sushu(int num){
    int temp = (int)sqrt(num + 1);
    for (int i = 2; i <= temp; i++)
    {
        if (num%i==0)
        {
            return 0;
        }
    }
    return 1;
}

void main(){
    for (int i = 0; i < 100; i++)
    {
        if (i==0||i==1||i==2||i==3)
        {
            printf("%d\n", i);
        }
        else{
            if (sushu(i))
            {
                printf("%d\n", i);
            }
        }
    }
    system("pause");
}

 

posted on 2016-01-23 00:09  寒魔影  阅读(247)  评论(0编辑  收藏  举报

导航