首页 > 代码库 > Objective-C 程序设计(第六版)第五章习题答案
Objective-C 程序设计(第六版)第五章习题答案
1.
NSLog(@"数字N N的平方"); NSLog(@"--------------------"); for (int n = 1; n <= 10; n++) { NSLog(@"%2d %d",n,n*n); } //结果如下2014-09-27 15:00:26.457 prog1[1218:303] 数字N N的平方2014-09-27 15:00:26.459 prog1[1218:303] --------------------2014-09-27 15:00:26.460 prog1[1218:303] 1 12014-09-27 15:00:26.460 prog1[1218:303] 2 42014-09-27 15:00:26.461 prog1[1218:303] 3 92014-09-27 15:00:26.461 prog1[1218:303] 4 162014-09-27 15:00:26.461 prog1[1218:303] 5 252014-09-27 15:00:26.462 prog1[1218:303] 6 362014-09-27 15:00:26.462 prog1[1218:303] 7 492014-09-27 15:00:26.463 prog1[1218:303] 8 642014-09-27 15:00:26.463 prog1[1218:303] 9 812014-09-27 15:00:26.463 prog1[1218:303] 10 100
2.
NSLog(@"整数N N的三角数"); NSLog(@"-------------------"); int n, triangularNumber; for (n = 5; n <= 50; n += 5) { triangularNumber = n * (n + 1) / 2; NSLog(@" %2d %d", n , triangularNumber); } //结果如下2014-09-27 15:14:33.348 prog1[1367:303] 整数N N的三角数2014-09-27 15:14:33.350 prog1[1367:303] -------------------2014-09-27 15:14:33.351 prog1[1367:303] 5 152014-09-27 15:14:33.351 prog1[1367:303] 10 552014-09-27 15:14:33.352 prog1[1367:303] 15 1202014-09-27 15:14:33.352 prog1[1367:303] 20 2102014-09-27 15:14:33.353 prog1[1367:303] 25 3252014-09-27 15:14:33.353 prog1[1367:303] 30 4652014-09-27 15:14:33.353 prog1[1367:303] 35 6302014-09-27 15:14:33.354 prog1[1367:303] 40 8202014-09-27 15:14:33.354 prog1[1367:303] 45 10352014-09-27 15:14:33.354 prog1[1367:303] 50 1275Program ended with exit code: 0
3.
//前十个整数是包括零的,就是这个破零搞的我头大了。 int n, f, factorial = 1; for (n = 0; n < 10; n++) { for (f = 1; f <= n; f++) { factorial *= f; } NSLog(@" %d 的阶乘是 %d", n, factorial); factorial = 1; } //结果如下2014-09-27 15:59:35.513 prog1[1731:303] 0 的阶乘是 12014-09-27 15:59:35.515 prog1[1731:303] 1 的阶乘是 12014-09-27 15:59:35.515 prog1[1731:303] 2 的阶乘是 22014-09-27 15:59:35.516 prog1[1731:303] 3 的阶乘是 62014-09-27 15:59:35.516 prog1[1731:303] 4 的阶乘是 242014-09-27 15:59:35.517 prog1[1731:303] 5 的阶乘是 1202014-09-27 15:59:35.517 prog1[1731:303] 6 的阶乘是 7202014-09-27 15:59:35.517 prog1[1731:303] 7 的阶乘是 50402014-09-27 15:59:35.518 prog1[1731:303] 8 的阶乘是 403202014-09-27 15:59:35.518 prog1[1731:303] 9 的阶乘是 362880Program ended with exit code: 0
4.略
5.
int n, number, triangularNumber, counter, times; NSLog(@"请输入你想要计算三角数的次数:"); scanf("%d", ×); //以上照搬书本 for (counter = 1; counter <= times; counter++) { NSLog(@"what triangular number do you want?"); scanf("%d",&number); triangularNumber = 0 ; for ( n = 0; n <= number; ++n) { triangularNumber += n; } NSLog(@"Triangular number %d is %d", number, triangularNumber); }
6.
//代码清单5-2 int n = 1; int triangularNumber = 0; while ( n <= 200 ) { triangularNumber += n; n++; } NSLog(@"The 200th triangular number is %d", triangularNumber); //代码清单5-3 int n, triangularNumber; NSLog(@"TABLE OF TRIANGULAR NUMBERS"); NSLog(@" n Sum form 1 to n "); NSLog(@"----- --------------"); triangularNumber = 0; n = 1; while ( n <= 10 ) { triangularNumber += n; NSLog(@" %2i %i", n, triangularNumber); ++n; } //代码清单5-4 int n, number, triangularNumber; NSLog(@"What triangular number do you want?"); scanf("%i",&number); triangularNumber = 0; n = 1; while (n <= number) { triangularNumber += n; ++n; } NSLog(@"Triangular number %i is %i\n", number, triangularNumber);//代码清单5-5 int n, number, triangularNumber; int counter = 1; while (counter <= 5) { NSLog(@"What triangular number do you want?"); scanf("%i", &number); triangularNumber = 0; n = 1; while (n <= number) { triangularNumber += n; ++n; } NSLog(@"Triangular number %i is %i", number, triangularNumber); ++counter; }
7.输入负数得到的每个各数也是负数!
8.
int number, right_digit, result; NSLog(@"Enter your number:"); scanf("%i", &number); while ( number != 0 ) { right_digit = number % 10; result += right_digit; number /= 10; } NSLog(@"%d",result);
Objective-C 程序设计(第六版)第五章习题答案
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。