温度转换挑战

问题描述:

我是做了编码的挑战网站,前提是:温度转换挑战

在这种挑战,写一个程序,它在三个参数,启动温度( 摄氏度),结束温度(摄氏度)和步长。打印出一张表格,该表格从开始温度到结束温度,按步长大小逐步进行;如果步长不完全匹配,则实际上不需要打印最终结束温度 。 您应该执行输入验证:不要接受小于较低的 限制(您的代码应指定为常量)或高于上限(您的代码应该指定的 )的起始温度。你不应该允许一个大于 温差的步长。 (这个练习是基于C编程 语言的一个问题)。

我得到了与解决方案相同的结果,但我很好奇为什么他们的解决方案更有效率(我假设是这样)。任何人都可以向我解释?他们的解决方案首先是我的。

#include <stdio.h> 

#define LOWER_LIMIT 0 
#define HIGHER_LIMIT 50000 

int main(void) { 
    double fahr, cel; 
    int limit_low = -1; 
    int limit_high = -1; 
    int step = -1; 
    int max_step_size = 0; 

    /* Read in lower, higher limit and step */ 
    while(limit_low < (int) LOWER_LIMIT) { 
     printf("Please give in a lower limit, limit >= %d: ", (int) LOWER_LIMIT); 
     scanf("%d", &limit_low); 
    } 
while((limit_high <= limit_low) || (limit_high > (int) HIGHER_LIMIT)) { 
    printf("Please give in a higher limit, %d < limit <= %d: ", limit_low, (int) HIGHER_LIMIT); 
    scanf("%d", &limit_high); 
} 
max_step_size = limit_high - limit_low; 
while((step <= 0) || (step > max_step_size)) { 
    printf("Please give in a step, 0 <step>= %d: ", max_step_size); 
    scanf("%d", &step); 
} 

/* Initialise Celsius-Variable */ 
cel = limit_low; 

/* Print the Table */ 
printf("\nCelsius\t\tFahrenheit"); 
printf("\n-------\t\t----------\n"); 
while(cel <= limit_high) { 
    fahr = (9.0 * cel)/5.0 + 32.0; 
    printf("%f\t%f\n", cel, fahr); 
     cel += step; 
    } 
    printf("\n"); 

    return 0; 
} 

我的解决办法:

#include <stdio.h> 
#include <stdlib.h> 

#define LOW 0 
#define HIGH 50000 


int main(void) 
{ 
int lower, higher, step, max_step; 
float cel, fahren; 

printf("\nPlease enter a lower limit, limit >= 0: "); 
scanf("%d", &lower); 

if (lower < LOW) 
{ 
    printf("\nERROR: Lower limit must be >= 0."); 
    exit(1); 
} 


printf("\nPlease enter a upper limit, limit <= 50000: "); 
scanf("%d", &higher); 

if (higher > HIGH) 
{ 
    printf("\nERROR: Upper limit must be <= 50,000."); 
    exit(1); 
} 

printf("\nPlease enter an increment amount, 0 < step <= 10: "); 
scanf("%d", &step); 

max_step = higher - lower; 

if (step > max_step) 
{ 
    printf("\nERROR: Step size cannot exceed difference between higher and lower limit."); 
    exit(1); 
} 

printf("Celsuis \tFahrenheit\n"); 
printf("------- \t-----------\n\n"); 

cel = (float)lower; 

while (cel < higher) 
{ 
    fahren = cel * 9/5 + 32; 
    printf("%f \t%f\n", cel, fahren); 
    cel = cel + step; 
} 

return 0; 

}

+0

除了事实“+ =”比我有较少的机器指令。 –

+0

是什么让你说第一个是“更高效”? –

+0

为什么您认为该网站的解决方案比您的解决方案更有效率,以及采取什么措施? –

嗯,这是一种制造要求,我们需要一些指标以前更加高效......,什么是我们在这里谈论?运行?二进制大小? 只是一个简单的例子......我们可以编译两种解决方案,并与“时间”命令和最坏的情况下(0-50000为1的步骤),看看我们使用什么类型的次运行它们:


“他们” 的解决方案尺寸:

text  data  bss  dec  hex filename 
1937  276  8 2221  8ad a.out 

“他们的” 解决方案运行时间:

user 0m 0.024s 
sys 0m 0.601s 

您的搜索解决方案ñ大小:

text  data  bss  dec  hex filename 
2054  276  8 2338  922 a.out 

您的解决方案的运行时间:

user 0m 0.025s 
sys 0m 1.047s 

所以您的解决方案需要更长的时间,并具有较大的图像大小。我们现在可以说“他们”有更高效的方案吗?并非如此,time在这个尺度上并不完全准确(并且在系统中发生其他事情),所以我们需要一些运行。平均超过四*运行:

// you 
user 0.0178 
system 0.9015 
// "them" 
user 0.016 
system 0.914 

所以,不,他们是不是真的更“高效”,那么你的。

有一些琐碎的事情可以做,以提高“效率”在这里,但由于解决方案是如此相似,和代码是如此微不足道(单步遍历)我真的不知道,如果它的事项全部那么多。

就“高效”代码大小而言,您会注意到您的.text大小比其他解决方案大。你的信息更加详细和可读,所以你的尺寸受到了打击。更高效吗?也许如果规模很重要,但我个人认为,除非我们正在谈论嵌入式解决方案,否则可读性更重要。

* - 你需要更多的奔跑和更灵敏的计时机制,但是这仅仅是一个简单的例子

+0

感谢您的所有信息! :) –