• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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);

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap