7

ゼロで除算しておらず、コードに float データ型がありません。それでも浮動小数点例外が発生します。

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    unsigned long long int t,n;

    cin>>t;
    while(t--)
    {
        cin>>n;
        unsigned long long int deno = pow(10,n-1),count=2,sum = 0,f1=1,f2=1;

         while(1){
            sum = f1+f2;
            f1 = f2;
            f2 = sum;
            count++;
            if((int)(sum/deno)>0){
                cout<<count<<endl;
                 break;
             } 
        }

    }
    return 0;
}

同じことに関する以前のすべての質問には、ゼロで割るという同様の問題がありましたが、変数 deno は としてゼロになることはありませんn>=2

私の側からの以前の研究:

  1. float を含まないコードの「浮動小数点例外」
  2. 浮動小数点例外 C++ なぜ、それは何ですか?

問題文: https://www.hackerrank.com/contests/projecteuler/challenges/euler025/problem

2 つのテスト ケースに合格し、2 つ失敗します。すべて非表示のテスト ケースです。結果イメージ

入力 1 50 を渡すと、エラーを再現できます。詳細:

 GDB trace: Reading symbols from solution...done. [New LWP 15127] Core
 was generated by `solution'. Program terminated with signal SIGFPE,
 Arithmetic exception.
 #0  main () at solution.cc:23 
 23 if((int)(sum/deno)>0){
 #0  main () at solution.cc:23
4

1 に答える 1