题目 poj – 1426(A题)
来源:互联网 发布:java判断txt文件结尾 编辑:程序博客网 时间:2024/06/07 01:44
Given a positive integer n, write a program to find out anonzero multiple m of n whose decimal representation contains only the digits 0and 1. You may assume that n is not greater than 200 and there is acorresponding m containing no more than 100 decimal digits.
Input
The input file may contain multiple test cases. Each linecontains a value of n (1 <= n <= 200). A line containing a zeroterminates the input.
Output
For each value of n in the input print a line containingthe corresponding value of m. The decimal representation of m must not containmore 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(n<=200);找个100位以内的由0和1组成的数,若找到多个,只输出其中一个就行.
思路:可以用dfs 写,也可以用bfs写,但要注意了,这道题找的数用longlong能存下.
起始位一定是 1 是吧,以后面跟0,或者1,假设另sum=1,它的下一个数是sum*10或sum*10+1,所以就有两条路可以走,代码如下,dfs ,bfs 的都有,还有我当时
不知道有longlong能存下时写的内存超限的代码
我写的代码适合初学者看
这是用dfs写的
#include<stdio.h>int f;long long n;void dfs(long long x,int step) //题意上说是一百位,但longlong就能存下 step 为位数{if(f||step>18) // 大于18位就return,我觉着一定是事先知道,能被200之内的正整return ;// 数由0 1组成的数,在18位之内一定能找到一个if(x%n==0){f=1; //若找到就返回主函数printf("%lld\n",x) ; return ;}dfs(x*10,step+1); // 这个有两种情况 这个是后面加0if(f) return ;dfs(x*10+1,step+1);//这个是后面加1if(f) return ;}int main(){int i,j,k,t;while(~scanf("%lld",&n)&&n){f=0;dfs(1,0); }return 0;}
用 bfs 写的:
#include<stdio.h>int n,f;struct st{long long sum;}stu[1111000];void bfs(){stu[0].sum=1;int star=0,end=1;while(star<end){for(int i=1;i<=2;i++){long long x;if(i==1)x=stu[star].sum*10;elsex=stu[star].sum*10+1;if(x>=9223372036854775807)continue;stu[end].sum=x;if(x%n==0){printf("%lld\n",x);return ;}end++;}star++;}}int main(){int i,j,k,t;while(~scanf("%d",&n)&&n){f=0;if(1%n==0){printf("1\n");continue;}bfs();}return 0;}
#include<stdio.h>int n,f;struct st{char s[300]; //用存0 和 1 组成的数int sum; // 这个是上一个由0 1组成的数除以n 剩下的余数;int l; //这个是多少位}stu[111000];void bfs(){stu[0].s[0]='1';stu[0].sum=1;stu[0].l=0;int star=0,end=1;while(star<end){int i,j;for(i=1;i<=2;i++){for(j=0;j<=stu[star].l;j++)stu[end].s[j]=stu[star].s[j];int sum=stu[star].sum;if(i==1){stu[end].s[j]='0';sum=sum*10;}else if(i==2){stu[end].s[j]='1';sum=sum*10+1;}int sum1=sum%n;stu[end].sum=sum1;stu[end].l=j;if(sum1==0){f=1;printf("%s\n",stu[end].s);return ;}if(j>100) return ;end++;}star++;}}int main(){int i,j,k,t;while(~scanf("%d",&n)&&n){f=0;if(1%n==0){printf("1\n");continue;}bfs();}return 0;}
错误原因:
我不知道用long long 能存下,还有就是小编能力有限,应该还有好的方法,望博友给提些建议;
- 题目 poj – 1426(A题)
- 题目:poj – 2395 (B题)
- 题目: poj – 3669(D题)
- POJ 题目 2488 A Knight's Journey(dfs)
- POJ 题目1740 A New Stone Game(博弈)
- POJ 题目1942 Paths on a Grid(排列组合,水)
- POJ 题目2356 Find a multiple(鸽巢原理)
- POJ 题目2449 Remmarguts' Date(K短路,A*+spfa)
- POJ 题目1691 Painting A Board(DFS)
- poj 题目3468 A Simple Problem with Integers (线段树)
- POJ 题目2031 Building a Space Station(最小生成树)
- POJ 题目3468 A Simple Problem with Integers(线段树成段更新,区间求和)
- poj题整理/题目分类
- POJ题目刷题路线
- POJ题目分类 基础题
- poj--题目分类(转)
- poj 搜索题目(转)
- POJ题目分类(搜索)
- Android常用库
- 关于mybatis-generator配置文件解析失败的坑
- HTTP POST请求报文格式分析与Java实现文件上传
- Git学习笔记 chapter02--版本回退
- Mycat源码篇 : 起步,Mycat源码阅读调试环境搭建
- 题目 poj – 1426(A题)
- DOM节点之nodeType
- Out of Hay
- XYNUOJ 1000 例题2-1 连乘计算
- spring声明式事务管理(注解实现)
- 使用taglib来提取在音乐文件中的封面信息
- smack操作openfire个人信息
- Java Web 之文件上传与下载
- 二分查找(范围统计)