您的位置:首页 > 其它

第四周作业提交

2014-03-31 21:47 204 查看
实验作业

1.完成课本每一个编程题。要求先画出流程算法图或N-S图,然后编程实现,有可能的话使用两种以上方法;

2.编程求“百钱百鸡”问题。(鸡翁一值钱五,鸡母 一值钱三,鸡雏三值钱一。百钱买百鸡,问鸡翁、鸡母、鸡雏各几何?)

3.编程输入一个整数,计算它是几位数字,分别输出每一位数字,并输出各个数位上数字之和。

4.在一个平面上,有四个圆柱形塔,塔底圆心坐标分别为(2,2)、(-2,2)、(-2,-2)、(2,-2),塔半径为1,塔高为10米,塔外无建筑物。编程,输入任一个点平面坐标,求该点建筑物的高度。

5.编程计算s=1!+2!+3!+......n!(其中n为整数,n!表示计算n阶乘),要求使用两种以上的方法。

6.猴子吃苹果问题:猴子第一天摘了若干个苹果,当时吃了一半,还不过隐,又多吃了一个。第二天,又吃掉余下的一半,又多吃一个。以后每一天,都是吃掉前一天余下的一半零一个。到第10天,只有一个苹果了。问猴子第一天共摘了多少个苹果?

7.计算s
=a+aa+aaa+aa...a(n个)的值。其中a是一个数字,n表示a的位数。例如,当a=1,n=5时,则要计算的表达式为 s[5]=1+11+111+1111+11111

8.打印九九乘法表。

9.两个羽毛队进行单打比赛,各出3个人。甲队为张三、李四、王五3个队员,已队为陈六、赵七、宋八3个队员。现已经抽签决定比赛名单,有人向队员打听比赛名单,张三说他不和陈六打,王五说他不和陈六和宋八打。请编程找出3对比赛名单。

10.积累调试程序经验,收集错误信息原因(每个同学收集3-5条错误信息原因,并输入电脑形成文字)。n 作业要求网上提交各个程序和相关要求的文字。可以分两个博客文章来交。前5个题和后5个题各放到一个博客文章中提交。

1)课本编程题 (有几个题还没做,迟点补上)

习题7

//将输入数据按照小于10 , 10-100,100-1000 ,1000以上分类显示
//			by Tin Lin

# include <iostream>
using namespace std ;

int main()
{
float i ;
cout << "请输入一个数:" ;
cin >> i ;
if (i < 10)
cout << i << " " << "is less than 10" << endl ;
else if (i >= 10 && i <= 100)
cout << i << " " << "is 10 to 100" << endl ;
else if (i >= 100  && i <= 1000)
cout << i << " " << "is 100 to 1000" << endl ;
else if (i > 1000)
cout << i << " " << "is greater than 1000" << endl ;

return 0 ;
}




习题9

//求满足1^2+2^2 +3^2 +……+n^2<=1000的最大n值
//  by Tin Lin

# include <iostream>
using namespace std ;

int main ()
{
int n = 1 , sum = 0;
do
{
sum = sum + n * n ;
n++ ;
} while (sum <= 1000) ;
cout << --n << endl ;
return 0 ;
}




</pre><p></p><p></p><p></p><p></p><p><span style="font-size: 18px;"><span style="font-size:14px"><span style="font-size:14px">2)百鸡问题</span></span></span></p><p></p><pre code_snippet_id="267413" snippet_file_name="blog_20140331_4_6197102" name="code" class="cpp">//百鸡问题
//by Tin Lin

# include <iostream>
using namespace std ;

int main ()
{
<span style="white-space:pre">	</span>int i , j , k ;
cout << endl ;
cout << "方案为:" << endl ;
for (i = 0 ; i < 100 ; i++)
{for (j = 0 ; j < 100 ; j++)
{for (k = 0 ; k < 100 ; k+=3)
{
if(5*i+3*j+k/3==100 && i+j+k==100)
cout << "鸡翁" << i << "只"<< "鸡母" << j << "只" << "鸡雏" << k << "只"<< endl ;
}
}
}
return 0 ;

}



3)编程输入一个整数,计算它是几位数字,分别输出每一位数字,并输出各个数位上数字之和。

//输入一个整数,计算它是几位数字,分别输出每一位数字,并输出各个数位上数字之和。
//		by Tin Lin

# include <iostream>
using namespace std ;

int main()
{
int  a , b , c = 0 ,  d ,temp , s = 0 ;
cout << "请输入一个整数" ;		//该数不能太大,不然会溢出
cin >> a ;
b = a ;
d = a ;
do
{
a = a / 10 ;
c = c++ ;
} while ( a > 0 ) ;
cout << "该数为" << c << "位数" << endl << endl ;
cout << "该数" << d << "每个位上的数分别为" ;
do
{
temp = b % 10 ;
b = b / 10 ;
s = s + temp ;
cout <<  temp << "\t" ;
} while ( b > 0) ;
cout << endl ;
cout << "该数每一位相加之和为:" << s << endl ;

return 0 ;
}



4)在一个平面上,有四个圆柱形塔,塔底圆心坐标分别为(2,2)、(-2,2)、(-2,-2)、(2,-2),塔半径为1,塔高为10米,塔外无建筑物。编程,输入任一个点平面坐标,求该点建筑物的高度。

// 求建筑物的高度
// by Tin Lin

# include <iostream>
# include <math.h>
using namespace std ;

int main()
{
float x , y , d ;
cout << "输入横坐标x:";
cin >> x ;
cout << "输入纵坐标y:";
cin >> y ;
x = fabs(x) ;
y = fabs(y) ;
d = sqrt((x - 2) * (x - 2) + (y - 2) * (y - 2)) ;<span style="white-space:pre">	</span>//求(|x| , |y|) 到点(2,2)的距离
if (d <= 1)
cout << "该点坐标为:(" << x << "," << y << ")" <<"该建筑物的高度为10米" << endl ;
else
cout << "该建筑物的高度为0" << endl ;

return 0 ;
}








5)计算s=1!+2!+3!+......n!(其中n为整数,n!表示计算n阶乘),要求使用两种以上的方法。

// 计算s=1!+2!+3!+4!+5!+……+n!
//		by Tin Lin

# include <iostream>
using namespace std ;

int main ()
{
int i , n , s = 1 , sum = 0  ;
cout << "请输入任意一个自然数n=" ;
cin >> n ;
for (i = 1 ; i <= n ; i++)
{
s = s * i ;
sum = sum + s ;

}
cout << "sum = 1!+2!+……+" << n << "!" << "=" << sum << endl ;
return 0 ;

}



方法二

// 计算s=1!+2!+3!+4!+5!+……+n!
//		by Tin Lin

# include <iostream>
using namespace std ;

int main ()
{
int i , n , s = 1 , sum = 0  ;
cout << "请输入任意一个自然数n=" ;
cin >> n ;
i = 1 ;
do
{
s = s * i ;
sum = sum + s ;
i++ ;

} while (i <= n);
cout << "sum = 1!+2!+……+" << n << "!" << "=" << sum << endl ;
return 0 ;

}



6)猴子吃苹果问题:猴子第一天摘了若干个苹果,当时吃了一半,还不过隐,又多吃了一个。第二天,又吃掉余下的一半,又多吃一个。以后每一天,都是吃掉前一天余下的一半零一个。到第10天,只有一个苹果了。问猴子第一天共摘了多少个苹果?

//猴子吃苹果问题
//  by Tin Lin

# include <iostream>
using namespace std ;

int main ()
{
int sum = 1 , t ;
for (t = 1 ; t <= 9 ; ++t)
{
sum = (sum + 1) * 2 ;
}
cout << endl << endl ;
cout << "答案是" << sum << endl ;
return 0 ;
}




7)计算s
=a+aa+aaa+aa...a(n个)的值。其中a是一个数字,n表示a的位数。例如,当a=1,n=5时,则要计算的表达式为s[5]=1+11+111+1111+11111

//计算s
= a+aa+aaa+aaaa...
//          by Tin Lin

# include <iostream>
using namespace std ;

int main ()
{
int a , n , t ,i = 0 ,s = 0 ;
cout << "请输入a的值" ;
cin >> a ;
cout << "请输入n的值" ;
cin >> n ;
for (t = 1 ; t <= n ; t++)
{
i = 10 * i + a ;
s = s + i ;
}
cout << endl ;
cout << "所以" << "S["<< n <<"]=" << s << endl ;
return 0 ;

}




8)打印九九乘法表。

//九九乘法口诀
// by Tin Lin

# include <iostream>
using namespace std ;

int main ()
{
int i , j , s ;
for (i = 1 ; i <= 9 ; i++)
{
for (j = 1 ; j <= i ; j++)
{
s = i * j ;
cout << i << "*" << j << "=" << s << "\t" ;
}
cout << endl ;
}
return 0 ;
}




补交的作业

习题5

//求e得近似值
//by Tin Lin

# include <iostream>
# include <cmath>
using namespace std ;

int main ()
{
int i = 1 ;
long double j = 0 , temp = 1 , por, result = 0 ;
do
{
temp = temp * i ;
j = j + temp ;
i++ ;
por = 1 / temp ;
result += por ;
}while (fabs(por)>=1e-5) ;
cout << "e = " << result + 1 << endl ;
return 0 ;
}




习题6

//求e得近似值
//by Tin Lin

# include <iostream>
# include <cmath>
using namespace std ;

int main ()
{
int i = 1 ;
long double j = 0 , temp = 1 , por, result = 0 ;
do
{
temp = temp * i ;
j = j + temp ;
i++ ;
por = 1 / temp ;
result += por ;
}while (fabs(por)>=1e-5) ;
cout << "e = " << result + 1 << endl ;
return 0 ;
}




习题8

//输出图形

#include<iostream>
using namespace std;
int main()
{
cout <<"       *       " << endl ;
cout <<"     * * *     " << endl ;
cout <<"   * * * * *   " << endl ;
cout <<" * * * * * * * " << endl ;
cout <<"   * * * * *   " << endl ;
cout <<"     * * *     " << endl ;
cout <<"       *       " << endl ;
return 0;
}



习题10

//百万富翁和陌生人的换钱问题
//			by Tin Lin

# include <iostream>
# include <math.h>
using namespace std ;
int main ()
{
int t , j , i ;
float  result = 0.0 ;
j = 1 ;
for (t= 1 ; t < 31 ; t++)
{
j = pow(2,t-1) ;
result = j + result ;
i = 1000000 * t ;
}
cout << "一个月中陌生人给了百万富翁"<< result / 1000000 << "万元" << endl ;
cout << "百万富翁给了陌生人" << i/100000 << "万元" << endl ;
return 0 ;
}



9两个羽毛队进行单打比赛,各出3个人。甲队为张三、李四、王五3个队员,已队为陈六、赵七、宋八3个队员。现已经抽签决定比赛名单,有人向队员打听比赛名单,张三说他不和陈六打,王五说他不和陈六和宋八打。请编程找出3对比赛名单。

//羽毛球比赛对战选手名单
//		by Tin Lin

# include <iostream>
using namespace std ;

int main()
{
char zhang , li , wang ;
cout << "D是陈六,E是赵七,F是宋八" << endl ;
for (zhang = 'D' ; zhang <= 'F' ; zhang++)
for(li = 'D' ; li <= 'F' ; li++)
for (wang = 'D' ; wang <= 'F' ; wang++)
{
if (zhang != li && zhang != wang && li != wang)			//排除重复
if (zhang != 'D' && wang != 'D' && wang != 'F' )	//排除已知情况
{
cout << "张三VS" << zhang << endl ;
cout << "李四VS" << li << endl ;
cout << "王五VS" << wang << endl ;
}

}
return 0 ;
}



习题7修改后

//将输入数据按照小于10 , 10-100,100-1000 ,1000以上分类显示
//          by Tin Lin

# include <iostream>
using namespace std ;

int main()
{
float i ;
cout << "请输入一个数:" ;
cin >> i ;
if (i < 10)
cout << i << " " << "is less than 10" << endl ;
else if ( i < 101)
cout << i << " " << "is 10 to 100" << endl ;
else if (i < 1001)
cout << i << " " << "is 100 to 1000" << endl ;
else
cout << i << " " << "is greater than 1000" << endl ;

return 0 ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: