do-while循环↓ while循环↓
数数位
运用成while
#includeint main() { int x; int n=0; scanf("%d",&x); n++; x /= 10; while ( x> 0){ n++; x /= 10; } printf("%dn",n); return 0; }
游戏名称1:
设计思路:
rand()//随机数
#include#include #include int main() { srand(time(0)); int a = rand(); printf("%dn",a); return 0; }
%100:x%n的结果是【0,n-1】的一个整数
#include#include #include int main() { srand(time(0)); int number = rand()%100+1; int count = 0; int a = 0; printf("我已经想好了一个1到100之间的数。"); do{ printf("请猜这个1到100之间数:"); scanf("%d",&a); count ++; if( a < number ){ printf("你猜的数大了。"); }else if ( a < number ){ printf("你猜的数小了"); } }while ( a != number); printf("太好了,你用了%d次就猜到了答案。n",count); return 0; }
循环运用:算平均数
#includeint main() { int number; int sum = 0; int count = 0; do{ scanf("%d",&number); if( number != -1){ sum += number; count ++; } }while( number != -1;) printf("%fn",1.0 * sum/count); return 0; }
二次判断改一次判断:
#includeint main() { int number; int sum = 0; int count = 0; scanf("%d",&number); while( number != -1){ sum += number; count ++; scanf("%d",&number); } printf("%fn",1.0 * sum/count); return 0; }
循环应用:循环计算
// log2x #includeint main() { int x; int ret = 0; // scanf("%d",&x); x = 128; int t = x; while( x > 1){ x /= 2; ret ++; } printf("log2 of %d is %d.", t, ret); return 0; }
计数循环倒计时:
#includeint main() { int n = 100; while( n >= 0){ printf("%dn",n); n--; } printf("发射!n"); return 0; }
整数求逆:
#includeint main() { int x; scanf("%d",&x); int dight; int ret = 0; while( x > 0){ dight = x%10; //printf("%dn",dight); ret = ret*10 + dight; printf("x=%d,dight=%d,ret=%dn",x,dight,ret); x /= 10; } printf("%d",ret); return 0; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)