码迷,mamicode.com
首页 > 其他好文 > 详细

Objective-C 程序设计(第六版)第五章习题答案

时间:2014-09-28 02:26:50      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   for   sp   div   2014   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              1
2014-09-27 15:00:26.460 prog1[1218:303]  2              4
2014-09-27 15:00:26.461 prog1[1218:303]  3              9
2014-09-27 15:00:26.461 prog1[1218:303]  4              16
2014-09-27 15:00:26.461 prog1[1218:303]  5              25
2014-09-27 15:00:26.462 prog1[1218:303]  6              36
2014-09-27 15:00:26.462 prog1[1218:303]  7              49
2014-09-27 15:00:26.463 prog1[1218:303]  8              64
2014-09-27 15:00:26.463 prog1[1218:303]  9              81
2014-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           15
2014-09-27 15:14:33.351 prog1[1367:303]  10           55
2014-09-27 15:14:33.352 prog1[1367:303]  15           120
2014-09-27 15:14:33.352 prog1[1367:303]  20           210
2014-09-27 15:14:33.353 prog1[1367:303]  25           325
2014-09-27 15:14:33.353 prog1[1367:303]  30           465
2014-09-27 15:14:33.353 prog1[1367:303]  35           630
2014-09-27 15:14:33.354 prog1[1367:303]  40           820
2014-09-27 15:14:33.354 prog1[1367:303]  45           1035
2014-09-27 15:14:33.354 prog1[1367:303]  50           1275
Program 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 的阶乘是 1
2014-09-27 15:59:35.515 prog1[1731:303]   1 的阶乘是 1
2014-09-27 15:59:35.515 prog1[1731:303]   2 的阶乘是 2
2014-09-27 15:59:35.516 prog1[1731:303]   3 的阶乘是 6
2014-09-27 15:59:35.516 prog1[1731:303]   4 的阶乘是 24
2014-09-27 15:59:35.517 prog1[1731:303]   5 的阶乘是 120
2014-09-27 15:59:35.517 prog1[1731:303]   6 的阶乘是 720
2014-09-27 15:59:35.517 prog1[1731:303]   7 的阶乘是 5040
2014-09-27 15:59:35.518 prog1[1731:303]   8 的阶乘是 40320
2014-09-27 15:59:35.518 prog1[1731:303]   9 的阶乘是 362880
Program ended with exit code: 0

 4.略

 5.

        int n, number, triangularNumber, counter, times;
    
        NSLog(@"请输入你想要计算三角数的次数:");
        scanf("%d", &times);
        //以上照搬书本
        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 程序设计(第六版)第五章习题答案

标签:style   blog   color   ar   for   sp   div   2014   c   

原文地址:http://www.cnblogs.com/MingMing-King/p/3997519.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!