您的位置:首页 > Web前端

HDU4161 Iterated Difference(模拟 + 递推)

2016-05-11 17:42 309 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4161

题意很简单,题也很水,训练时是队友做的,赛后自己写了写感觉没啥难度。

题意:给定一串数字,每次变化时每个数字等于这个数字与下一个数字的差的绝对值,问经过几次转变这一串数字变为相等的数字。直接模拟就好。

#include<iostream>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
using namespace std;
const int maxn = 25;
int a[maxn];
int main()
{
int n,kase = 0;
while(scanf("%d",&n) == 1 && n)
{
memset(a, 0, sizeof(a));
int vis = 0;
for(int i = 0; i < n; i++)
{
scanf("%d",&a[i]);
if(i && a[i] == a[i-1])
vis++;
}
if(vis == n-1)
{
printf("Case %d: 0 iterations\n",++kase);
continue;
}
else
{
int a0,sum = 0,flag = 0;
a0 = a[0];
while(1)
{

if(sum == 1000) break;
for(int i = 0; i < n; i++)
{
if(i == n-1) a[i] = abs(a[i] - a0);
else a[i] = abs(a[i] - a[i+1]);
}
int ans = 0;
for(int j = 0; j < n-1; j++)
{
if(a[j] == a[j+1])
ans++;
}
flag = 0;
sum++;
if(ans == n-1)
{
flag = 1;
break;
}
else
{
a0 = a[0];
}
}

if(flag)
printf("Case %d: %d iterations\n",++kase, sum);
else
printf("Case %d: not attained\n",++kase);
}

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: