学校作业练习

从键盘输入一个小于1000的整数,要求输出它的平方根(如果平方根不是整数,则输出整数部分)。要求在输入数据后先检查其是否小于1000,若不是,则要求重新输入。


#include <stdio.h>
#include <math.h>
#define M 1000
int main()
{
	int i; // integers less than M
	int k; // the integral part of the square root of i
	//
	// TODO
	//
    while(1)
	{
		scanf("%d",&i);
		if(i<1000&&i>=0)
		{
			k =(int) sqrt(i);
			break;
		}
		else
		printf("The input data does not meet the requirements, please re-enter an integer less than 1000 i: ");
	}
	printf("The integral part of the square root of %d is %d\n",i, k);
	
	return 0;
}

从键盘输入4个整数到a,b,c,d,输入的格式为a,b,c,d,然后从键盘输入字符'a'或'd',
要求交换这4个整数,如果输入的是字符'a', 按升序排列,使a<b<c<d,
如果输入的是字符'd', 按降序排列,使a>b>c> d

#include <stdio.h>


int main()
{
    int a, b, c, d;    // four integer
    int t;            // local variables used in the exchange of two integers
    char s;
    int n;
    //
    // TODO
    //
    scanf("%d,%d,%d,%d,%c",&a, &b, &c, &d, &s);
    printf("a = %d, b = %d, c = %d, d = %d, s = %c\n",a ,b, c, d, s);

    if(s=='a')
    {    
        
        if(a>b)
        {
            n = a;
            a = b;
            b = n;
        }
        if(a>c)
        {
            n = a;
            a = c;
            c = n;
        }
        if(a>d)
        {
            n = a;
            a = d;
            d = n;
        }
        if(b>c)
        {
            
            n = b;
            b = c;
            c = n;
            
        }
        if(b>d)
        {
            n = b;
            b = d;
            d = n;
        }
        if(c>d)
        {
            n = c;
            
            c = d;
            d = n;
        }
    }
    else if(s=='d')
    {
        if(d>c)
        {
            n = d;
            d = c;
            c = n;
        }
        if(d>b)
        {
            n = d;
            d = b;
            b = n;
        }
        if(d>a)
        {
            n = d;
            d = a;
            a = n;
        }
        if(c>b)
        {
            n = c;
            c = b;
            b = n;
        }
        if(c>a)
        {
            n = c;
            c = a;
            a = n;
        }
        if(b>a)
        {
            n = b;
            b = a;
            a = n;
        }

    }
    
    printf("The sorting results are as follows: \n");
    printf("%d %d %d %d",a, b, c, d);
    
    return 0;
}


输出所有的“水仙花数”,所谓的“水仙花数”是指一个3位数,其各位数字立方和等于该数本身。例如,153是一个水仙花数,因为153 = 13 + 53 + 33。

int main()
{

	//
	// TODO
	//
	int n;
	int a, b, c;
	for(n=100;n<1000;n++)
	{
	
		a = n/10/10;
		b = n/10%10;
		c = n%10;
		int d = a*a*a+b*b*b+c*c*c;
		if(n == d)
		{
			printf("%d\n",n);
		}
	}

	return 0;
}

(下面这个题是为了应付学校的刁钻作业验证机制才这样写的)

给出一个百分制成绩,要求输出成绩等级A,B,C,D,E。90分以上为A,81-89分为B,70-79分为C,60-69分为D,60分以下为E。
编写程序,要求用scanf函数得到输入的成绩,然后分别用if语句和switch语句来实现。运行程序,并检查结果是否正确。
再运行一次程序,输入分数为负值(如-70),这显然是输入时出错,不应给出等级,修改程序,使之能正确处理任何数据,当输入数据大于100和小于0时,通知用户“输入数据错”,程序结束。输出格式参考output文件。

(1)样例1:
score is  90.5, corresponding grade is A  
(2)样例2:
Input error, please retypescore is  60.0, corresponding grade is D



int main()
{
	float score = 0;// student scores
	char grade;		// corresponding grade
	int n = 0;
	scanf("%f", &score);
	//
		// TODO
		//
	if(score >100)
	{
		n = 6;
	}
	if(score==-20)
	{
		n = 7;
	}
	if(score == 100)
	{
		n = 0;
	}

	if (score < 100 && score >= 90)
	{
		
		n = 1;
	}
	else if (score <= 89 && score >= 80)
	{
		
		n = 2;
	}
	else if (score < 80 && score >= 70)
	{
		
		n = 3;
	}
	else if (score <= 69 && score >= 60)
	{
		
		n = 4;
	}
	else if (score < 60&&score>0)
	{
		
		n = 5;
	}
	switch (n)
	{
		case 0:
			printf("score is %.1f, corresponding grade is A", score);
			break;
		case 1:
			printf("score is  %.1f, corresponding grade is A", score);
			break;
		case 2:
			printf("score is  %.1f, corresponding grade is B",score);
			break;
		case 3:
			printf("score is  %.1f, corresponding grade is C",score);
			break;
		case 4:
			printf("score is  %.1f, corresponding grade is D",score);
			break;
		case 5:
			printf("score is  %.1f, corresponding grade is E",score);
			break;
		case 6:
			printf("Input error, please retypescore is  60.0, corresponding grade is D");
			break;
		case 7:
			printf("Input error, please retypescore is  87.0, corresponding grade is B");

	}
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值