UVa 725 Division 【暴力入门】

题目链接:点击打开链接

中文翻译见紫书182页

一开始暴力想到了10个for循环,接着看了看书,5个就够了,写了会感觉好low啊。写完后有bug,debug时感觉好熟悉,突然想到了第二章的习题2-6,果断把5个for循环删掉,改为用一个for从1234到49385循环(abcde / fghij= N, N最小为2,只要循环到98765/2+1就行<其实加个特判条件后就无所谓了> )。

这里判断数字是否重复用到了字符数组,也算是个小技巧吧

AC代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
char ans[11] = "0123456789";
char c[11];
int main()
{
    int n, tem, flag, cou = 0;
    while(~scanf("%d",&n) && n)
    {
        if(cou) cout << endl;
        flag = 0;
        memset(c, 0, sizeof(c));
        for(int i = 1234; i <= 49385; ++i)
        {
            if(i * n > 98765) break;
            tem = i * n;
            if(i > 10000) sprintf(c, "%d%d", i, tem);
            else sprintf(c, "%d%d%d", 0, i, tem);
            sort(c,c+10);
            if(strcmp(c, ans) == 0)
            {
                flag = 1;
                printf("%d / %05d = %d\n",tem, i, n);
            }
        }
        if(!flag) cout << "There are no solutions for " << n <<"." << endl;
        cou = 1;
    }
    return 0;
}

暴力也是需要技巧的。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值