本文主要是介绍HDU4161 Iterated Difference(模拟 + 递推),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目链接: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);elseprintf("Case %d: not attained\n",++kase);}}return 0;}
这篇关于HDU4161 Iterated Difference(模拟 + 递推)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!