2011-05-22 37 views
1

嘿,我有最困難的時間搞清楚如何顯示這個結果。舉例來說,我輸入一個數字,例如59.基於該數字,我會得到1周(s)2天和5小時的剩餘結果。這當然假定一週有40個小時,1天有7個小時才能得到這個輸出。任何幫助正確的方向將是有益的。到目前爲止,我已經設定,就像這樣:C modulos and Remainders

scanf("%d %d %d", &totalWeeksWorked, &totalDaysWorked, &totalHoursWorked); 
+2

什麼'scanf'應該在那裏做? – 2011-05-22 03:45:09

+0

我只是讓它掃描將顯示的三個變量輸出。還有更多的代碼我只想顯示一小段代碼,對於不夠清晰感到遺憾。 – theGrayFox 2011-05-22 03:48:40

+0

@ user520720:您是否打算使用printf打印將顯示的輸出? – 2011-05-22 04:06:59

回答

0
#include <stdio.h> 

int const HOURS_PER_WEEK = 40; 
int const HOURS_PER_DAY = 7; 

int main() { 
    int total_hours = 59; // this is the input you get 

    int remaining = total_hours; // 'remaining' is scratch space 

    int weeks = remaining/HOURS_PER_WEEK; 
    remaining %= HOURS_PER_WEEK; 

    int days = remaining/HOURS_PER_DAY; 
    remaining %= HOURS_PER_DAY; 

    int hours = remaining; 

    printf("%d hours = %d weeks, %d days, %d hours\n", 
     total_hours, weeks, days, hours); 

    return 0; 
} 
+0

非常好!我真的很喜歡你的結構。 – theGrayFox 2011-05-22 04:25:38

2

這不是最快的方法,但也許是最能說明問題:

int numodweeks = input/(7*24); 
int numofdays =input/24; 
int numofhours = 24 - (input/24); 

使用模:

 int numofweeks = input/(7*24); 
     int numofdays = (input%numofweeks)/7; 
     int numofhours = (input%(numofdays*24)); 

然後向他們展示你想要的。

+0

我起初嘗試過,但我試圖找出一種方法來使用餘數運算符來顯示。一旦完成並使用上述方法,我可能會重新考慮這一點。欣賞輸入雖然! – theGrayFox 2011-05-22 03:45:55

+0

是使用該操作員的底層操作系統,還是您認爲這是一個很好的方法? – soandos 2011-05-22 03:46:52

+0

我喜歡這兩種方式,但我試圖讓使用該操作符更舒適。我對使用它並不是很熟悉,我只是認爲在這種情況下最好使用它。你知道這樣做的一種方式嗎? – theGrayFox 2011-05-22 03:50:45