算法竞赛入门经典第一章实验及习题

实验

  • 实验A1:表达式11111*11111的值是多少?把5个1改成6个1呢?9个1呢?
```c
实验A1:表达式11111*11111的值是多少?把5个1改成6个1呢?9个1呢?
#include <stdio.h>
int main()
{
    printf("%d\n",11111*11111);  正常数字
    printf("%d\n",111111*111111); 数据太大溢出,为负值
    printf("%d\n",111111111*111111111); 结果是整数,但是数值错误
    return 0;
}
实验A2:把实验A1中的所有数换成浮点数,结果如何  
#include <stdio.h>
int main()
{
    printf("%f\n",1.1111*1.1111);//1.234543
    printf("%f\n",1.11111*1.11111);//1.234565
    printf("%f\n",1.11111111*1.11111111);//1.234568
    return 0;
}
实验A3:表达式sqrt(-10)的值是多少?尝试用各种方式输出。在计算过程中系统会报错吗?  
#include <stdio.h>
#include <math.h>
int main()
{
    printf("%f\n",sqrt(-10));//-1.#IND00
    printf("%d\n",sqrt(-10));//0
    printf("%.10f\n",sqrt(-10));//-1.#IND000000
    return 0;
} 
实验A4:表达式1.0/0.0,0.0/0.0的值是多少?尝试用各种方式输出。在计算过程中会报错吗?  
#include <stdio.h>
#include <math.h>
int main()
{
    printf("%f\n",1.0/0.0);//1.#INF00
    printf("%.2f\n",1.0/0.0);//1.#J
    printf("%.10f\n",1.0/0.0);//1.#INF000000
    printf("%f\n",0.0/0.0);//-1.#IND00
    printf("%.2f\n",0.0/0.0);//-1.#J
    return 0;
}
实验A5:表达式1/0的值是多少?在计算过程会报错吗?  
#include <stdio.h>
#include <math.h>
int main()
{
    printf("%f\n",1/0);//不显示
    printf("%d\n",1/0);//不显示

    return 0;
}  

取模也有问题

 
 因为编译器所能检测到的错误信息是有限的,它能检测C语言的语法错误和语义错误,而逻辑错误一般是检测不出来的. 当你输入的程序语法和语义都没有出错但是逻辑出错时,编译会通过,但是运行的时候就通不过了,你需要重新检查你的代码,找出错误改正过来.
实验B1:在同一行中输入12和2,是否得到了预期的结果?  

#include<stdio.h>
int main()
{
    int a;
    int b;
    scanf("%d%d",&a, &b);//输入a,b
    printf("%d%d\n",a, b);//122
    printf("%d% d\n",a, b);//12 2
    return 0;
}
实验B2:在不同的两行中输入12和2,是否得到了预期的结果?  

#include<stdio.h>
int main()
{
    int a;
    int b;
    scanf("%d%d",&a, &b);//输入a,b
    printf("%d%d\n",a, b);//122
    printf("%d% d\n",a, b);//12 2
    return 0;
}
实验B3:在实验B1和B2中,在12和2前面和后面加入大量的空格或水平制表符,甚至插入一些空行  
#include<stdio.h>  
  
int main()  
{  
    int a,b;  
    scanf("%d%d",&a,&b);  
    printf("%d%d\n",a,b);   //连续输出  
    printf("%d %d\n",a,b);  //输出两个数之间空一格  
    printf("%d\t%d\n",a,b); //输出空一个水平制表符  
    return 0;  
}
实验B4:把2换成字符s,重复实验B1~B3  
#include<stdio.h>  
  
int main()  
{  
    int a=12;  
    char b='s';  
    printf("%d%c\n",a,b);  
    printf("%d %c\n",a,b);  
    printf("%d\t%c\n",a,b);  
    return 0;  
  
}
实验C1:仅用一条printf语句,打印1+2和3+4的值,用两个空行隔开  
#include<stdio.h>  
  
int main()  
{  
    printf("%d\n\n%d\n",1+2,3+4);  
    return 0;    
}
实验C2:试着把%d中的两个字符(百分号和小写字母d)输出到屏幕  
#include<stdio.h>  
  
int main()  
{  
    printf("%c %c",'%','d');  
    return 0;    
}
实验C3:试着把\n中的两个字符(反斜线和小写字母n)输出到屏幕    
#include<stdio.h>

int main()
{
    printf("%c %c\n",'\\','n');//\ n
    printf("\\n");//\n
    return 0;
} 

习题

判断年份是否是闰年

#include <stdio.h>
int main()
{
    int n=2000;
    if((n%4==0)&&(n%100!=0)||(n%400==0))
    {
        printf("true");
    }
    else
    {
        printf("false");
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42420263/article/details/86559735