标签:do int number 笔记 while ret printf include
do-while循环↓ while循环↓
数数位
运用成while
#include <stdio.h>
int main()
{
int x;
int n=0;
scanf("%d",&x);
n++;
x /= 10;
while ( x> 0){
n++;
x /= 10;
}
printf("%d\n",n);
return 0;
}
游戏名称1:
设计思路:
rand()//随机数
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(0));
int a = rand();
printf("%d\n",a);
return 0;
}
%100:x%n的结果是【0,n-1】的一个整数
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
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;
}
循环运用:算平均数
#include <stdio.h>
int main()
{
int number;
int sum = 0;
int count = 0;
do{
scanf("%d",&number);
if( number != -1){
sum += number;
count ++;
}
}while( number != -1;)
printf("%f\n",1.0 * sum/count);
return 0;
}
二次判断改一次判断:
#include <stdio.h>
int main()
{
int number;
int sum = 0;
int count = 0;
scanf("%d",&number);
while( number != -1){
sum += number;
count ++;
scanf("%d",&number);
}
printf("%f\n",1.0 * sum/count);
return 0;
}
循环应用:循环计算
// log2x
#include <stdio.h>
int 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;
}
计数循环倒计时:
#include <stdio.h>
int main()
{
int n = 100;
while( n >= 0){
printf("%d\n",n);
n--;
}
printf("发射!\n");
return 0;
}
整数求逆:
#include <stdio.h>
int main()
{
int x;
scanf("%d",&x);
int dight;
int ret = 0;
while( x > 0){
dight = x%10;
//printf("%d\n",dight);
ret = ret*10 + dight;
printf("x=%d,dight=%d,ret=%d\n",x,dight,ret);
x /= 10;
}
printf("%d",ret);
return 0;
}
标签:do,int,number,笔记,while,ret,printf,include 来源: https://blog.csdn.net/weixin_63715821/article/details/122714212
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。