用c/c++代碼求解時分秒針重合次數

來源:互聯網
上載者:User

[liuzy@linux237 testcode]$ vi testhms.c

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #define hdg 30 //時針每轉動一下走30度
      4 #define mdg 6//分針每轉動一下走6度
      5 #define sdg 6//秒針每轉動一下走6度
      6 int gethourdg(int h,int m)
      7 {
      8   int result = h* 30 + m*0.5;//分針每轉動一下 時針轉0.5度 30/60
      9   return result;
     10 }
     11
     12 int getminutedg(int m,int s)
     13 {
     14   return m * 6 + s * 0.5;//秒針每轉動一下 分針轉0.5 30/60
     15 }
     16
     17 int getsecdg(int s)
     18 {
     19   return s * 6;
     20 }
     21
     22 int main()
     23 {
     24    for(int i=0;i<=12;i++)
     25    {
     26      for(int j=0;j<=59;j++)
     27      {
     28        for(int k=0;k<=59;k++)
     29        {
     30           if(i>12) i = i-12;
     31           int hourdg=gethourdg(i,j)%360;
     32           int minutedg=getminutedg(j,k)%360;
     33           int secdg=getsecdg(k)%360;
     34           if((hourdg == minutedg) && (minutedg == secdg))
     35           {
     36             printf("hour=%d minute= %d second = %d/n",i,j,k);
     37          }
     38        }
     39     }
     40    }
     41
     42 }
~

[liuzy@linux237 testcode]$ g++ testhms.c
testhms.c: In function `int gethourdg(int, int)':
testhms.c:8: warning: converting to `int' from `double'
testhms.c: In function `int getminutedg(int, int)':
testhms.c:14: warning: converting to `int' from `double'
[liuzy@linux237 testcode]$ a.out
hour=0 minute= 0 second = 0
hour=12 minute= 0 second = 0
[liuzy@linux237 testcode]$

 

從結果我們可以看出,重合兩次,分別是0點和12點。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.