2018.9.3 keep秋招的一个编程题

1<=m,n<=10000

求n能整除m的所以组合(按n中的顺序排)

例:n=1234,m=4

12,24,124,4

不能有(32,432)这种排序

求大神用python帮写下。


#秋招##Keep#
全部评论
import java.util.Scanner; import java.util.ArrayList; import java.util.List; public class KeepTest {      public static void main(String[] args) {          Scanner in = new Scanner(System.in);          while(in.hasNext()) {              String str = in.nextLine();              int n = Integer.parseInt(in.nextLine());              List<String> list = new ArrayList<String>();              System.out.println(GetSub(str, 0, list, n));              }          }            public static List GetSub(String str,int m, List<String> list, int n) {                    if(m<0||m>str.length()-1||str==null) {              return null;          }          if(str.length()==1) {              return list;          }          int m1=m;          for(m1=0; m1<str.length();m1++) {              StringBuffer sb = new StringBuffer(str);              sb.delete(m1,m1+1);              if(Integer.valueOf(sb.toString())%n==0&&!list.contains(sb.toString())) {                  list.add(sb.toString());              }              if(sb.toString().length()>1) {                  GetSub(sb.toString(),0,list,n);              }          }          return list;      }       }
点赞 回复 分享
发布于 2018-09-05 16:07
n,m=map(int,input().split()) c=[] def dfs(a):     for i in range(len(a)):         if len(a)==1:             return         b=a[:]         b.pop(i)         xx=int(''.join(b))         if xx%m==0 and  xx not in c:             c.append(xx)         dfs(b)     return c print(dfs(list(str(n))))
点赞 回复 分享
发布于 2018-09-04 16:53
回溯
点赞 回复 分享
发布于 2018-09-04 16:37

相关推荐

07-31 14:17
门头沟学院 Java
点赞 评论 收藏
分享
勇敢牛牛不怕困难,希望能过初筛
投递韶音科技等公司10个岗位
点赞 评论 收藏
分享
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-29 11:57
投递浪潮等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
8
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务