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

博客讲述了在解决POJ Monthly的zby03问题时遇到的一个简单但耗费时间的错误,主要总结了三个教训:1) 当找不出错误时,通过穷举对比测试数据来定位;2) 使用while循环比双重循环更适合某些情况;3) 注意swap操作的正确使用。博主通过分析错误的测试案例发现循环条件判断的问题,并给出了修正后的解决方案。
摘要由CSDN通过智能技术生成

总结: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] = '\0';
    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 LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值