2018.7.28
0-0 热身运动
小明跟着某软件的指令做热身运动,该软件的某项热身操可以发出一系列指令,小明想要消耗掉至少x的能量,至多y的能量,帮他计算是否可以按照这一系列的指令得到这个要求。
输出1代表可以达到要求,输出0代表不可以达到要求。
指令有3种
深蹲(A),原地起跳(B),俯卧撑(C),消耗的能量分别为20 10 25
例如输入:
100 150
AAACB
输出:
0//因为一共消耗的能量是95,所以不可以。
0-1 现在小明不仅想知道做完这个热身操能不能达到要求,他还想知道如果达不到要求,还差至少多少热量需要消耗或者多消耗了最少多少热量。
输出为:
0
5 //因为要得到要求,必须要再消耗5的热量。
代码1(自己写的):
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int x,y,c;
int cot=0;
bool sym;
scanf("%d %d",&x,&y);
getchar();
while((c=getchar())!='\n')
{
if(c=='A')
cot+=20;
else if(c=='B')
cot+=10;
else if(c=='C')
cot+=25;
}
printf("cot的值为:%d\n",cot);
if(cot>=x&&cot<=y)
{
sym=1;
printf("1-已达到要求\n");
}
else
{
sym=0;
printf("0-未达到要求\n");
}
if(!sym)
{
if(cot<x) printf("还差%d才能达到要求\n",x-cot);
else printf("多消耗了%d\n",cot-y);
}
return 0;
}
代码2(西交wrong):
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
const int maxn = 100000;
typedef pair<int,int> P;
//char s[105];
int cost[]={20,10,25};
int main()
{
int x,y,res=0;
scanf("%d%d",&x,&y);
char c;
getchar();
while((c=getchar())!='\n') res+=cost[c-'A']; //O(strlen(s)) O(1)
/*
for(int i=0;i<strlen(s);i++) //O(strlen(s)) O(strlen(s))
if(s[i]=='A') res+=20;
else if(s[i]=='B') res+=10;
else
if(s[i]=='C') res+=25;
if(c>='a')
cost[c-'a'+26]
else
cost[c-'A']
*/
cout<<res<<endl;
if(res>=x&&res<=y) printf("1\n");
else
{
printf("0\n");
//x y res
printf("%d\n",min(abs(res-y),abs(res-x)));
//if(res<x) printf("%d\n",x-res);
//else printf("%d\n",res-y);
}
return 0;
}
(感谢西交wrong学长提供以上题目练习)