Java 代码
import java.util.Scanner;
public class Main{
public static void main(String[]args)
{
Scanner in = new Scanner(System.in);
int n,last = 0,ans = 0;
while(true)
{
n = in.nextInt();
if(n==2) {last +=2;ans += last;} //上一个是中心
else if(n==1) {ans+=1;last = 0;} //不是中心,重置.
else break;
}
System.out.println(ans);
}
}