NTUBIME100HW 板


LINE

PS.因为BBS页宽的关系 有些换行的地方要特别注意... (尤其是程式码比较长的那几行) 第一题:(10分) 解释名词 (1) stack last-in, first-out(LIFO) data structures (2) syntax error compiler-time error, code that violate language rules(syntax) (3) local variable variables declared in a function definition's body (4) lvalue variable can be used on the left side of an assignment operater (5) public: indicate the members appears after this access specifier can be called by others in the program 第二题:(10分) 下列叙述执行後变数A,B,C,D,E之值分别为何? int A=0, B=10, C=20, D=10, E=40, i=0; while( i <= 5 ) { if( i > 2 ) A = A + 5; else if( D >= 7 ) B = B + A; else C = D + 2*C; D--; i++; } E *= i; ─────────────────────────────────── 答案栏: A = ˍ15ˍ B = ˍ10ˍ C = ˍ20ˍ D = ˍ4ˍ E = ˍ240ˍ ─────────────────────────────────── 第三题:(10分)   下列程式片段执行後的结果请列於答案栏中 for( int i=1; i<=6; i++ ) { for( int j=1; j<=15; j++ ) { if( i%3 == 0 ) cout << ‘*’; else cout << ‘+’; } cout << endl; } ─────────────────────────────────── 第四题:(10分)   假设x的值为22,y的值为4,z的值为8,请将下列逻辑运算式的值写出来(请填入true或false)。 逻辑运算式 真假值 (true or false) x + y >= z True y == (x-2*z-2) True 6*z!=z True (x=y)==4 True (( x > y ) ? 5 : 10) == 5 True ────────────────────────────────── 第五题:(10分)   请将下列程式在个人电脑上执行後之输出仔细地填入右侧答案栏中。 答案栏: x = 20 x = 30 x = 20 x = 10 x = 5 x = 6 x = 10 x = 5 x = 6 #include <iostream> using std::cout; using std::endl; void a(void); void b(void); int x = 10; main() { int x=20, y=0; cout << “ x = “ << x << endl; { int x=30; cout << “ x = “ << x << endl; } cout << “ x = “ << x << endl; a(); b(); a(); b(); } void a( void ) { cout << “ x = “ << x << endl; } void b( void ) { int x = 5; cout << “ x = “ << x << endl; x++; cout << “ x = “ << x << endl; } 第六题:(10分) 请设计一个程式,列出九九乘法表,其格式如下: 1*1=1 1*2= 2 1*3= 3 1*4= 4 1*5= 5 1*6= 6 1*7= 7 1*8= 8 1*9= 9 2*1=2 2*2= 4 2*3= 6 2*4= 8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 … … 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 ─────────────────────────────────── 答案栏:(空间不够时请写於背面) #include <iostream> using namespace std ; void main() { for(int i=1;i<10;i++) { for(int j=1;j<10;j++) { cout << i << "*" << j << "=" << i*j << "\t" ; } cout << endl ; } } 第七题:(10分)   请计算并列印出费朋纳西序列(Fibonacci sequence)之前二十项, 请注意列印之每一行共有五个数(合计四行)。   提示:费朋纳西序列为 1, 1, 2, 3, 5, 8, 13, 21, 34,..... ─────────────────────────────────── 答案栏:(空间不够时请写於背面) #include <iostream> using namespace std ; int fib(int num) { if(num < 1) return -1 ; //假如输入值为负或0 则回传负值代表错误 else if(num == 1) return 1 ; else if(num == 2) return 1 ; //上两项是递回限制 else return fib(num-1) + fib(num-2) ; //递回 } void main() { for(int i=1;i<=num;i++) { cout << fib(i) << " " ; //输出 if(i % 5 == 0) cout << endl ; } } 第八题:(10分) 请设计一个程式模拟醉汉走路的结果。假设醉汉的行走只有前後左右四个方向, 而往四个方向行进的机率都一样,令醉汉初始的位置座标为(x,y)=(0,0), 请利用程式模拟算出醉汉行走1000步以後,他所在的座标位置。 ─────────────────────────────────── 答案栏:(空间不够时请写於背面) #include <stdlib.h> #include <iostream> #include <time.h> using namespace std ; void main() { srand(time(0)) ; //初始化随机种子 int x= 0 , y=0 , vec ; //位置 x y 方向 vec for(int i=0;i<1000;i++) //移动1000次 { vec = rand() % 4 ; //随机取得要走的方向 switch(vec) //判断此步的方向 { case 0: x += 1 ; break ; case 1: x -= 1 ; break ; case 2: y += 1 ; break ; case 3: y -=1 ; break ; default: break ; } } cout << "(" << x << "," << y << ")" ; } 第九题:(10分) 请设计一个名称为Sphere的类别,此类别有三个内部资料(private data member), 分别为radius、volume与s_area。 此类别中另有三个成员函式(public member function), 分别为CalculateVolume、CalculateSurfaceArea与PrintSphere, 用来计算球体的体积、表面积与输出体积及表面积至电脑萤幕。 请写出这个类别的完整内容,包括成员函式的定义程式码。 【Hint: 球体表面积公式为4pr2 ,球体体积公式为(4/3)pr3】 ─────────────────────────────────── 答案栏:(空间不够时请写於背面) #define PI 3.1415926 #include <stdlib.h> #include <iostream> #include <cmath> using namespace std ; class Sphere { public: Sphere(double r); //constructor 需输入此物件的半径 { radius = r ; } double CalculateVolume(void) ; //member function double CalculateSurfaceArea(void) ; void PrintSphere(void) ; private: double radius ; //data member double volume ; double s_area ; }; double Sphere::CalculateSurfaceArea(void) { s_area = 4.0 * PI * pow(radius,2) ; return s_area ; } double Sphere::CalculateVolume(void) { volume = 4.0 / 3.0 * PI * pow(radius,3) ; return volume ; } void Sphere::PrintSphere() { cout << "Surface area:" << s_area << endl ; cout << "Volume:" << volume << endl ; } 第十题:(10分) 请写出一个程式模拟国内乐透彩卷的开奖号码。 此程式必须以主程式main()呼叫函式int loto_number()的方式完成, 而loto_number()函式所传回给主程式的整数为随机模拟1~42中的一个号码。 主程式则需要连续呼叫函式多次以产生 一组7个数字的乐透彩号码(六个号码加上一个特别号),最後将结果显示於萤幕上。 ※请特别注意,程式需要检查相同的号码不能同时出现於一组乐透彩号码中。 【Hint: 您将可能会使用到 srand()及rand()函式 】 ─────────────────────────────────── 答案栏:(空间不够时请写於背面) #include "stdafx.h" #include "stdlib.h" #include "time.h" #include <iostream> using namespace std; int loto_number(void) //函式随机输出数字 1~42 { return rand() % 42 + 1 ; } void main() { int n1 = 0 ,n2 = 0 ,n3 = 0 ,n4 = 0 ,n5 = 0 ,n6 = 0 ,n7 = 0 ; //七个号码先设定为0 srand(time(0)) ; //设定乱数种子 n1 = loto_number() ; //第一个数字无重复问题 直接取得即可 n2 = loto_number() ; //第二个数字开始 需要检查是否与之前的数字相同 while(n2 == n1) //检查 若是相同 则一直取到不同为止 { n2 = loto_number() ; } n3 = loto_number() ; //依此类推 while(n3 == n1 || n3 == n2) { n3 = loto_number() ; } n4 = loto_number() ; while(n4 == n1 || n4 == n2 || n4 == n3) { n4 = loto_number() ; } n5 = loto_number() ; while(n5 == n1 || n5 == n2 || n5 == n3 || n5 == n4) { n5 = loto_number() ; } n6 = loto_number() ; while(n6 == n1 || n6 == n2 || n6 == n3 || n6 == n4 || n6 == n5) { n6 = loto_number() ; } n7 = loto_number() ; while(n7 == n1 || n7 == n2 || n7 == n3 || n7 == n4 || n7 == n5 || n7 == n6) { n7 = loto_number() ; } cout << n1 << "," << n2 << "," << n3 << "," << n4 << "," << n5 << "," << n6 << "," << n7 << endl ; //输出结果 system("pause") ; } -- ζ ξ 长的越帅,责任越重 ○- <(╯ 难怪这一生我活着都没什麽压力.. ■)﹥ --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 59.115.55.158
1F:推 arthurkot:杰夫是好人 11/20 19:20
2F:推 happierway:别随便发我卡~尤其是最近...... 11/20 22:43
3F:推 Jason0815:杰夫是帅哥 11/21 19:55
4F:推 happierway:楼上真中肯!!(大心) 11/21 22:11
5F:→ angelwing12:杰夫是坏人 11/21 23:14







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Tech_Job站内搜寻

TOP