输出结果不符点 英文

1015人阅读
Algorithm(8)
Write an algorithm to determine if a number is “happy”.
A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.
Example: 19 is a happy number
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
The idea is just simple, the hints of HashMap and Recursion will help you a lot.
Java Solution( Wrong Answer)
public class Solution {
private static Map&Integer, Integer& map = new HashMap&Integer, Integer&();
public boolean isHappy(int n) {
if (n == 1) {
return true;
} else if (map.containsKey(n)) {
return false;
map.put(n, map.size());
String str = n + "";
char[] c = str.toCharArray();
int temp = 0;
for (int i = 0; i & c. i++) {
temp += Integer.parseInt(c[i]+"") * Integer.parseInt(c[i]+"");
return isHappy(temp);
But the problem is that the answer of LeetCode is different from that of local IDE, which means when I iuput 10 in my local IDE, the output is true. Details is in the picture below:
The solution is : try to declare the class variables as class instance variables instead of c That’s because the judger runs all test cases in one go.
Reference:
Java Solution( Accepted)
public class Solution{
private Map&Integer, Integer& map = new HashMap&Integer, Integer&();
public boolean isHappy(int n) {
if (n == 1) {
return true;
} else if (map.containsKey(n)) {
return false;
map.put(n, map.size());
String str = n + "";
char[] c = str.toCharArray();
int temp = 0;
for (int i = 0; i & c. i++) {
temp += Integer.parseInt(c[i]+"") * Integer.parseInt(c[i]+"");
return isHappy(temp);
Online Judge:
If you have any questions or any bugs are found, please feel free to contact me.
Your comments and suggestions are welcome!
&&相关文章推荐
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:14829次
排名:千里之外
原创:20篇
(2)(1)(1)(2)(1)(1)(3)(1)(6)(2)同样一个数组为什么分开输出,和一个一个输出的结果咋不一样,该如何处理_C++,C语言_ThinkSAAS
同样一个数组为什么分开输出,和一个一个输出的结果咋不一样,该如何处理
同样一个数组为什么分开输出,和一个一个输出的结果咋不一样,该如何处理
内容来源: 网络
同样一个数组为什么分开输出,和一个一个输出的结果咋不一样#include&iostream&#include&ctime&#include&windows.h&void main(){char *arr[10]; time_ t=time(NULL); for(int i=0;i&4;i++) { cin&&y; time_ t=time(NULL);
arr[i]=ctime(&t); cout&&arr[i];
} for(int i=0;i&4;i++) { cout&&arr[i]; }}各位大侠看一下,同样arr[i]为什么输出的结果不一样------解决方案--------------------输出样的结果,都是一样的吧,运行了一下
------解决方案--------------------ctime应该是用一个静态字符数组容纳时间文本的,所以每次你得到的返回值都是指向同一个位置的指针。这样在分别输出的时候你看到的是每一次转换出的时间值,而最后一次显示时因为它们都指向同一个位置,而该位置存放的是最后的转换字符串,你看到的就都是最后的那个串。要改变这个问题你要分别为每个值申请一个存储空间,然后用strcpy将ctime的返回串拷贝进去,并将串首指针存放到你的arr里。
------解决方案--------------------C/C++ code
#include&iostream&
#include&ctime&
#include&windows.h&
void main()
char *arr[10],*p;
t=time(NULL);
for(int i=0;i&4;i++)
t=time(NULL);
p=new char[20];
strcpy(p,ctime(&t));
cout&&arr[i];
for(int i=0;i&4;i++)
cout&&arr[i];
------解决方案--------------------???我又反复试验了几次,没发现有什么问题。难道是你粘贴的时候没把原来的东西清理干净?
------解决方案--------------------没有QQ。输入一个数字按一次回车,别按多了。
PHP开发框架
开发工具/编程工具
服务器环境
ThinkSAAS商业授权:
ThinkSAAS为用户提供有偿个性定制开发服务
ThinkSAAS将为商业授权用户提供二次开发指导和技术支持
让ThinkSAAS更好,把建议拿来。
开发客服微信新人求教!!自己算出来的结果和输出结果不一样,求大神指教【java吧】_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:612,876贴子:
新人求教!!自己算出来的结果和输出结果不一样,求大神指教收藏
class B {static int sum = 0;void setN(int n) {this.n =}int getSum() {for(int i =1; i&=n; i++)sum = sum+i;}}public class TestA {public static void main(String args[]) {B b1 = new B(), b2 = new B();b1.setN(3);b2.setN(5);int s1 = b1.getSum();int s2 = b2.getSum();System.out.println(s1+s2);}}输出后是27,可是自己算就算成21,求前辈们指教下!万分感谢啊~
java做为IT行业主流技术,是很受企业青睐的.达内java培训O基础4-16周您精通速成班!更为抢手.达内IT培训,专设java学习课程「入门+进阶+精通」,学习+就业!一步搞定!
表示很郁闷,书上的只有答案,但没解析过程。。。。
我自己笔算出来是21,可是不知道错在哪,希望大神们指教下
给大神奉上黑猫的COS图
静态变量是属于类的 是这个类的所有对象共享的 显然不符合楼主逻辑所要
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或问题对人有帮助,内容完整,我也想知道答案
问题没有实际价值,缺少关键内容,没有改进余地
文件名为const.py在另一个文件引入const模块时会发现第一个print输出const,而第二个print输出None.为什么?
#!/usr/bin/python2.7
#coding:utf-8
import sys
class _const:
class ConstError(TypeError): pass
def __setattr__(self, key, val):
if self.__dict__.has_key(key):
raise self.ConstError, "can't rebind const(%s)" % key
self.__dict__[key] = val
print __name__
sys.modules[__name__] = _const()
print __name__
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
本来sys.modules[__name__]是&module 'const' from 'const.py'&
你执行sys.modules[__name__] = _const()等于将constmodule卸载再导入_const()这个东东
但实际这样导入是不对的,_const()只是个对象而已,跟module根本不搭噶
所以constmodule实际上被卸载了,所以打印__name__是None,因为它根本没被导入
分享到微博?
Hi,欢迎来到 SegmentFault 技术社区!⊙▽⊙ 在这里,你可以提出编程相关的疑惑,关注感兴趣的问题,对认可的回答投赞同票;大家会帮你解决编程的问题,和你探讨技术更新,为你的回答投上赞同票。
明天提醒我
关闭理由:
删除理由:
忽略理由:
推广(招聘、广告、SEO 等)方面的内容
与已有问题重复(请编辑该提问指向已有相同问题)
答非所问,不符合答题要求
宜作评论而非答案
带有人身攻击、辱骂、仇恨等违反条款的内容
无法获得确切结果的问题
非开发直接相关的问题
非技术提问的讨论型问题
其他原因(请补充说明)
我要该,理由是:
扫扫下载 App其他回答(2)
估计是因为fork启动了一个新的进程的问题
&试试这个看看
收获园豆:20
园豆:15824
你应该是使用&这个重定向导致的,你使用&&重定向应该就和屏幕输出一样了。
收获园豆:30
&&&您需要以后才能回答,未注册用户请先。

我要回帖

更多关于 csgo不符合封测资格 的文章

 

随机推荐