您的位置:首页 > 其它

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

2015-08-13 09:25 531 查看
题目链接: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


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
Multiple input.
We have two integer n
(0<=n<=104 ) , t(0<=t<=105 ) in each row.
When n==-1 and t==-1 mean the end of input.

[align=left]Output[/align]
For each input , if the final number are divisible by
11, output “Yes”, else output ”No”. without quote.

[align=left]Sample Input[/align]

35 2

35 1
-1 -1

[align=left]Sample Output[/align]

Case #1: Yes

Case #2: No

[align=left]Source[/align]
2015
Multi-University Training Contest 7

题目大意:将前面的数加起来的得到的和接在后面,并判断最后得到的这个数是否可以被11整除。例如:23->一次变换后235->两次变换后23510
解题思路:想象一下,除以11是怎么除的,每次都是前面的先除存下余数加上后面的继续除。这样的话就算104也不在话下。这里要注意的是后面接上的数字不一定是一位数两位数or三位数。所以要特殊判断下,第一个剩下的余数要乘以几个10。

详见代码。(这个需要用G++提交)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>

using namespace std;

int fun(int n)
{
int sum=0;
while (n)
{
int a1=n%10;
sum+=a1;
n/=10;
}
return sum;
}

int fun1(int x)
{
int t=0;
while (x)
{
t++;
x/=10;
}
return t;
}

int fun2(int n)
{
int s=1;
for (int i=0;i<n;i++)
{
s*=10;
}
return s;
}

int main()
{
int n,t;
int flag=1;
while (~scanf("%d%d",&n,&t))
{
if (n==-1&&t==-1)
break;
int ans=n%11;
//cout<<ans<<endl;
int ss=fun(n);
for (int i=0; i<t; i++)
{
ans=ans*fun2(fun1(ss))+ss;//pow(10,fun1(ss))+ss;
//cout<<ans<<endl;
ans%=11;
//cout<<ans<<endl;
ss+=fun(ss);
//cout<<ss<<endl;
}
if (ans%11==0)
printf ("Case #%d: Yes\n",flag++);
else
printf ("Case #%d: No\n",flag++);
}
return 0;
}


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

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

详见代码。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
#define ll long long
const double eps = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;

int n,t;
int x,y,k;

int main ()
{
int a,b,c,d,e,ii=1;
while (scanf ("%d%d",&n,&t)==2)
{
if (n==-1&&t==-1)
break;
a = n/10000;
b = (n/1000)%10;
c = (n/100)%10;
d = (n/10)%10;
e = n%10;
//if (d!=0){k++; if(c!=0)k++; if(b!=0)k++; if(a!=0)k++;}
y = d+b;
x = c+a+e;

while (t--)
{
k = 0;
int p=0,q=0,m=x+y;
while (m)
{
k++;
if (k%2)
p += m%10;
else
q += m%10;
m /= 10;
}
//cout<<p<<" "<<q<<endl;cout<<x<<" "<<y<<endl;
if (k%2)
{
x += q;
y += p;
swap(x, y);
}
else
{
x += p;
y += q;
}
}
if ((x-y)%11)
printf ("Case #%d: No\n",ii++);
else
printf ("Case #%d: Yes\n",ii++);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: