POJ1426:Find The Multiple(算是bfs水题吧,投机取巧过的)

http://poj.org/problem?id=1426

Description

Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100 decimal digits.

Input

The input file may contain multiple test cases. Each line contains a value of n (1 <= n <= 200). A line containing a zero terminates the input.

Output

For each value of n in the input print a line containing the corresponding value of m. The decimal representation of m must not contain more than 100 digits. If there are multiple solutions for a given value of n, any one of them is acceptable.

Sample Input

2
6
19
0

Sample Output

10
100100100100100100
111111111111111111

大致题意:

给出一个整数n,(1 <= n <= 200)。求出任意一个它的倍数m,要求m必须只由十进制的‘0‘或‘1‘组成。

没看见是Special Judge题解:我感觉这题能够是投机取巧,输出结果根本就没有100位,之前以为是大数,一直没敢做,谁知是一个超级大坑题。还有给的测试数据给的那么大,害我一看测试数据就不敢做了。还有为什么我用STL中的queue用C++交超时,而用G++就A了,而自己写的结构体用C++交就过了。主要思想:和二叉树差不多,1->10,11;10->100,101,11->110,111.....就是q.push(t*10);q.push(t*10+1);
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
int n;
struct node
{
    long long int x;
} q[10000001];
struct node t,f;
void bfs()
{
    int s=0;
    int e=0;
    t.x=1;
    q[e++]=t;
    while(s<e)
    {
        t=q[s++];
        if(t.x%n==0)
        {
            printf("%lld\n",t.x);
            break;
        }
        f.x=t.x*10;
        q[e++]=f;
        f.x=t.x*10+1;
        q[e++]=f;
    }
}
int main()
{
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
        bfs();
    }
    return 0;
}

G++;

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <queue>
using namespace std;
int n;
long long t;
void bfs()
{
    queue<long long >q;
    q.push(1);
    while(!q.empty())
    {
        t=q.front();
        q.pop();
        if(t%n==0)
        {
            printf("%lld\n",t);
            break;
        }
        q.push(t*10);
        q.push(t*10+1);
    }
}
int main()
{
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
        bfs();
    }
    return 0;
}

时间: 2024-08-09 14:45:01

POJ1426:Find The Multiple(算是bfs水题吧,投机取巧过的)的相关文章

hdu 1312 Red and Black(BFS水题)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9684    Accepted Submission(s): 6021 Problem Description There is a rectangular room, covered with square tiles. Each tile is colore

hdu1240 bfs 水题

原题链接 思路:水题,直接搜 1 #include "map" 2 #include "queue" 3 #include "math.h" 4 #include "stdio.h" 5 #include "string.h" 6 #include "iostream" 7 #include "algorithm" 8 #define abs(x) x > 0

Yet Another Multiple Problem(bfs好题)

Yet Another Multiple Problem Time Limit : 40000/20000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other) Total Submission(s) : 2   Accepted Submission(s) : 1 Problem Description There are tons of problems about integer multiples. Despite the f

poj1426——Find The Multiple(BFS)

Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no mo

POJ3126 Prime Path bfs, 水题 难度:0

题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为e,每步可以做如下操作,把当前的s中的四位数上的某一位改变,比如1009可以变为2009,1008,1309,1049,然后检验结果是否为大于1000的质数,如果是,那就可以把s变为这个数. 思路 质数明显需要先处理出来,然后采用bfs获取结果即可. 感想 下次需要先计算空间复杂度, 1e8的空间复

POJ3278Catch That Cow(BFS+水题)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 64176   Accepted: 20156 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,00

HDU1548A strange lift BFS水题

没啥好说的,注意一下,走过的楼层不能再走,否则会陷入循环 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #inclu

POJ3287(BFS水题)

Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer Jo

迷宫问题 BFS入门水题

1102:迷宫问题 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:84 解决: 41 题目描述 小明置身于一个迷宫,请你帮小明找出从起点到终点的最短路程. 小明只能向上下左右四个方向移动. 输入格式 输入包含多组测试数据.输入的第一行是一个整数T,表示有T组测试数据. 每组输入的第一行是两个整数N和M(1<=N,M<=100). 接下来N行,每行输入M个字符,每个字符表示迷宫中的一个小方格. 字符的含义如下: 'S':起点 'E':终点 '-':空地,可以通过 '#':障碍,无法通