day04:顺序结构实例
1. 买铅笔
【题目描述】班主任给小玉一个任务,到文具店里买尽量多的签字笔。已知一只签字笔的价格是1元9角,而班主任给小玉的钱是a元b角,小玉想知道,她最多能买多少只签字笔呢?
输入样例:10 3 输出样例:5
#include<iostream>
#include<cstdio>
using namespace std;
int main() {
int price = 19, a,b;
scanf("%d%d", &a, &b); // cin>>a>>b;
int money = a*10+b;
printf("%d\n", (int)(money/price)); // cout<<(int)(money/price)<<endl;
int num = money/price;
printf("%d", num); // cout<<num;
return 0;
}
2. 公交车
【题目描述】公交车公司要统计公交车从始发站到末站所花费的时间。已知公交车与a时b分从始发站出发,并于当天的c时d分到终点站(以上表述均为24小时制)。公交车从始发站到终点站共花费了e小时f分钟(0<=f<60),要求输出e和f的值。
输入样例:12 5 13 19 输出样例:1小时14分钟
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
int a,b,c,d,e,f;
scanf("%d%d%d%d", &a, &b, &c, &d); // cin>>a>>b>>c>>d;
int time = c*60+d - (a*60+b);
e = time/60;
f = time%60;
printf("%d小时%d分钟", e, f); // cout<<e<<"小时"<<f<<"分钟";
return 0;
}
3. 数的幂
【题目描述】输入四个正整数a,b,c,n(a,b,c均小于200, n<=6),求an+bn+c^n。
输入样例:34 56 7 5 输出样例:S=596184007
#include<cstdio>
#include<cmath>
int main(){
int a, b, c, n, sum;
scanf("%d%d%d%d", &a, &b, &c, &n); //cin>>a>>b>>c>>n;
sum = pow(a,n) + pow(b,n) + pow(c,n);
printf("%d", sum); // cout<<sum;
return 0;
}
4. 等差数列
【题目描述】给定整数等差数列的首项a和末项b以及项数n,求等差数列各项的总和。数据范围:0<=a, b<=10^9, n<=200。公式:sum =(首项+末项)*n/2
输入样例:5 10005 5 输出样例:等差数列的和为25025
#include <iostream>
#include<cstdio>
using namespace std;
int main() {
double a, b, n;
long long sum;
scanf("%lf%lf%lf", &a, &b, &n); // cin>>a>>b>>n;
sum = ((long long) a+b)*n/2;
printf("等差数列的和为%lld", sum); // cout<<"等差数列的和为"<<sum;
return 0;
}