7-12 高空坠球 (20分)

注意

  1. 两个非负整数:讨论0和1
  2. 注意整数要除以2.0:endh=h/2.0;

代码

#include<iostream>
#include<cmath>
using namespace std;

int main(){
	int h,n;cin>>h>>n;
	double sum=h,endh=h/2.0;
	if(n==0){
		printf("0.0 0.0");
	}else if(n==1){
		printf("%.1lf %.1lf",sum,endh);
	}
	else{
		for(int i=0;i<=n-2;i++){
			sum+=(h/pow(2,i));
		}
		for(int j=2;j<=n;j++){
			endh=endh/2.0;
		}
		printf("%.1lf %.1lf",sum,endh);
	}
	
}

参考代码

#include<stdio.h>
int main()
{
  int i=0,n,height;
  double s,h;
  scanf("%d %d",&height,&n);
  h=height;
  s=-h;
  do{
    s=s+h*2;
    h=h/2;
    i++;
  }while(i<n);
  
  if(n==0) printf("%.1lf %.1lf\n",height,height);
  else printf("%.1lf %.1lf\n",s,h);
}

学长的代码

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define rep(i,a,b) for(ll i=(a);i<=(b);++i)
#define per(i,a,b) for(ll i=(a);i>=(b);--i)
#define mem(a,b) memset(a,b,sizeof a)
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;
template <class T>
inline void read(T &x) {
	x=0;
	int f=0;
	char c=getchar();
	while(!isdigit(c)) {
		f|=(c=='-');
		c=getchar();
	}
	while(isdigit(c)) {
		x=(x<<3)+(x<<1)+c-'0';
		c=getchar();
	}
	if(f)x=-x;
}
const int maxn=1e+5;
int n;
int ksm(int a,int b){
	int s=1;
	while(b){
		if(b&1)
			s*=a;
		a=a*a;
		b>>=1;
	}
	return s;
}
int main(){
	ll t;
	double h,s;
	read(t);
	read(n);
	if(n==0){
		cout << "0.0 0.0" << endl;
		return 0;
	}
	s=h=t;
	rep(i,1,n-1){
		s+=h;
		h/=2;
	}
	printf("%.1f %.1f",s,h/2);
	return 0;
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DDouble-

你的鼓励是我最大的动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值