码迷,mamicode.com
首页 > 其他好文 > 详细

uva 10396

时间:2015-05-04 08:45:37      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

题意:给出一个数字n,4或者6或者8,然后找到两个n/2位的数字相乘,得到的数字刚好是选出的两个数字的位数上的数组合得到的。要求两个数字不能同时整除10,且相乘得到的数字不能是奇数。
题解:枚举两个数字并打表。

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <set>
using namespace std;
set<int> res6, res8;
int n, vis[10];

void solve(int cnt) {
    int st, en, minn, maxx;
    if (cnt == 3) {
        st = 100;
        en = 999;
        minn = 100000;
        maxx = 999999;
    }
    else {
        st = 1000; 
        en = 9999;
        minn = 10000000;
        maxx = 99999999;
    }
    for (int i = st; i <= en; i++) {
        for (int j = i; j <= en; j++) {
            if (i % 10 == 0 && j % 10 == 0)
                continue;
            if (i * j > maxx || i * j < minn || (i * j) % 2)
                continue;
            memset(vis, 0, sizeof(vis));
            int temp = j;
            while (temp != 0) {
                vis[temp % 10]++;
                temp /= 10;
            }
            temp = i;
            while (temp != 0) {
                vis[temp % 10]++;
                temp /= 10;
            }
            temp = i * j;
            while (temp != 0) {
                vis[temp % 10]--;
                temp /= 10;
            }
            int flag = 0;
            for (int k = 0; k < 10; k++)
                if (vis[k] != 0) {
                    flag = 1;
                    break;
                }
            if (!flag && cnt == 3)
                res6.insert(i * j); 
            if (!flag && cnt == 4)
                res8.insert(i * j);
        }
    }
}

int main() {
    solve(3);
    solve(4);   
    while (scanf("%d", &n) == 1) {
        if (n == 4)
            printf("1260\n1530\n6880\n");
        else if (n == 6) {
            for (set<int>::iterator it = res6.begin(); it != res6.end(); it++)
                cout << *it << endl;
        }
        else {
            for (set<int>::iterator it = res8.begin(); it != res8.end(); it++)
                cout << *it << endl;
        }
        printf("\n");
    }
    return 0;
}

uva 10396

标签:

原文地址:http://blog.csdn.net/hyczms/article/details/45461427

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!