c ++代码中出现意外输出,未获得预期结果

时间:2015-10-30 21:14:37

标签: c++

如果在小时和分针之间给出一个角度,我制作了这个程序来显示时钟中所有可能的时间..

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <math.h>
using namespace std;

int main(){

    //ifstream cin("input.txt");
    vector <float> hour , min , angle;
    string str;
    int h , m ;

    float diff, cal1 ,cal2 ,  i , j;;
    h = 11 ;
    m = 59;
    for ( i = 0 ; i <= h ; i++ ){
        for ( j = 0 ; j <= m ; j = j + 1){
            cal1 = (i*60/2) + (j/2); // hour angle
            cal2 = (j*6); // min angle;
            diff = fabs(cal1 - cal2) ;
            if ( diff > 180 ){
                diff = 360.0000 - diff ;
            }
            //cout << cal1 << " " << cal2 << " " << diff << endl;

            hour.push_back(i);
            min.push_back(int(j));
            angle.push_back(diff);
        }
    }

    int t , value , size;
    cin >> t;
    size = hour.size();
    while( t-- ){
        cin >> value;

        for ( int i = 0 ; i < size;i++ ){
            //cout << angle[i] << endl;
            if ( value == angle[i] ){
                if ( hour[i] > 9 && min[i] > 9 ){
                    cout << hour[i] <<":" << min[i] << endl;
                }
                else if ( hour[i] <= 9 && min[i] > 9 ){
                    cout << "0" << hour[i] <<":" << min[i] << endl;
                }
                else if ( hour[i] > 9 && min[i] <= 9 ){
                    cout << hour[i] <<":" << "0" << min[i] << endl;
                }
                else {
                    cout << "0" << hour[i] <<":" << "0" << min[i] << endl;
                }
            }
        }
    }
}

当我输入输入时有些共鸣:

    2
    60.0000
    30.0000

我得到了

   02:00
   10:00
   00:00

但我应该得到这个

    02:00
    10:00
    01:00
    11:00

当我输入此作为输入时

2
30.0000
60.0000

我得到了

    01:00
    11:00
    00.00

当我删除输入中的零并给它整数时,它显示正确的答案但是对于浮点数它会给出错误的答案。 我在ideone中运行了这段代码。它给出了同样的问题。

1 个答案:

答案 0 :(得分:2)

Value has a data type int but input demands float:

Try replacing int value with float value