16. seeking speed primary series of problems

Seeking speed

Problem Description

A car traveling at a constant speed, the driver can see at 10 o'clock in the morning reading on the odometer is a symmetrical number (ie the number read from left to right and right to left reading are exactly the same), to 95859, two after hours appear on the odometer of a new symmetrical number, the number is five digits, the speed of the car to ask is how much? The new number is the number of symmetry?

problem analysis

The meaning of the questions, the driver can see at 10 a.m. on the odometer readings symmetric number 95859 is a new symmetric After two hours the number appearing on the odometer must be greater than 95859, therefore, the number of symmetry and ask is i, and let its initial value of 95 860, i.e. from 95860 start detecting the value of i is incremented sequentially for each value of i which are decomposed, and the number at the symmetrical position are compared, if each in symmetrical positions the number is equal, the number can be determined that the current i symmetrical five-digit number as stored on the odometer is the emerging

algorithm design

1> i need to start from the 95,860 probe, an obvious need to use cyclic structure, the number of loop decomposition is completed in five and saved, and then detecting whether the number is a function of symmetry

2> problem requires a five-digit number and saved decomposed, thus can be used to hold an array of five digitally generated decomposition

#include <stdio.h>

int main(void)
{
    int t, a[5];    /* !<数组a存放分解后的五个数字*/
    long int k, i;
    /* !<以95860为初值,循环试探*/
    for (i = 95860; ; i++) {
        /* !<从高到低分解当前i中保存的五位数,并顺次存放在数组元素a[0]-a[4]中*/
        for (t = 0, k = 100000; k >= 10; t++) {
            a[t] = (i % k) / (k / 10);  /* !<保存分解后的数字*/
            k /= 10;
        }
        if ((a[0] == a[4]) && (a[1] == a[3])) {
            printf("里程表上出现的新的对称数为 %d%d%d%d%d\n",
                    a[0],a[1], a[2], a[3],a[4]);
            printf("该车的速度为: %.2f\n", (i-95859) / 2.0);
            break;  /* !< 跳出循环*/
        }
    }
}

/* !<output */
    里程表上出现的新的对称数为 95959
    该车的速度为: 50.00

    Process returned 0 (0x0)   execution time : 0.004 s
    Press any key to continue.



Guess you like

Origin www.cnblogs.com/xuzhaoping/p/11484623.html