C实现topcoder的SquareDigits(编译通过)
来源:互联网 发布:怎么在淘宝找开病假条 编辑:程序博客网 时间:2024/06/06 17:04
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)
//以下是C代码
#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>
#include <string.h>
#define N 50
main()
{
int num=0;
int rem=0;
int sum=0;
int i=0,j=0;
int t;
int pnum[N]={0};
printf("输入整数:\n");
scanf("%d",&num);
t=num;
while(num<0 || num>199)
{
printf("输入不合法。\n");
system("pause");
exit(1);
}
loop1:
do
{
rem = num%10;
sum += rem*rem;
num =num/10;
}while(num!=0);
//printf("sum=%3d ",sum); system("pause"); //测试sum输出是否正确
//printf("i=%d ",i);
num=sum; //将sum赋值给num,num为新产生的数
//printf("\n");
//printf("pnum[%d]=%d ",i,sum); //测试存储的sum是否正确
//printf("sum0=%d ",sum);system("pause"); //测试sum=0
if(i==N)
{
printf("在50个范围内没有找到,退出");
system("pause");
exit(1);
}
// 将sum和t(即输入的原始数字)比较是否相等 ,相等写入数字后停止计算
while(t==sum)
{
pnum[i]=sum;
for(j=0;j<=i;j++)
{
printf("%d ",pnum[j]);
}
printf("\n出现和原始数字相等,退出");
system("pause");
exit(1);
}
//将sum和已经计算出写入到数组的数字做比较判断,判断是否重复,重复则停止写入到数组
if(i==0) //当产生第一个数时,直接存储
{
// printf("------");
pnum[i]=sum;i++;
sum=0;goto loop1;
}
else
{
for(j=0;j<i;j++)
{
if(pnum[j]==sum)
{
//printf("++++++++\n");
goto loop2;
}
}
pnum[i]=sum;
sum=0; i++;
//printf("========");
goto loop1;
}
loop2:
for(j=0;j<i;j++)
{
printf("%d ",pnum[j]);
}
system("pause");
}
- C实现topcoder的SquareDigits(编译通过)
- TopCoder SquareDigits
- C实现KnightsTour(编译通过)
- 单链表的C语言实现[GCC编译通过]
- 通过下载的内核编译hello.c
- 魔方简单实现(c语言,tc2.0编译通过)不含解魔方的算法
- 双链表C语言实现[GCC编译通过]
- 队列C语言实现[GCC编译通过]
- 栈C语言实现[GCC编译通过]
- C语言-计算器实现(Dev c++编译通过)
- 冒泡排序C语言实现(编译通过)
- 写一个只能C编译通过,而C++不能编译通过的函数。
- C#: 通过动态编译获取字符串所表达的值
- C#: 通过动态编译获取字符串所表达的值
- Android上通过gcc编译普通的C程序
- java通过jni调用c编译的dll或so
- 过桥时间最短的算法实现(TopCoder)
- 通过SecureCRT实现window和linux的编译交互
- C#listbox用法
- 三层交换机创建VLAN方法
- 举例说明用LinkedList实现栈的方法
- Nokia补牢,为时未晚
- MFC CString 操作指南
- C实现topcoder的SquareDigits(编译通过)
- 深入理解Android Activity生命周期
- 用gprof分析性能初步
- GDB调试工具学习笔记
- spoj 2916 Can you answer these queries V
- Ubuntu探秘之二:软件包管理系统
- JQuery使用与细节总结
- 学习JQuery(一)
- 文章正在保存。。