c语言小程序 关于for循环,if else和break,帮忙改下错谢谢

c语言小程序 关于for循环,if else和break,帮忙改下错谢谢,第1张

#include<stdio.h>

int main()

{

char a

int i=6

while(--i)

{

printf("You have %d chances.Please input a character:",i)

a=getchar()

getchar()

if(a=='A')

{

printf("Congratulations!\n")

break

}

else{

printf("Please try again.\n")

}

}

if(i==0)

printf("You've tried too many times!\n")

getchar()

return 0

}

这个题目看似一样,其实还是有不同的,我想应该从时间方面比较.程序2 浪费的时间比程序1 要多,具体这样分析: 在这里是忽略 dosomething 为分号" "

程序1:

if(k>8) 执行 100次. else 拆行 100次. 也就是最多k<8是 100+1 次

程序2

for(100次...){if(..)else(..)} 也就是最多k<8是 100*2 次

显然,程序2 用的时间比 程序1 多.

当然,具体按汇编是不可以这样算步数的,不过大致是这样。

请自己也可以上机试下.希望采纳.

Scanner sc=new Scanner(System.in)

System.out.println("输入一个数字:")

int a=sc.nextInt()

System.out.println("正序:")

for(int i=1i*i<=ai++){

System.out.print(i*i+",")

}

System.out.println("\n倒序:")

for(int i=ai>0i--){

if(i*i<=a){

System.out.print(i*i+",")

}

}

自己写了一个,倒序代码没有最简化,但功能可以实现。。。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12097518.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-21
下一篇2023-05-21

发表评论

登录后才能评论

评论列表(0条)

    保存