uva 485 Pascal's Triangle of Death

题意:输出

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

1 6 15 20 15 6 1

1 7 21 35 35 21 7 1

知道出现10^60

总结:仍然是WA。单学会了很多关于double的处理方法,详细看注释

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <math.h>
using namespace std;

int main()
{
    cout<<"1"<<endl;
    cout<<"1 1"<<endl;
    cout<<"1 2 1"<<endl;
    cout<<"1 3 3 1"<<endl;
    vector<double> one;
    vector<double> two;
    one.push_back(1);
    one.push_back(3);
    one.push_back(3);
    one.push_back(1);
    
    while(1){
        double one_size = one.size();
        two.push_back(1);
        cout<<"1";
        for(double i = 1;i<=one_size-1;i++){
            double temp = one[i-1]+one[i];
            //double temp_int = floor(temp);//可以输出【temp】取整
            if(temp >= 10e60){
                for(double i = 1;i<two.size();i++){
                    cout.precision(0);//可以规定输出小数点数
                    cout<<" "<<std::fixed<<two[i];//可以不按照科学计数法输出
                }
                return 0;
            }
            two.push_back(temp);
            
        }
        two.push_back(1);
        for(double i = 1;i<two.size();i++){
            cout.precision(0);
            cout<<" "<<std::fixed<<two[i];
        }
        cout<<endl;
        one.clear();
        one=two;
        two.clear();
    }
    return 0;
}


正确代码(转)

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

int dp[1500][150] = {0};

int main(){
  dp[0][0] = 1;
  int row = 1, digit;
  bool exit = 0;
  do{
    for( int i = row-2 ; i > 0 ; i-- ){
      for( int j = 0 ; j < 150 ; j++ ){
        dp[i][j] += dp[i-1][j];
        dp[i][j+1] += dp[i][j]/10;
        dp[i][j] %= 10;
      }
    }
    for( int i = 0 ; i < row ; i++ ){
      for( digit = 149 ; digit >= 0 ; digit-- )
        if( dp[i][digit] ) break;
      if( digit >= 60 ) exit = 1;

      for( ; digit >= 0 ; digit-- )
        printf( "%d", dp[i][digit] );

      if( i != row-1 ) printf( " " );
      else printf( "\n" );
    }
    dp[row++][0] = 1;
  } while( !exit );
  return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值