SquareDigits

Problem Statement
    
***Note: Please keep programs under 7000 characters in length. Thank you

Class Name: SquareDigits
Method Name: smallestResult
Parameters: int
Returns: int

Define the function S(x) as the sum of the squares of the digits of x.
For example: S(3)=3*3=9 and S(230)=2*2+3*3+0*0=13.

Define the set T(x) to be the set of unique numbers that are produced by
repeatedly applying S to x. That is: S(x), S(S(x)), S(S(S(x))), etc...
For example, repeatedly applying S to 37:
S(37)=3*3+7*7=58.
S(58)=5*5+8*8=89.
S(89)=145.
S(145)=42.
S(42)=20.
S(20)=4.
S(4)=16.
S(16)=37.
Note this sequence will repeat so we can stop calculating now and:
T(37)={58,89,145,42,20,4,16,37}.
However, note T(x) may not necessarily contain x.

Implement a class SquareDigits, which contains a method smallestResult. The
method takes an int, n, as a parameter and returns the smallest int, x, such
that T(x) contains n.

The method signature is (be sure your method is public):
int smallestResult(int n);

TopCoder will ensure n is non-negative and is between 0 and 199 inclusive.

Examples:
If n=0: S(0) = 0, so T(0)={0}, so the method should return 0.

If n=2: T(0) through T(10) do not contain the value 2. If x=11, however:
S(11)=1*1+1*1=2, so T(11) contains 2, and the method should return 11.

If n=10: T(0) through T(6) do not contain 10. If x=7:
S(7)=49.
S(49)=97.
S(97)=130.
S(130)=10.
S(10)=1.
and it starts to repeat...
so T(7) is {49,97,130,10,1}, which contains 10, and the method should return 7.

n=1 -> x=1
n=19 -> x=133
n=85 -> x=5
n=112 -> x=2666
Definition
    
Class:
SquareDigits
Method:
smallestResult
Parameters:
int
Returns:
int
Method signature:
int smallestResult(int param0)
(be sure your method is public)


package com.ibm.billyao.topcoder;

import java.util.ArrayList;
import java.util.List;

public class SquareDigits {

public int smallestResult(int n) {
if(n == 0 || n == 1) return n;

List<Integer> vList = new ArrayList<Integer>();

for(int i = 2; true; i++) {
if(!vList.contains(i)) {
int gen = i;
while (true) {
gen = generate(gen);
if (gen == n) return i;

if(!vList.contains(gen)) {
vList.add(gen);
} else {
break;
}

}
}
}
}

private int generate(int input) {
int result = 0;

while(true) {
int val = input % 10;
input = input / 10;
result += val * val;
if(input == 0) break;
}

return result;
}

public static void main(String[] args) {
SquareDigits sd = new SquareDigits();

System.out.println("0 : " + sd.generate(0));
System.out.println("1 : " + sd.smallestResult(1));
System.out.println("5 : " + sd.smallestResult(5));
System.out.println("19 : " + sd.smallestResult(19));
System.out.println("13 : " + sd.smallestResult(13));
System.out.println("112 : " + sd.smallestResult(112));
System.out.println("85 : " + sd.smallestResult(85));
}
}

资源下载链接为: https://pan.quark.cn/s/502b0f9d0e26 计算机体系结构是计算机科学与技术领域极为关键的课程,它聚焦于硬件与软件的交互以及计算系统设计优化的诸多方面。国防科技大学作为国内顶尖工科院校,其计算机体系结构课程备受瞩目。本课件汇集了该课程的核心内容,致力于助力学生深入探究计算机工作原理。 课件内容主要涵盖以下要点:其一,计算机基本组成,像处理器(CPU)、内存、输入/输出设备等,它们是计算机硬件系统基石,明晰其功能与工作模式对理解计算机整体运行极为关键。其二,指令集体系结构,涵盖不同指令类型,如数据处理、控制转移指令等的执行方式,以及 RISC 和 CISC 架构的差异与优劣。其三,处理器设计,深入微架构设计,如流水线、超标量、多核等技术,这些是现代处理器提升性能的核心手段。其四,存储层次结构,从高速缓存到主内存再到外部存储器,探究存储层次缘由、工作原理及数据访问速度优化方法。其五,总线和 I/O 系统,学习总线协议,了解数据、地址、控制信号在组件间传输方式,以及 I/O 设备分类与交互方式,如中断、DMA 等。其六,虚拟化技术,讲解如何利用虚拟化技术使多个操作系统在同硬件平台并行运行,涉及虚拟机、容器等概念。其七,计算机网络与通信,虽非计算机体系结构主体,但会涉及计算机间通信方式,像 TCP/IP 协议栈、网络接口卡工作原理等。其八,计算机安全与可靠性,探讨硬件层面安全问题,如物理攻击、恶意硬件等及相应防御举措。其九,计算机体系优化,分析性能评估指标,如时钟周期、吞吐量、延迟等,学习架构优化提升系统性能方法。其十,课程习题与题库,通过实际题目训练巩固理论知识,加深对计算机体系结构理解。 国防科大该课程不仅理论扎实,还可能含实践环节,让学生借助实验模拟或真实硬件操作深化理解。课件习题集为学习者提供丰富练习机会,助力掌握课程内容。共享
内容概要:本文档详细介绍了基于单片机和C语言设计的10位数字密码锁项目。项目旨在提供一种高效、安全且智能化的门禁解决方案。硬件部分包括单片机(如STM32系列)、按键矩阵、液晶显示屏、蜂鸣器和电动锁等模块;软件部分涵盖输入处理、密码验证、状态管理和硬件控制四大模块。项目通过密码输入、验证、反馈等模块的设计,确保系统在复杂环境下稳定工作,防止未经授权的人员进入受保护区域。此外,系统还加入了防暴力破解、错误输入限制、密码加密等多项安全措施,并支持多用户管理、数据记录与审计等功能。文档还详细描述了硬件电路设计、代码实现、GUI设计等内容,并讨论了项目在智能家居、商业场所、高端酒店等多个领域的应用前景。 适合人群:具备一定单片机开发和C语言编程基础的研发人员,特别是从事嵌入式系统开发、智能硬件设计的专业人士。 使用场景及目标:①理解单片机与C语言在密码锁设计中的应用;②掌握按键矩阵、液晶显示、电动锁控制等硬件模块的设计方法;③学习密码验证、状态管理等软件模块的实现;④探索密码锁在智能家居、商业场所等领域的实际应用。 其他说明:项目不仅提供了详细的硬件电路设计和代码实现,还强调了系统的安全性、稳定性和用户体验。通过合理的设计和优化,确保密码锁在不同环境下的高效运行。未来,项目还可以扩展到支持生物识别、远程控制等智能化功能,进一步提升安全性和便捷性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值