基础强化报告新编范本

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

基础强化报告新编范本
附件1:

0121210680529
号:
课程设计
(基础强化训练)
题目Smith Numbers
学院计算机科学与技术
专业软件工程
班级Zy1202
姓名胡小意
指导教师段鹏飞
2014 年7 月11 日
课程设计任务书
学生姓名:胡小意专业班级:软件zy1202 指导教师:段鹏飞工作单位:计算机学院
题目: Smith Numbers
初始条件:
输入:
The input file consists of a sequence of positive integers, one integer per line.
Each integer will have at most 8 digits. The input is terminated by a line
containing the number 0.
输出:
For every number n > 0 in the input, you are to compute the smallest Smith
number which is larger than n,and print it on a line by itself. You can assume
that such a number exists.
要求完成的主要任务:(包括课程设计工作量及其
技术要求,以及说明书撰写等具体要求)
1、完成算法分析
2、给出对应的程序流程图
3、给出能正确实现的程序源码
5、给出试算截屏图
6、课程设计工作的分析与总结
7、给出不少于5篇参考文献。

时间安排:
2014-7-7到2014-7-11
指导教师签名:年月日
系主任(或责任教师)签名:年月日
目录
1 注册资料 (5)
2 选题描述 (5)
3 算法分析 (6)
3.1 构造逐位相加之和函数 (6)
3.2 求史密斯数 (5)
4 程序流程图 (6)
5 程序源码 (8)
6 试算截屏图 (9)
7 分析与总结 (9)
8 参考文献 (9)
1 注册资料
用户名:huxiaoyi
密码:123456789
选题题号:1142
2 选题描述
Description
While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Lehigh University,noticed that the telephone number of his brother-in-law H. Smith had the following peculiar property: The sum of the digits of that number was equal to the sum of the digits of the prime factors of that number. Got it? Smith's telephone number was 493-7775. This number can be written as the product of its prime factors in the following way:
4937775= 3*5*5*65837
The sum of all digits of the telephone number is 4+9+3+7+7+7+5= 42,and the sum of the digits of its prime factors is equally 3+5+5+6+5+8+3+7=42. Wilansky was so amazed by his discovery that he named this kind of numbers after his brother-in-law: Smith numbers.
As this observation is also true for every prime number, Wilansky decided later that a (simple and unsophisticated) prime number is not worth being a Smith number, so he excluded them from the definition.
Wilansky published an article about Smith numbers in the Two Year College Mathematics Journal and was able to present a whole collection of different Smith numbers: For example, 9985 is a Smith number and so is 6036. However,Wilansky was not able to find a Smith number that was larger than the telephone number of his brother-in-law. It is your task to find Smith numbers that are larger than 4937775!
Input
The input file consists of a sequence of positive integers, one integer per line. Each integer will have at most 8 digits. The input is terminated by a line containing the number 0.
Output
For every number n > 0 in the input, you are to compute the smallest Smith number which is larger than n,and print it on a line by itself. You can assume that such a number exists.
Sample Input
4937774
Sample Output
4937775
3 算法分析
3.1 构造逐位相加之和函数
要求大于n的最小的史密斯数,设此史密斯数为nn,由于史密斯数nn要满足质因数分解式每位相加之和等于其本身逐位相加之和,所以首先构建史密斯数每位相加的函数,其代码如下:
void get_sum(long n, int * sum){ //逐位求和
while(n != 0){
*sum += n%10;
n /= 10;
}
}
3.2 求史密斯数
首先我们了解到任意合数都可以分解为几个质因数的乘积并且给定合数的质因数分解表达式是唯一的。

根据上述性质,我们的质因数分解思路如下:
设被分解合数为N,则分解步骤如下:
•初始状态,M = 2
•用M试除N,若能整除,说明M为N的质因数,则更新N = N / M,M不变;若不能整除,则N不变,M++
本题中,算法描述如下:
{
sum1 = sum2 = cnt = 0;
get_sum(nn, &sum1);
n = nn; //nn 固定保存原N 值,n 用于整除后更新N 值 m = 2;
while(m <= sqrt(n)){
if(n%m == 0){
cnt++; //cnt 记录质因数个数,即标识了是否为素数 n = n/m;
get_sum(m, &sum2);
}
else m++;
}
get_sum(n, &sum2);
if(sum1 == sum2 && cnt != 0){
printf("%ld\n", nn);
break;
}
4 程序流程图
N
图1 get_sum 函数的流程图
输入任意
*sum += n%10; n 输出结果*sum
Y
N
图2 主函数的流程图 输入sum1,sum2,
是从键盘上输Ceil 用M 试除N ,若能整除,说明M 为N 的质因数,则更新N = N / M ,M 不变;若不能整除,则N 不变,M++,并求sum1和sum2 的值 sum1=输出所求出的满足要求的史密斯数nn 程序
5 程序源码
void get_sum(long n, int * sum){ //逐位求和
while(n != 0){
*sum += n%10;
n /= 10;
}
}
int main(){
int sum1, sum2;
long ceil, n, nn, m;
int cnt;
while(1){
scanf("%ld", &ceil);
if(ceil == 0) break;
for(nn = ceil+1; ; nn++){
sum1 = sum2 = cnt = 0;
get_sum(nn, &sum1);
n = nn; //nn固定保存原N值,n用于整除后更新N值
m = 2;
while(m <= sqrt(n)){
if(n%m == 0){
cnt++; //cnt记录质因数个数,即标识了是否为素数
n = n/m;
get_sum(m, &sum2);
}
else m++;
}
get_sum(n, &sum2);
if(sum1 == sum2 && cnt != 0){
printf("%ld\n", nn);
break;
}
}
}
return 0;
}
6 试算截屏图
图3 程序运行截图
7 分析与总结
史密斯数是一个很有趣的问题,一开始也许有一点找不到思路,但是仔细观察,发现史密斯数所包含的一些规律,问题就会得到解决。

有了思路之后,画出程序流程图有助于以后代码的编写。

首先要保证思路是正确的,后期的程序编写才能准确无误。

在解题的过程中我也认识到我的一些不足,基础的c程序编写还是有一些小毛病,但是发现后及时改正就能够正确的运行了。

但是解决这个问题的算法我也许不是最优的,在今后会多加实践,完善解题思路。

8 参考文献
[1] 李文新、郭炜、余华山. 程序设计导引及在线实践[M]. 北京:清华大学出版社
[2] 谭浩强. C程序设计[M]. 北京:清华大学出版社,2005.
[3] 严蔚敏,吴伟民. 数据结构[M]. 北京:清华大学出版社,1996.
[4] 钟珞. 计算机科学导论[M]. 武汉:武汉理
工大学出版社.
[5] 张富. C及C++程序设计[M]. 北京:人民邮电出版社.
本科生课程设计成绩评定表
班级:软件zy1202姓名:胡小意学号:0121210680529 序号评分项目满分实得分
1 学习态度认真、遵守纪律10
2 设计分析合理性10
3 设计方案正确性、可行性、创造性20
4 设计结果正确性40
5 设计报告的规范性10
6 设计验收10
总得分/等级
评语:
及格(60-69分)、60分以下为不及格
指导教师签名:
20014 年月日。

相关文档
最新文档