001.將輸入之秒數轉換成「時:分:秒」
第一題的做法最多人提供,我貼的也很辛苦......感謝這些作者。
同一個題目這麼多的做法,也可以提供大家對於一個題目不同的思考方法!
我們也從這個題目可以看出,一樣米養百樣人,大家的思維方式都不同。
果真軟體開發是一個藝術,不是一個工程。
作者:jefferylee
作者:turboted
作者:lhh
作者:H.A.R.D
作者:gump996
作者:rclrn
作者:andyeric
作者:zoe
作者:blueluna
作者:kulisa
作者:gsrr
作者:Franklin
在此感謝當年在數位高手(NBP)分享的朋友,本人將資料彙整於此並盡量將當初發表者名稱註記。
第一題的做法最多人提供,我貼的也很辛苦......感謝這些作者。
同一個題目這麼多的做法,也可以提供大家對於一個題目不同的思考方法!
我們也從這個題目可以看出,一樣米養百樣人,大家的思維方式都不同。
果真軟體開發是一個藝術,不是一個工程。
#include <iostream>
using namespace std;
void main(void)
{
int timer;
cout << "Please input second to transfer:";
cin >> timer;
cout.fill('0');
cout.width(2);
cout << timer/3600 << ":";
timer-=(timer/3600)*3600;
cout << timer/60 << ":";
timer-=(timer/60)*60;
cout << timer;
}
作者:jefferylee
#include<iostream>
using namespace std;
void main(void)
{
int second=0;
int unit[3]={3600,60,1};
int a[3]={0};
cout<<"輸入欲轉換之秒數: ";
cin>>second;
for(int i=0;i<3;i++)
{
a[i]=second/unit[i];
second%=unit[i];
}
cout<<"輸入的是"<<a[0]<<"時"<<a[1]<<"分"<<a[2]<<"秒"<<endl;
}
#include<stdio.h>
void main()
{
int time=0;
printf("Input seconds:");
scanf("%d",&time);
printf("%02d:%02d:%02d\n",time/3600%60,time/60%60,time%60);
}
作者:turboted
int main()
{
int sec,min,hour,total;
cout << "Input Sec To Change: ";
cin >> total;
hour=total/(60*60),total=total-(hour*60*60);
min=total/60, total=total-(min*60);
sec=total;
cout << "hour:"<<hour<<" min:"<< min<<" sec:"<<sec<<endl;
system("PAUSE");
return 0;
}
作者:lhh
#include <iostream.h>
void main(void)
{
int x,h,m,s;
cout<<"Enter a second :";
cin>>x;
h=x/3600;
m=x%3600/60;
s=x%3600%60;
cout<<h<<" hours "<<m<<" minutes "<<s<<" seconds/n ";
}
作者:H.A.R.D
#include<stdio.h>
#include<conio.h>
void main()
{
int time,hour,min,sec,i,j;
clrscr();
printf("enter the time:");
scanf("%d",&time);
for(i=1;;i++)
{
if(time<(3600*i))
{
hour=i-1;
break;
}
else if(time==(3600*i))
{
hour=i;
min=0;
sec=0;
break;
}
}
for(j=1;;j++)
{
if((time-3600*hour)<(60*j))
{
min=j-1;
break;
}
else if((time-3600*hour)==(60*j))
{
min=j;
sec=0;
break;
}
}
if (sec!=0)
sec=time-3600*hour-60*min;
printf("the time is %d:%d:%d",hour,min,sec);
}
作者:gump996
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
//#include <alloc.h> This line has to be remarked in Dev-C++ 4.
typedef struct time {
long lHour;
long lMin;
long lSec;
}TM_t;
void TransTime(TM_t *);
int main()
{
TM_t *ptrTime;
if((ptrTime = (TM_t *)calloc(1, sizeof(TM_t))) == NULL)
printf("Memory Allocation Error!\n");
printf("Seconds?\n");
scanf("%ld", &ptrTime->lSec);
TransTime(ptrTime);
system("PAUSE");
return 0;
}
void TransTime(TM_t *ptrTM) {
ptrTM->lHour = ptrTM->lSec / 3600;
ptrTM->lMin = (ptrTM->lSec % 3600) / 60;
ptrTM->lSec = ptrTM->lSec % 60;
printf("Time: %ld:%ld:%ld\n", ptrTM->lHour, ptrTM->lMin, ptrTM->lSec);
}
作者:rclrn
//1.將輸入之秒數轉換成「時:分:秒」。
//compiled by Borland C++ Builder 6
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int sec = 0;
cout << "Input seconds: ";
cin >> sec;
int hr = sec / 3600;
int min = (sec % 3600) / 60;
sec = sec % 60;
cout.fill('0');
cout << setw(2) << hr << ":"
<< setw(2) << min << ":"
<< setw(2) << sec << endl;
system("pause");
return 0;
}
作者:andyeric
#include <iostream.h>
int main()
{
int sec;
int hour,min;
cout<<"請輸入秒數:";
cin>>sec;
hour = sec/3600;
sec = sec-3600*hour;
min = sec/60;
sec = sec-60*min;
cout<<hour<<":"<<min<<":"<<sec<<endl;
return 0;
}
作者:zoe
#include <stdio.h>
main()
{
int h = 0, m = 0, s = 0;
scanf("%d", &s);
if ( s >= 60 )
{
m = s / 60;
s = s % 60;
}
if ( m >= 60 )
{
h = m / 60;
m = m % 60;
}
printf("%.2d:%.2d:%.2d\n", h, m, s);
}
作者:blueluna
#include <iostream.h>
int main()
{
unsigned int input,second,minute,hour;
cout<<"Input the seconds:\n";
cin>>input;
hour=input/3600;
input%=3600;
minute=input/60;
input%=60;
second=input;
cout<<"The time is:\n";
cout<<hour<<":"<<minute<<":"<<second<<"\n";
return 0;
}
作者:kulisa
//輸入秒數將之轉化為時分秒
#include "iostream.h"
void main()
{
int t;
cout<<"input the second:"; cin>>t;
cout<<t/3600<<"h"<<(t%3600)/60<<"m"<<(t%3600)%60<<"s\n";
}
作者:gsrr
#include <iostream>
using namespace std;
int main()
{
int seconds;
cout<<"please input seconds: ";
cin>>seconds;
cout<<"the time transfer is: ";
int hr,min,sec;
int mid1;
hr=seconds/3600;
mid1=seconds%3600;
min=mid1/60;
sec=mid1%60;
cout<<hr<<" hr "<<min<<" min "<< sec <<" sec "<<endl;
system("pause");
return 0;
}
作者:Franklin
#include <stdio.h>
#define SECONDS_IN_A_DAY (24*60*60) // May use 86400 directly
void print_time(int num, int level)
{
if (level){
print_time(num/60,--level);
printf(":");
}
printf("%02d",num%60);
return;
}
int main()
{
int sec;
printf("Input seconds please -> ");
scanf("%d",&sec);
if (sec<0) // Handle negative seconds
sec+=((-sec/SECONDS_IN_A_DAY)+1)*SECONDS_IN_A_DAY;
sec%=SECONDS_IN_A_DAY; // Handle seconds > 1 day
printf("The time is ");
print_time(sec,2);
printf ("\n");
return 0;
}
在此感謝當年在數位高手(NBP)分享的朋友,本人將資料彙整於此並盡量將當初發表者名稱註記。
文章標籤
全站熱搜

原本在搜尋引擎找出一堆 Blog 文章,不知哪幾篇值得花時間一看, 後來用 PTT搜尋引擎,輾轉看到您這的好文而有緣來到這, 謝謝您用心分享有價值的內容, 也回饋給您這實用的主題排名網站資訊,可查看與您 Blog 內容相關的排名好文,應該對寫 Blog 也有所幫助,期待您持續產出好文章 ^^ https://searchptt.cc/