遇到分数时不要定义为int型,因为1/2,1/3=0,要定义为float型或者double型

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 int main()
 4 {
 5     int n,k,i;
 6     double s,t;
 7     scanf("%d",&n);
 8     while(n--)
 9     {
10         s=0;
11         scanf("%d",&k);
12         for(i=1;;i++)
13         {
14             t=1.0/i;
15             s+=t; 
16             if(s>k) break;
17         }
18         printf("%d\n",i);
19     }
20     system("pause");
21     return 0;
22 }

相关文章:

  • 2021-11-11
  • 2021-06-20
  • 2021-09-07
  • 2021-12-01
  • 2021-09-13
  • 2021-07-12
  • 2021-09-03
猜你喜欢
  • 2022-12-23
  • 2021-12-25
  • 2021-08-09
  • 2021-08-07
  • 2022-12-23
  • 2021-11-08
相关资源
相似解决方案