- User inputs positive int value (
number
); - 用户输入正int值(数);
- User prints
number
int values; - 用户打印数字int值;
- Need to find max value and print it.
- 需要找到最大值并打印出来。
My code is
我的代码是
#include <stdio.h>
int main() {
int number;
int max;
int temp;
scanf("%d", &number);
scanf("%d", &max);
for ( int i = 1; i < number; i++ ) {
scanf("%d", &temp);
if ( temp > max ) {
max = temp;
}
}
printf("%d\n", max);
return 0;
}
This works but online test tool says i need to optimize code, because it uses too many operations. Arrays are forbidden. Can only use stdio.
这是可行的,但是在线测试工具说我需要优化代码,因为它使用了太多的操作。数组是禁止的。只能使用它。
6 个解决方案
#1
4
By using Duff's device you could save some comparisons in the for-loop. It'd be a bad practice, but maybe that's what you are expected to do.
通过使用Duff的设备,您可以在for循环中保存一些比较。这是一种糟糕的做法,但也许这就是你应该做的。
#include <stdio.h>
int main (void) {
unsigned max = 0;
unsigned length;
scanf("%u", &length);
unsigned temp = 0;
unsigned iterations = (length+8-1) / 8;
switch (length % 8) {
case 0: do { scanf("%u", &temp); if (temp > max) max = temp;
case 7: scanf("%u", &temp); if (temp > max) max = temp;
case 6: scanf("%u", &temp); if (temp > max) max = temp;
case 5: scanf("%u", &temp); if (temp > max) max = temp;
case 4: scanf("%u", &temp); if (temp > max) max = temp;
case 3: scanf("%u", &temp); if (temp > max) max = temp;
case 2: scanf("%u", &temp); if (temp > max) max = temp;
case 1: scanf("%u", &temp); if (temp > max) max = temp;
} while (--iterations > 0);
}
printf("%u\n", max);
return 0;
}
I used unsigned
ints, because you said you only have positive numbers. The code assumes that the sequence has at least one element.
我用的是无符号整数,因为你说只有正数。代码假设序列至少有一个元素。
Update 1:
Example using manual loop unrolling. That's an even worse practice than Duff's device. Maybe the testing tool you got will like it, but you should never use this code to impress a potential employer!
示例使用手动循环展开。这比达夫的设备更糟糕。也许你得到的测试工具会喜欢它,但是你永远不应该使用这个代码来给你的潜在雇主留下深刻的印象!
#include <stdio.h>
int main (void) {
signed max = -0x80000000;
unsigned length;
scanf("%u", &length);
signed temp;
for (; length >= 8; length -= 8) {
scanf("%d", &temp); if (temp > max) max = temp;
scanf("%d", &temp); if (temp > max) max = temp;
scanf("%d", &temp); if (temp > max) max = temp;
scanf("%d", &temp); if (temp > max) max = temp;
scanf("%d", &temp); if (temp > max) max = temp;
scanf("%d", &temp); if (temp > max) max = temp;
scanf("%d", &temp); if (temp > max) max = temp;
scanf("%d", &temp); if (temp > max) max = temp;
}
if (length > 4) {
scanf("%d", &temp); if (temp > max) max = temp;
scanf("%d", &temp); if (temp > max) max = temp;
scanf("%d", &temp); if (temp > max) max = temp;
scanf("%d", &temp); if (temp > max) max = temp;
length -= 4;
}
for (; length > 0; --length) {
scanf("%d", &temp); if (temp > max) max = temp;
}
printf("%d\n", max);
return 0;
}
Update 2:
You stated that your evaluation tool likes it if scanf is less often called, so:
如果scanf很少被调用,则表示您的评估工具喜欢使用scanf,因此:
#include <stdio.h>
int main (void) {
signed max = -0x80000000;
unsigned length;
scanf("%u", &length);
signed t1, t2, t3, t4, t5, t6, t7, t8;
for (; length >= 8; length -= 8) {
scanf("%d%d%d%d%d%d%d%d", &t1, &t2, &t3, &t4, &t5, &t6, &t7, &t8);
if (t1 > max) max = t1;
if (t2 > max) max = t2;
if (t3 > max) max = t3;
if (t4 > max) max = t4;
if (t5 > max) max = t5;
if (t6 > max) max = t6;
if (t7 > max) max = t7;
if (t8 > max) max = t8;
}
if (length > 4) {
scanf("%d%d%d%d", &t1, &t2, &t3, &t4);
if (t1 > max) max = t1;
if (t2 > max) max = t2;
if (t3 > max) max = t3;
if (t4 > max) max = t4;
length -= 4;
}
for (; length > 0; --length) {
scanf("%d", &t1); if (t1 > max) max = t1;
}
printf("%d\n", max);
return 0;
}
#2
2
I'd like to know what test tool says you have too many operations that isn't some code golf competition. Also, what the number of acceptable operations is and how they define 'operation'.
我想知道什么测试工具说你有太多的操作不是一些代码高尔夫比赛。此外,可接受的操作的数量以及它们如何定义“操作”。
#include <stdio.h>
int main() {
int numbersLeft,
number,
max = 0;
scanf("%d", &numbersLeft);
while ( numbersLeft-- ) {
scanf("%d", &number);
max = number > max? number: max;
}
printf("%d\n", max);
return 0;
}
#3
2
Your 'test tool' is probably broken, or expects you to do meaningless micro-optimizations while making code hard to read, which the compiler will do anyway.
您的“测试工具”可能已经损坏,或者期望您在使代码难以阅读的同时进行无意义的微优化,编译器无论如何都会这样做。
- You need to ask the user for the
number
of numbers => 1scanf
. You did this. - 您需要向用户询问编号=> 1 scanf的数量。你这样做。
- Next you need to ask the user for
number
numbers =>scanf
,number
times. You did this. - 接下来,您需要向用户询问number => scanf, number times。你这样做。
- Next you need to find the largest => loop
number
times, and compare. You already achieved this in the same loop as the previous one. And you did it well because you did the minimum possiblenumber - 1
comparisons. - 接下来,您需要找到最大的=>循环次数,并进行比较。您已经在前面的循环中实现了这一点。你做得很好因为你做了最小可能的数字1比较。
- Next you need to print the result => 1
printf
. You did this too. - 接下来需要打印结果= >1printf。你也这样做。
This is the fastest you can possibly get[1]. There is no scope for 'optimization'.
这是最快的[1]。没有“优化”的余地。
[1] If you had 2 cores, and were writing a multi-threaded program, you could do step 2 and 3 faster by pipelining them (see unkulunkulu's comments for why this is the fastest you can get).
[1]如果您有两个内核,并且正在编写一个多线程程序,您可以通过流水线方式更快地完成第2步和第3步(请参阅unkulunkulu关于为什么这是您能获得的最快的评论)。
#4
2
Should have used 1 scanf() instead of two:
应该使用1 scanf()代替2:
#include <stdio.h>
int main() {
int number;
int max;
int temp;
scanf("%d %d", &number, &max);
for ( int i = 1; i < number; i++ ) {
scanf("%d", &temp);
if ( temp > max ) {
max = temp;
}
}
printf("%d\n", max);
return 0;
}
Sorry, and thanks to everyone! Hugs and kisses!
对不起,谢谢大家!拥抱和亲吻!
#5
1
You don't really need the i
variable: you can use number
itself.
你不需要i变量:你可以用number本身。
Also you may want to abuse the for
statement :)
你可能还想滥用for语句:
#include <stdio.h>
int main(void) {
int number;
int max;
int temp;
for (scanf("%d", &number), scanf("%d", &max)
; --number && scanf("%d", &temp)
; )
{
if (temp > max) max = temp;
}
printf("%d\n", max);
return 0;
}
#6
-1
#include <stdio.h>
int main() {
int number;
int max;
int temp,i;
scanf("%d", &number);
for ( i = 0; i < number; i++ ) {
scanf("%d", &temp);
if(i==0)
max=temp;
if ( temp > max ) {
max = temp;
}
}
printf("max=%d\n", max);
return 0;
}
}
#1
4
By using Duff's device you could save some comparisons in the for-loop. It'd be a bad practice, but maybe that's what you are expected to do.
通过使用Duff的设备,您可以在for循环中保存一些比较。这是一种糟糕的做法,但也许这就是你应该做的。
#include <stdio.h>
int main (void) {
unsigned max = 0;
unsigned length;
scanf("%u", &length);
unsigned temp = 0;
unsigned iterations = (length+8-1) / 8;
switch (length % 8) {
case 0: do { scanf("%u", &temp); if (temp > max) max = temp;
case 7: scanf("%u", &temp); if (temp > max) max = temp;
case 6: scanf("%u", &temp); if (temp > max) max = temp;
case 5: scanf("%u", &temp); if (temp > max) max = temp;
case 4: scanf("%u", &temp); if (temp > max) max = temp;
case 3: scanf("%u", &temp); if (temp > max) max = temp;
case 2: scanf("%u", &temp); if (temp > max) max = temp;
case 1: scanf("%u", &temp); if (temp > max) max = temp;
} while (--iterations > 0);
}
printf("%u\n", max);
return 0;
}
I used unsigned
ints, because you said you only have positive numbers. The code assumes that the sequence has at least one element.
我用的是无符号整数,因为你说只有正数。代码假设序列至少有一个元素。
Update 1:
Example using manual loop unrolling. That's an even worse practice than Duff's device. Maybe the testing tool you got will like it, but you should never use this code to impress a potential employer!
示例使用手动循环展开。这比达夫的设备更糟糕。也许你得到的测试工具会喜欢它,但是你永远不应该使用这个代码来给你的潜在雇主留下深刻的印象!
#include <stdio.h>
int main (void) {
signed max = -0x80000000;
unsigned length;
scanf("%u", &length);
signed temp;
for (; length >= 8; length -= 8) {
scanf("%d", &temp); if (temp > max) max = temp;
scanf("%d", &temp); if (temp > max) max = temp;
scanf("%d", &temp); if (temp > max) max = temp;
scanf("%d", &temp); if (temp > max) max = temp;
scanf("%d", &temp); if (temp > max) max = temp;
scanf("%d", &temp); if (temp > max) max = temp;
scanf("%d", &temp); if (temp > max) max = temp;
scanf("%d", &temp); if (temp > max) max = temp;
}
if (length > 4) {
scanf("%d", &temp); if (temp > max) max = temp;
scanf("%d", &temp); if (temp > max) max = temp;
scanf("%d", &temp); if (temp > max) max = temp;
scanf("%d", &temp); if (temp > max) max = temp;
length -= 4;
}
for (; length > 0; --length) {
scanf("%d", &temp); if (temp > max) max = temp;
}
printf("%d\n", max);
return 0;
}
Update 2:
You stated that your evaluation tool likes it if scanf is less often called, so:
如果scanf很少被调用,则表示您的评估工具喜欢使用scanf,因此:
#include <stdio.h>
int main (void) {
signed max = -0x80000000;
unsigned length;
scanf("%u", &length);
signed t1, t2, t3, t4, t5, t6, t7, t8;
for (; length >= 8; length -= 8) {
scanf("%d%d%d%d%d%d%d%d", &t1, &t2, &t3, &t4, &t5, &t6, &t7, &t8);
if (t1 > max) max = t1;
if (t2 > max) max = t2;
if (t3 > max) max = t3;
if (t4 > max) max = t4;
if (t5 > max) max = t5;
if (t6 > max) max = t6;
if (t7 > max) max = t7;
if (t8 > max) max = t8;
}
if (length > 4) {
scanf("%d%d%d%d", &t1, &t2, &t3, &t4);
if (t1 > max) max = t1;
if (t2 > max) max = t2;
if (t3 > max) max = t3;
if (t4 > max) max = t4;
length -= 4;
}
for (; length > 0; --length) {
scanf("%d", &t1); if (t1 > max) max = t1;
}
printf("%d\n", max);
return 0;
}
#2
2
I'd like to know what test tool says you have too many operations that isn't some code golf competition. Also, what the number of acceptable operations is and how they define 'operation'.
我想知道什么测试工具说你有太多的操作不是一些代码高尔夫比赛。此外,可接受的操作的数量以及它们如何定义“操作”。
#include <stdio.h>
int main() {
int numbersLeft,
number,
max = 0;
scanf("%d", &numbersLeft);
while ( numbersLeft-- ) {
scanf("%d", &number);
max = number > max? number: max;
}
printf("%d\n", max);
return 0;
}
#3
2
Your 'test tool' is probably broken, or expects you to do meaningless micro-optimizations while making code hard to read, which the compiler will do anyway.
您的“测试工具”可能已经损坏,或者期望您在使代码难以阅读的同时进行无意义的微优化,编译器无论如何都会这样做。
- You need to ask the user for the
number
of numbers => 1scanf
. You did this. - 您需要向用户询问编号=> 1 scanf的数量。你这样做。
- Next you need to ask the user for
number
numbers =>scanf
,number
times. You did this. - 接下来,您需要向用户询问number => scanf, number times。你这样做。
- Next you need to find the largest => loop
number
times, and compare. You already achieved this in the same loop as the previous one. And you did it well because you did the minimum possiblenumber - 1
comparisons. - 接下来,您需要找到最大的=>循环次数,并进行比较。您已经在前面的循环中实现了这一点。你做得很好因为你做了最小可能的数字1比较。
- Next you need to print the result => 1
printf
. You did this too. - 接下来需要打印结果= >1printf。你也这样做。
This is the fastest you can possibly get[1]. There is no scope for 'optimization'.
这是最快的[1]。没有“优化”的余地。
[1] If you had 2 cores, and were writing a multi-threaded program, you could do step 2 and 3 faster by pipelining them (see unkulunkulu's comments for why this is the fastest you can get).
[1]如果您有两个内核,并且正在编写一个多线程程序,您可以通过流水线方式更快地完成第2步和第3步(请参阅unkulunkulu关于为什么这是您能获得的最快的评论)。
#4
2
Should have used 1 scanf() instead of two:
应该使用1 scanf()代替2:
#include <stdio.h>
int main() {
int number;
int max;
int temp;
scanf("%d %d", &number, &max);
for ( int i = 1; i < number; i++ ) {
scanf("%d", &temp);
if ( temp > max ) {
max = temp;
}
}
printf("%d\n", max);
return 0;
}
Sorry, and thanks to everyone! Hugs and kisses!
对不起,谢谢大家!拥抱和亲吻!
#5
1
You don't really need the i
variable: you can use number
itself.
你不需要i变量:你可以用number本身。
Also you may want to abuse the for
statement :)
你可能还想滥用for语句:
#include <stdio.h>
int main(void) {
int number;
int max;
int temp;
for (scanf("%d", &number), scanf("%d", &max)
; --number && scanf("%d", &temp)
; )
{
if (temp > max) max = temp;
}
printf("%d\n", max);
return 0;
}
#6
-1
#include <stdio.h>
int main() {
int number;
int max;
int temp,i;
scanf("%d", &number);
for ( i = 0; i < number; i++ ) {
scanf("%d", &temp);
if(i==0)
max=temp;
if ( temp > max ) {
max = temp;
}
}
printf("max=%d\n", max);
return 0;
}
}