C语言基础算法————输入三个值,并且由小到大输出 1

这篇博客包含两部分:一是使用C语言和JavaScript实现输入三个数并按从小到大顺序输出的代码;二是解决一个数学问题,即猴子吃桃子的递归模型,通过C语言编写程序反推猴子第一天摘了多少桃子。内容涉及编程基础和数学思维应用。
摘要由CSDN通过智能技术生成

1.输入三个值,并且由小到大输出 1
c代码

#include <stdio.h>

int main(){
	int a,b,c,t;
	printf("输入三个数:");
	scanf("%d,%d,%d",&a,&b,&c); 
	if(a>b)
	{
		t = a;
		a = b;
		b = t;
	}
	if(a>c)
	{
		t = a;
		a = c;
		c =t ;
	}
if(b>c){
	t = b;
	b = c;
	c = t;
}

printf("顺序:\n");
printf("%d,%d,%d",a,b,c);
	return 0;
}

js代码

var a,b,c,t;
a = +prompt("请输入第一个值: ");
b = +prompt("请输入第二个值: ");
c = +prompt("请输入第三个值: ");
if(a>b)
	{
		t = a;
		a = b;
		b = t;
	}
if(a>c)
	{
		t = a;
		a = c;
		c =t ;
	}
if(b>c){
	t = b;
	b = c;
	c = t;
}
document.write("输入三个数:"+ a+','+b+','+c);

2.猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。第二天早上又将第一天剩下的桃子吃掉一半,有多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第 10 天早上想再吃时,发现只剩下一个桃子了。编写程序求猴子第一天摘了多少个桃子。
c

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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int  day,t1,t2;
	
	printf("day:");
	scanf("%d",&day);
	printf("t2:");
	scanf("%d",&t2);
	while(day > 0)
	{
		t1 = (t2+1)*2;
		t2 = t1;
		day--;
		
	}
	printf("num is:");
	printf("%d",t1);
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值