当前位置: 首页 > news >正文

Homework Exercises 1

 

第一题

返回所有这样的整数k的和:

  • 1 <= k < N
  • N可被k整除
/* 1a. */
/** Returns the sum of all integers, k, such that 1 <= k < N and
 *  N is evenly divisible by k. */
static int factorSum(int N) {
    // Fill in here
}
static int factorSum(int N) {
    int i = 0;
    int j = 0;
    for (; i <= N / 2; i++) {
        if (N % i == 0) {
            j += i;
        }
    }
    return j;
}

注意无需遍历N,只需要到N/2就行了。

打印出0 - N间的所有相亲数,每一对占一行,小的在前,大的在后。

所谓想亲数就是:指两个正整数中,彼此的全部约数之和(本身除外)与另一方相等。

例如220与284:

  • 220的全部约数(除掉本身)相加是:1+2+4+5+10+11+20+22+44+55+110=284
  • 284的全部约数(除掉284本身)相加的和是:1+2+4+71+142=220
/* 1b. */
/** Print the set of all sociable pairs whose members are all
 *  between 1 and N>=0 (inclusive) on the standard output (one pair per
 *  line, smallest member of each pair first, with no repetitions). */
static void printSociablePairs(int N) {
    // Fill in here
}
static void printSociablePairs(int N) {
    int i = 0;
    int j = 0;
    for(; i<=N; i++){
        j = factorSum(i)
        if (j > i){
            system.out.println(i + ", " + j);
        }
    }
}

 

第二题

根据下面的简单链表实现完成下面题目。

import java.util.Formatter;

/** Scheme-like pairs that can be used to form a list of integers.
 *  @author P. N. Hilfinger
 *  [Do not modify this file.]
 */
public class IntList {
    /** First element of list. */
    public int head;
    /** Remaining elements of list. */
    public IntList tail;

    /** A List with head HEAD0 and tail TAIL0. */
    public IntList(int head0, IntList tail0) {
        head = head0;
        tail = tail0;
    }

    /** A List with null tail, and head = 0. */
    public IntList() {
        /* NOTE: public IntList () { }  would also work. */
        this (0, null);
    }

    /** Returns a new IntList containing the ints in ARGS. */
    public static IntList list(Integer ... args) {
        IntList result, p;

        if (args.length > 0) {
            result = new IntList(args[0], null);
        } else {
            return null;
        }

        int k;
        for (k = 1, p = result; k < args.length; k += 1, p = p.tail) {
            p.tail = new IntList(args[k], null);
        }
        return result;
    }

    /** Returns true iff X is an IntList containing the same sequence of ints
     *  as THIS. */
    public boolean equals(Object x) {
        if (!(x instanceof IntList)) {
            return false;
        }
        IntList L = (IntList) x;
        IntList p;
        for (p = this; p != null && L != null; p = p.tail, L = L.tail) {
            if (p.head != L.head) {
                return false;
            }
        }
        if (p != null || L != null) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        return head;
    }

    @Override
    public String toString() {
        Formatter out = new Formatter();
        String sep;
        sep = "(";
        for (IntList p = this; p != null; p = p.tail) {
            out.format("%s%d", sep, p.head);
            sep = ", ";
        }
        out.format(")");
        return out.toString();
    }

}

返回一个IntList包含IntList A,后面跟着IntList B,不允许使用new。

/* 2a. */
/** Returns a list consisting of the elements of A followed by the
 *  elements of B.  May modify items of A. Don't use 'new'. */
static IntList dcatenate(IntList A, IntList B) {
    // Fill in here
}
static IntList dcatenate(IntList A, IntList B) {
    IntList p
    for (p = A; p != null; p = p.tail;){
        if(p.tail == null){
            p.tail = B;
        }
    }
    
    return A;
}

注意链表的概念。

返回一个IntList的一部分,从原IntList L的第start个元素开始,长度为len。不允许改变原IntList的每个元素,如果元素不存在会抛出错误。

/* 2b. */
/** Returns the sublist consisting of LEN items from list L,
 *  beginning with item #START (where the first item is #0).
 *  Does not modify the original list elements.
 *  It is an error if the desired items don't exist. */
static IntList sublist(IntList L, int start, int len) {
    //Fill in here
}
static IntList sublist(IntList L, int start, int len) {
    IntList p;
    IntList k;
    IntList j;
    int i = 0;
    int head0;
    for (p = L; i < start + len; p = p.tail, i++){
        if (i == start){
            head0 = p.head;
            k = new IntList(head0, null);
            j = k;
        } else if (i > start && i < start + len)
            head0 = p.head;
            j.tail = new IntList(head0, null);
            j = j.tail;
        }
    }
        
    return k;
}

返回一个IntList的一部分,从原IntList L的第start个元素开始,长度为len。需要改变原IntList的元素,如果元素不存在会抛出错误。

/* 2c. */
/** Returns the sublist consisting of LEN items from list L,
 *  beginning with item #START (where the first item is #0).
 *  May modify the original list elements. Don't use 'new'.
 *  It is an error if the desired items don't exist. */
static IntList dsublist(IntList L, int start, int len) {
    // Fill in here
}
static IntList dsublist(IntList L, int start, int len) {
    IntList p;
    int i = 0;
    for (p = L; i <= start + len; p = p.tail, i++){
        if (i == start){
            L = p;
        } else if (i == start + len){
            p = null;
        }
    }
        
    return L;
}

 

 

转载于:https://www.cnblogs.com/justany/archive/2013/02/04/2889431.html

相关文章:

  • 图片旋转和翻转
  • atoi简单实现
  • rndc: connect failed: 127.0.0.1#953: connection refused
  • 2013年
  • 我国多地进行区划调整
  • python制作galgame引擎(五)
  • HTML 重定向 ----定时跳转刷新页面
  • 管理Quarz的工具类,QuarzManager
  • 经验之谈:ping命令诊断网络故障
  • oracle ora-12154 无法解析连接字符串
  • 关于Android的多种屏幕适配
  • 我来了,筒子们
  • Unity3D的Android移动之路之平台依赖编译
  • 时尚型男进化论第一篇
  • 在iOS中UI渲染过程具有绝对的优先等级
  • 《微软的软件测试之道》成书始末、出版宣告、补充致谢名单及相关信息
  • Docker容器管理
  • gcc介绍及安装
  • GDB 调试 Mysql 实战(三)优先队列排序算法中的行记录长度统计是怎么来的(上)...
  • github指令
  • go语言学习初探(一)
  • GraphQL学习过程应该是这样的
  • Webpack4 学习笔记 - 01:webpack的安装和简单配置
  • win10下安装mysql5.7
  • 道格拉斯-普克 抽稀算法 附javascript实现
  • 搞机器学习要哪些技能
  • 记录:CentOS7.2配置LNMP环境记录
  • 快速构建spring-cloud+sleuth+rabbit+ zipkin+es+kibana+grafana日志跟踪平台
  • 免费小说阅读小程序
  • 前端js -- this指向总结。
  • 世界编程语言排行榜2008年06月(ActionScript 挺进20强)
  • 走向全栈之MongoDB的使用
  • Python 之网络式编程
  • ​queue --- 一个同步的队列类​
  • !!java web学习笔记(一到五)
  • #调用传感器数据_Flink使用函数之监控传感器温度上升提醒
  • (Matalb时序预测)PSO-BP粒子群算法优化BP神经网络的多维时序回归预测
  • (Redis使用系列) Springboot 使用Redis+Session实现Session共享 ,简单的单点登录 五
  • (附表设计)不是我吹!超级全面的权限系统设计方案面世了
  • (附源码)计算机毕业设计ssm高校《大学语文》课程作业在线管理系统
  • (原創) 是否该学PetShop将Model和BLL分开? (.NET) (N-Tier) (PetShop) (OO)
  • (转)jdk与jre的区别
  • (转)socket Aio demo
  • (转)全文检索技术学习(三)——Lucene支持中文分词
  • ***利用Ms05002溢出找“肉鸡
  • **PyTorch月学习计划 - 第一周;第6-7天: 自动梯度(Autograd)**
  • .h头文件 .lib动态链接库文件 .dll 动态链接库
  • .NET LINQ 通常分 Syntax Query 和Syntax Method
  • .Net Remoting(分离服务程序实现) - Part.3
  • .net 微服务 服务保护 自动重试 Polly
  • .netcore 6.0/7.0项目迁移至.netcore 8.0 注意事项
  • .NetCore Flurl.Http 升级到4.0后 https 无法建立SSL连接
  • .Net程序帮助文档制作
  • /proc/interrupts 和 /proc/stat 查看中断的情况
  • @Controller和@RestController的区别?