gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:18:00 26 4
gpt4 key购买 nike

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

#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

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

最佳答案

值的数据类型为int,但输入需要float:

尝试将 int value 替换为 float value

关于c++ - c++代码中的意外输出,未获得预期结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33445038/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com