Pyramid of Glasses 酒杯金字塔 [CF-676B]

这里写图片描述
这里写图片描述

O(n^2)的做法:一次性地将酒倒入第一个杯子,然后两层循环扫描每个杯子,把酒量超过1的杯子中的酒向左下向右下溢出。(其实是模拟啊)
注意要用数组double型。

#include<iostream>
#include<cstdio>
using namespace std;
double a[1005][1005];
int t,n,ans;
int main()
{
    scanf("%d%d",&n,&t);
    a[1][1]=(double)t;
    for(int i=1;i<=n;i++)
      for(int j=1;j<=i;j++){
         if(a[i][j]>1){
            a[i+1][j]+=(a[i][j]-1)/2.0;
            a[i+1][j+1]+=(a[i][j]-1)/2.0;
            a[i][j]=1;
         } 
      }
    for(int i=1;i<=n;i++)
     for(int j=1;j<=i;j++)
      if(a[i][j]==1) ans++;
    printf("%d",ans);
    return 0;   
} 
原文地址:https://www.cnblogs.com/dfsac/p/7587906.html