POJ Monthly,zby03一个简单的问题:历时两个多小时得弱智bug(找错方法总结)(第三次作业

阅读: 评论:0

POJ Monthly,zby03一个简单的问题:历时两个多小时得弱智bug(找错方法总结)(第三次作业

POJ Monthly,zby03一个简单的问题:历时两个多小时得弱智bug(找错方法总结)(第三次作业

总结:1、找不出错误时,穷举题解测试数据跟错误方法对比,输出不一样的值,再单独分析
2、截止到找到某个数,应该用while更靠谱,及时终止;别乱用双重循环
3、swap比我靠谱

As we known, data stored in the computers is in binary form. The problem we discuss now is about the positive integers and its binary form.
Given a positive integer I, you task is to find out an integer J, which is the minimum integer greater than I, and the number of '1’s in whose binary form is the same as that in the binary form of I.
For example, if “78” is given, we can write out its binary form, “1001110”. This binary form has 4 '1’s. The minimum integer, which is greater than “1001110” and also contains 4 '1’s, is “1010011”, i.e. “83”, so you should output “83”.
输入
One integer per line, which is I (1 <= I <= 1000000).
A line containing a number “0” terminates input, and this line need not be processed.
输出
One integer per line, which is J.

题目本身不难,贪心算法方面不存在问题,问题在于细节处理以及错误的分析过程

错误题解(简单模拟,但存在特殊解处理不当,导致无法ac)

char a[65];
char *tobin(int x) {memset(a, 0, sizeof(a));int i = 0;while (x != 0) {a[i] = x % 2 + '0';x /= 2;i++;}a[i] = '';return a;
}
int huanyuan(char a[]) {int i, ans = 0;for (i = strlen(a) - 1; i >= 0; i--) {ans *= 2;ans += a[i] - '0';}return ans;
}
int main() {
#ifdef LOCALfreopen("", "r", stdin);freopen("", "w", stdout)

本文发布于:2024-01-29 02:49:05,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170646774812163.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:作业   弱智   多小时   找错   两个
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23