编写一个程序,输出一个24小时制数字时钟显示的所有回文时间(如13:31)
Write a program to output all palindromic times displayed by a 24 hour digital clock (e.g. 13:31)
我正在尝试编写一个程序,使用 C 编程输出所有回文时间如下所示:
00:00
01:10
02:20
03:30
04:40
05:50
10:01
11:11
12:21
13:31
14:41
15:51
20:02
21:12
22:22
23:32
我知道将小时和分钟视为两个单独的变量而不是实际时间。此外,还会有一些计数器来增加小时和分钟,但不确定确切的位置是否有任何想法值得赞赏?
也许这就是你想要的,
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
int main()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "Current local time and date: %s\n", asctime (timeinfo) );
char hour[20], mints[20], rec[10];
snprintf(hour, sizeof(hour), "%d", timeinfo->tm_hour);
snprintf(mints, sizeof(mints), "%d", timeinfo->tm_min);
int length = strlen(hour);
int j = 0;
for(int i = length; i > 0; i--)
{
rec[j++] = hour[i - 1];
}
rec[j] = '[=10=]';
if(strcmp(rec, mints) == 0)
printf ( "%d %d", timeinfo->tm_hour, timeinfo->tm_min );
return 0;
}
这就是你想要的完美答案
#include <stdio.h>
int pallindrome(int loop)
{
if (loop >= 10)
{
int temp, temp1 = 0, sum = 0;
for(temp = 0; temp < 2;temp++)
{
temp1 = loop % 10;
loop = loop / 10;
sum = sum * 10 + temp1;
}
return(sum);
}
else
{
return(loop * 10);
}
}
int main(void) {
// your code goes here
int a=0;
int loop=0;
for (loop=0; loop<=59; loop++)
{
a = pallindrome(loop);
if (a < 60)
printf("%.2d:%.2d\n", loop,a);
}
return 0;
}
在此处查看输出:http://ideone.com/54ouHv
#include <stdio.h>
int main()
{
int i;
char h[3] = {'0', '0', 0};
for(i = 0 ; i < 24 ; i++)
{
if((i % 10) < 6)
{
snprintf(h, 3, "%02d", i);
printf("%s:%c%c\n", h, h[1], h[0]);
}
}
return 0;
}
这段代码的输出:http://ideone.com/N0L4ft
我正在尝试编写一个程序,使用 C 编程输出所有回文时间如下所示:
00:00
01:10
02:20
03:30
04:40
05:50
10:01
11:11
12:21
13:31
14:41
15:51
20:02
21:12
22:22
23:32
我知道将小时和分钟视为两个单独的变量而不是实际时间。此外,还会有一些计数器来增加小时和分钟,但不确定确切的位置是否有任何想法值得赞赏?
也许这就是你想要的,
#include <stdio.h> #include <time.h> #include <stdlib.h> #include <string.h> int main() { time_t rawtime; struct tm * timeinfo; time ( &rawtime ); timeinfo = localtime ( &rawtime ); printf ( "Current local time and date: %s\n", asctime (timeinfo) ); char hour[20], mints[20], rec[10]; snprintf(hour, sizeof(hour), "%d", timeinfo->tm_hour); snprintf(mints, sizeof(mints), "%d", timeinfo->tm_min); int length = strlen(hour); int j = 0; for(int i = length; i > 0; i--) { rec[j++] = hour[i - 1]; } rec[j] = '[=10=]'; if(strcmp(rec, mints) == 0) printf ( "%d %d", timeinfo->tm_hour, timeinfo->tm_min ); return 0; }
这就是你想要的完美答案
#include <stdio.h>
int pallindrome(int loop)
{
if (loop >= 10)
{
int temp, temp1 = 0, sum = 0;
for(temp = 0; temp < 2;temp++)
{
temp1 = loop % 10;
loop = loop / 10;
sum = sum * 10 + temp1;
}
return(sum);
}
else
{
return(loop * 10);
}
}
int main(void) {
// your code goes here
int a=0;
int loop=0;
for (loop=0; loop<=59; loop++)
{
a = pallindrome(loop);
if (a < 60)
printf("%.2d:%.2d\n", loop,a);
}
return 0;
}
在此处查看输出:http://ideone.com/54ouHv
#include <stdio.h>
int main()
{
int i;
char h[3] = {'0', '0', 0};
for(i = 0 ; i < 24 ; i++)
{
if((i % 10) < 6)
{
snprintf(h, 3, "%02d", i);
printf("%s:%c%c\n", h, h[1], h[0]);
}
}
return 0;
}
这段代码的输出:http://ideone.com/N0L4ft