#include "stdafx.h"
#include <iostream.h>
void moveArray(int *p,int n,int m); //moveArray函數(shù)
void remainded(int s[],int n);
int judgeOne(int s[],int n);
int getNext(int s[],int n,int m);
int main(int argc, char* argv[]) //主函數(shù)
{
printf("Hello World!\n");
int a[5]={1,2,3,4}; //初始化
//moveArray(a,5,2); //調(diào)用移動函數(shù)
//for(int i=0;i<5;i++) //輸出這個(gè)數(shù)組的n個(gè)元素
// cout<<*(a+i)<<" ";
//cout<<endl; //輸出回車換行
//作業(yè)10.5
//
//cout<<"KJ;L"<<getNext(a,5,4)<<endl;
remainded(a,4);
for(int i=0;i<4;i++){
cout<<a[i]<<endl;
}
return 0;
}
/**************moveArray函數(shù)******************/
//p:為整形數(shù)成組
//n:為囊型數(shù)組的長度
//m:為指定的順序后移的次數(shù)
/*********************************************/
void moveArray(int * p,int n,int m){
for(int i=0;i<m;i++) //循環(huán)移動m次
{
int temp;
temp=*(p+n-1); //最后一個(gè)數(shù)取出
for(int j=n-1;j>=1;j--) //前n-1個(gè)數(shù)后移
{
*(p+j)=*(p+j-1);
}
*(p)=temp; //把最后一個(gè)數(shù)放在數(shù)組的開頭
}
}
void remainded(int s[],int n){
int count=0,previous=0;
int position=0,next;
while(true)
{
if(judgeOne(s,n)) break;
position=getNext(s,n,s[position]);
next=position;
if(position==-1) break;
count++;
if(count==3) {
s[position]=0;
count=0;
position=next;
}
}
}
int judgeOne(int s[],int n){
int sum=0;
for(int i=0;i<n;i++){
if(s[i]!=0) sum++;
}
if(sum==1) return 1;
else return 0;
}
int getNext(int s[],int n,int m){
int next=-1;
for(int i=0;i<n;i++){
if(s[i]==m) break;
}
//cout<<"i="<<i;
for(int j=i+1;j<n;j++)
{
if(s[j]!=0) {
next=j;
break;
}
}
for(int k=0;k<i;k++){
if(s[k]!=0) {
next=k;
break;
}
}
return next;
}