SPOJ Complete the Sequence Solution | Classical Problem Solution
Sphere Online Judge Solution Complete the Sequence Solution
Sphere Online Judge Main Problem Link - Complete the Sequence Solution - http://www.spoj.com/problems/CMPLS
Problem Name: SPOJ Problem Classical 8 - Complete the sequence
Problem Number : SPOJ Problem Classical 8 - Complete the sequence
Online Judge : SPOJ Solution
Solution Language : C,CPP
Level : Classical
SPOJ Complete the sequence Solution/Code in CPP:
#include <cstdio> #include <numeric> using namespace std; bool check(int *p, int &n){ for(int i=1;i<n;i++) if((*(p+i)) != (*p)) return false; return true; } int main(){ int T,S,C,n,a[100],start[101],sum; scanf("%d",&T); for(int tc=1;tc<=T;tc++){ scanf("%d %d",&S,&C); for(int i=0;i<S;i++) scanf("%d",&a[i]); n=S; while(!check(a,n)){ start[n]=a[0]; for(int i=0;i<n-1;i++) a[i]=a[i+1]-a[i]; n--; } for(int i=n;i<n+C;i++) a[i]=a[0]; for(int i=n;i<S;i++){ sum=accumulate(a,a+i+C,0)+start[i+1]; for(int j=i+C;j>=0;j--){ a[j]=sum; if(j>0) sum-=a[j-1]; } } for(int i=S;i<S+C;i++){ if(i>S) printf(" "); printf("%d",a[i]); } printf("\n"); } return 0; }
Tags: Sphere Online Judge Solutions, SPOJ Complete the Sequence Solution , Sphere online Judge Solution Complete the Sequence Solution
No comments:
Post a Comment