算法竞赛入门经典 1.5 实验题目上机

//1.5 小结与习题
//数据类型实验(C++)
//实验A1:表达式11111*11111的值是多少?把5个1改成6个1呢?9个1呢?
#include<iostream>
using namespace std;

int main()
{
	cout<<11111*11111<<endl;		//5个1在范围内,结果为123454321
	cout<<111111*111111<<endl;		//6个1整型常量溢出
	cout<<111111111*111111111<<endl;//整型常量溢出,编译出错
	return 0;
}

//实验A2:把实验A1中的所有数换成浮点数,结果如何
#include<iostream>
using namespace std;

int main()
{
	cout<<11111.0*11111.0<<endl;		//结果为:1.23454e+008
	cout<<111111.0*111111.0<<endl;		//结果为:1.23457e+010
	cout<<111111111.0*111111111.0<<endl;//结果为:1.23457e+016
	return 0;
}

//实验A3:表达式sqrt(-10)的值是多少?尝试用各种方式输出。在计算过程中系统会报错吗?
#include<iostream>
#include<cmath>
using namespace std;

int main()
{
	double a=sqrt(-10.0);	//sqrt函数返回一个double型数值
	cout<<sqrt(-10.0)<<endl;//输出结果:-1.#IND
	cout<<a<<endl;
	return 0;
}

//实验A4:表达式1.0/0.0,0.0/0.0的值是多少?尝试用各种方式输出。在计算过程中会报错吗?
#include<iostream>
using namespace std;

int main()
{
	double a,b;
	a=1.0/0.0;
	b=0.0/0.0;
	cout<<a<<" "<<b<<endl;			//出错:被零除或对零求模
	cout<<1.0/0.0<<" "<<0.0/0.0<<endl;//出错:被零除或对零求模
	return 0;
}

//实验A5:表达式1/0的值是多少?在计算过程会报错吗?
#include<iostream>
using namespace std;

int main()
{
	cout<<1/0<<endl;  //报错:被零除或对零求模
	return 0;
}


//1.5.2 scanf输入格式实验(C语言)
//实验B1:在同一行中输入12和2,是否得到了预期的结果?
#include<stdio.h>

int main()
{
	int a,b;
	scanf("%d%d",&a,&b);	//输入a,b
	printf("%d%d",a,b);		//连续输出
	printf("%d %d",a,b);	//间隔输出
	return 0;
}

	
//实验B2:在不同的两行中输入12和2,是否得到了预期的结果?
#include<stdio.h>

int main()
{
	int a,b;
	scanf("%d\n%d\n",&a,&b);
	printf("%d%d",a,b);
	printf("%d %d",a,b);
	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;

}


//1.53 printf语句输出实验
//实验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');
	return 0;

}


posted on 2012-03-24 12:34  1.曲待续  阅读(265)  评论(0编辑  收藏  举报

导航