转载

hdu 5373 The shortest problem(杭电多校赛第七场)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5373

The shortest problem

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 995    Accepted Submission(s): 498

Problem Description

In this problem, we should solve an interesting game. At first, we have an integer n, then we begin to make some funny change. We sum up every digit of the n, then insert it to the tail of the number n, then let the new number be the interesting number n. repeat it for t times. When n=123 and t=3 then we can get 123->1236->123612->12361215.

Input

Multiple input.

We have two integer n (0<=n<=

10 4 ) , t(0<=t<=

10 5 ) in each row.

When n==-1 and t==-1 mean the end of input.

Output

For each input , if the final number are divisible by 11, output “Yes”, else output ”No”. without quote.

Sample Input

35 2
35 1
-1 -1

Sample Output

Case #1: Yes
Case #2: No

Source

2015 Multi-University Training Contest 7

题目大意:将前面的数加起来的得到的和接在后面,并判断最后得到的这个数是否可以被11整除。例如:23->一次变换后235->两次变换后23510

解题思路:想象一下,除以11是怎么除的,每次都是前面的先除存下余数加上后面的继续除。这样的话就算10 4 也不在话下。这里要注意的是后面接上的数字不一定是一位数两位数or三位数。所以要特殊判断下,第一个剩下的余数要乘以几个10。

详见代码。

 1 #include <iostream>  2 #include <cstdio>  3 #include <cstring>  4 #include <cmath>  5   6 using namespace std;  7   8 int fun(int n)  9 { 10     int sum=0; 11     while (n) 12     { 13         int a1=n%10; 14         sum+=a1; 15         n/=10; 16     } 17     return sum; 18 } 19  20 int fun1(int x) 21 { 22     int t=0; 23     while (x) 24     { 25         t++; 26         x/=10; 27     } 28     return t; 29 } 30  31 int fun2(int n) 32 { 33     int s=1; 34     for (int i=0;i<n;i++) 35     { 36         s*=10; 37     } 38     return s; 39 } 40  41 int main() 42 { 43     int n,t; 44     int flag=1; 45     while (~scanf("%d%d",&n,&t)) 46     { 47         if (n==-1&&t==-1) 48             break; 49         int ans=n%11; 50         //cout<<ans<<endl; 51         int ss=fun(n); 52         for (int i=0; i<t; i++) 53         { 54             ans=ans*fun2(fun1(ss))+ss;//pow(10,fun1(ss))+ss; 55             //cout<<ans<<endl; 56             ans%=11; 57             //cout<<ans<<endl; 58             ss+=fun(ss); 59             //cout<<ss<<endl; 60         } 61         if (ans%11==0) 62             printf ("Case #%d: Yes/n",flag++); 63         else 64             printf ("Case #%d: No/n",flag++); 65     } 66     return 0; 67 }

还有另外一种比较省时间的代码。

能被11整除的数的特征把一个数由右边向左边数,将奇位上的数字与偶位上的数字分别加起来,再求它们的差,如果这个差是11的倍数(包括0),那么,原来这个数就一定能被11整除。

详见代码。

 1 #include <iostream>  2 #include <cstdio>  3 #include <cstring>  4 #include <stack>  5 #include <queue>  6 #include <map>  7 #include <set>  8 #include <vector>  9 #include <cmath> 10 #include <algorithm> 11 using namespace std; 12 #define ll long long 13 const double eps = 1e-6; 14 const double pi = acos(-1.0); 15 const int INF = 0x3f3f3f3f; 16 const int MOD = 1000000007; 17  18 int n,t; 19 int x,y,k; 20  21 int main () 22 { 23     int a,b,c,d,e,ii=1; 24     while (scanf ("%d%d",&n,&t)==2) 25     { 26         if (n==-1&&t==-1) 27             break; 28         a = n/10000; 29         b = (n/1000)%10; 30         c = (n/100)%10; 31         d = (n/10)%10; 32         e = n%10; 33         //if (d!=0){k++; if(c!=0)k++; if(b!=0)k++; if(a!=0)k++;} 34         y = d+b; 35         x = c+a+e; 36  37         while (t--) 38         { 39             k = 0; 40             int p=0,q=0,m=x+y; 41             while (m) 42             { 43                 k++; 44                 if (k%2) 45                     p += m%10; 46                 else 47                     q += m%10; 48                 m /= 10; 49             } 50             //cout<<p<<" "<<q<<endl;cout<<x<<" "<<y<<endl; 51             if (k%2) 52             { 53                 x += q; 54                 y += p; 55                 swap(x, y); 56             } 57             else 58             { 59                 x += p; 60                 y += q; 61             } 62         } 63         if ((x-y)%11) 64             printf ("Case #%d: No/n",ii++); 65         else 66             printf ("Case #%d: Yes/n",ii++); 67     } 68     return 0; 69 }
正文到此结束
Loading...