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

数论(数论题)

时间:2016-11-09 19:58:30      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:leo   sort   indicator   测试数据   ++   ring   exp   print   .net   

数论

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

给出一个数N,含有数字1,2,3,4.把N的所有位数重新排列一下组成一个新数,使得它是7的倍数。

Input

第一行测试数据的总数T(T<=10000)。
接下来T行每行一个整数N,N不超过20位。

Output

如果存在这样的排列则随便输出一个符合的排列。
否则输出0.

Sample Input

2

1234

531234

Sample Output

4123

354123

 

//听说是腾讯某次面试题,有意思。错了很多次 Orz

由1 2 3 4 组成的排列可以余7为 0-6 所以,无论给的什么符合要求的数,都可以余7为0

举个栗子

a1b2c3d4

把 1 2 3 4 拿出来

使得该数字变为 abcd0000 不管余多少 加上一个 1234 的排列补充就好

数字很大,不能 long long 要当字符串处理

然而,还有个坑,如果除了1234都是0呢,所以,abcd 要降序排序一下,再去求 abcd0000 的余

还有,如果 str[0] 还是0 就得输出 1234 的一个排列余 7 为 0 的再把 0 输出

31ms

技术分享
 1 #include <stdio.h>
 2 #include <math.h>
 3 #include <string.h>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 int cmp(char x,char y)
 8 {return x>y;}
 9 
10 const int remain[7]={3241,3214,3124,2341,2314,1342,2134};
11 
12 int Yu7(char str[])
13 {
14     int len = strlen(str);
15     int y=0;
16     for(int i=0;i<len;i++)
17     {
18         y+=str[i]-0;
19         y%=7;
20         y*=10;
21     }
22     y/=10;
23     return y;
24 }
25 
26 int main()
27 {
28     int T;
29     char str[25];
30     char real[25];
31     scanf("%d",&T);
32     while (T--)
33     {
34         scanf("%s",&str);
35         int ti[5]={0};
36         int i,len = strlen(str);
37         int e,s=0;
38         for (i=0;i<len;i++)
39         {
40             e=str[i]-0;
41             //抽出四个数1234
42             if (e<=4&&e>=1&&ti[e]==0)
43             {
44                 ti[e]=1;
45                 continue;
46             }
47             real[s++]=str[i];
48         }
49         sort(real,real+s,cmp);
50         for (i=0;i<4;i++)
51         real[s++]=0;
52         real[s++]=\0;
53         int yu = Yu7(real);
54         if (yu==0) yu=7;
55         len-=4;
56         real[len]=\0;
57         if (real[0]!=0)
58         printf("%s%d\n",real,remain[7-yu]);
59         else
60         printf("%d%s\n",remain[0],real);
61     }
62     return 0;
63 }
View Code

 

 

 

 

数论(数论题)

标签:leo   sort   indicator   测试数据   ++   ring   exp   print   .net   

原文地址:http://www.cnblogs.com/haoabcd2010/p/6048027.html

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