标签:
===== 01:小明买雪糕 =====
小明有5元钱,他想去买雪糕吃,雪糕的价格各不相同,根据雪糕的价格,计算小明最多能买多少根雪糕。
1.3
3
#include <iostream>
using namespace std;
int main() {
double a;
cin >> a;
cout << (int)(5 / a) << endl;
return 0;
}
===== 02:判断闰年 =====
2006
N
#include <iostream>
using namespace std;
int main(){
int a;
cin >> a;
/*
闰年的判断条件:
能被4整除的大多数是闰年
但能被100整除,而不能被400整除的不是闰年
能被3200整除的也不是闰年
*/
if (a % 4 == 0 && !(a % 100 == 0 && a % 400 != 0) &&(a % 3200 != 0))
cout << ‘Y‘;
else
cout << ‘N‘;
cout << endl;
return 0;
}
===== 03:短信计费 =====
用手机发短信,一般一条短信资费为0.1元,但限定每条短信的内容在70个字以内(包括70个字)。如果你所发送的一条短信超过了70个字,则大多数手机 会按照每70个字一条短信的限制把它分割成多条短信发送。假设已经知道你当月所发送的每条短信的字数,试统计一下你当月短信的总资费。
10 39 49 42 61 44 147 42 72 35 46
1.3
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
// n是短信条数
int n;
// words用于记录一条短信的数目
int words;
// money是短信总资费
double cost = 0.0;
// 输入短信总条数
cin >> n;
// 处理每条短信
for (int i = 0; i < n; i++){
cin >> words;
if(words % 70 !=0)
cost += words/ 70 * 0.1+0.1;
else
cost +=words / 70 * 0.1;
}
cout << fixed << setprecision(1) << cost << endl;
return 0;
}
输出一个整数数列中不与最大数相同的数字之和
3 1 2 3
3
#include <iostream>
using namespace std;
int main(){
//n是输入的数字个数,n<=100
int n;
//a记录输入的数字
int a[100];
//max记录最大数
int max = 0;
//sum记录除去最大数之后的数字之和
int sum = 0;
int i = 0;
cin >> n;
// 输入并找出最大数
for (i = 0; i < n; i++){
cin >> a[i];
if (a[i] > max)
max = a[i];
}
// 求最大数以外的和
for (i = 0; i < n; i++) {
if (a[i] != max)
sum += a[i];
}
// 输出
cout << sum;
return 0;
}
0 5 15 105
3 5 7 5 3 5 3 5 7
#include <iostream>
using namespace std;
int main() {
int n;
while(cin >> n) {
if(n % 3 == 0) {
cout << 3 << " ";
}
if(n % 5 == 0) {
cout << 5 << " ";
}
if(n % 7 == 0) {
cout << 7 << " ";
}
if(n%3!=0 && n%5!=0 && n%7!=0) {
cout << "n";
}
cout << endl;
}
return 0;
}
鸡尾酒疗法,原指“高效抗逆转录病毒治疗”(HAART),由美籍华裔科学家何大一于1996年提出,是通过三种或三种以上的抗病毒药物联合使用来治疗艾 滋病。该疗法的应用可以减少单一用药产生的抗药性,最大限度地抑制病毒的复制,使被破坏的机体免疫功能部分甚至全部恢复,从而延缓病程进展,延长患者生 命,提高生活质量。人们在鸡尾酒疗法的基础上又提出了很多种改进的疗法。为了验证这些治疗方法是否在疗效上比鸡尾酒疗法更好,可用通过临床对照实验的方式 进行。假设鸡尾酒疗法的有效率为x,新疗法的有效率为y,如果y-x大于5%,则效果更好,如果x-y大于5%,则效果更差,否则称为效果差不多。下面给 出n组临床对照实验,其中第一组采用鸡尾酒疗法,其他n-1组为各种不同的改进疗法。请写程序判定各种改进疗法效果如何。
5 125 99 112 89 145 99 99 97 123 98
same worse better same
#include <iostream>
using namespace std;
int main(){
// 组数
int num;
cin >> num;
// 记录鸡尾酒疗法的总人数、有效人数
double totalCock, positiveCock;
double ratioCock;
cin >> totalCock >> positiveCock;
if(totalCock > 0 && positiveCock > 0 && totalCock > positiveCock){
ratioCock = (positiveCock / totalCock) * 100;
// cout << ratioCock;
// 处理n-1个数据
num = num -1;
for(int i = 0; i < num; i++){
double tmpTotal = 0, tmpPositive = 0;
cin >> tmpTotal >> tmpPositive;
if((tmpTotal > tmpPositive )&& (tmpTotal > 0) && (tmpPositive > 0)){
double tmpRatio = (tmpPositive / tmpTotal) * 100;
if (tmpRatio-ratioCock > 5){
cout << "better" << endl;
}
else if (ratioCock - 5 > tmpRatio ){
cout << "worse" << endl;
}
else{
cout << "same" << endl;
}
}
else{
cout << endl;
continue;
}
}
}
return 0;
}
6 34.0 23.0 28.1 21.6 14.7 17.1 17.0 27.2 34.7 67.1 29.3 65.1
53.8516
#include <iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main(){
// 所有点的X,Y坐标。假设最多有100个点
double X[100], Y[100];
// 记录最大的距离平方
double maxDisSquare = 0;
// 点的数量
int num;
cin >> num;
for (int i = 0; i < num; i++){
cin >> X[i] >> Y[i];
// 比较与已有的点的距离
for (int j = 0; j < i; j++){
double DisSquare = pow(X[j]-X[i],2)+pow(Y[j]-Y[i],2);
if (maxDisSquare < DisSquare) maxDisSquare = DisSquare;
}
}
cout << fixed << setprecision(4) << sqrt(maxDisSquare) << endl;
return 0;
}
24
Cube = 6, Triple = (3,4,5) Cube = 12, Triple = (6,8,10) Cube = 18, Triple = (2,12,16) Cube = 18, Triple = (9,12,15) Cube = 19, Triple = (3,10,18) Cube = 20, Triple = (7,14,17) Cube = 24, Triple = (12,16,20)
#include<iostream>
using namespace std;
int main(){
// 上限
int limit;
cin >> limit;
for(int a = 2; a <= limit; a++){
for(int b = 2; b < a; b++)
for(int c = b; c < a; c++)
for(int d = c; d < a; d++){
if(a * a * a == b * b * b + c * c * c + d * d * d)
cout << "Cube = " << a << ", Triple = ("
<< b << "," << c << "," << d << ")" << endl;
}
}
return 0;
}
输入一个正整数,请判断它是不是素数。如果是输出yes,不是则输出no
10
no
#include <iostream>
using namespace std;
int main(){
bool is = true;
int num;
cin >> num;
if (num == 1) {
cout << "no" << endl;
return 0;
}
for (int i = 2; i < num; i++) {
if (num % i == 0){
is = false;
break;
}
}
cout << ((is)?"yes":"no") << endl;
return 0;
}
2 abbccc adfadffasdf
c 3 f 4
#include<iostream>
#include<cstring>
using namespace std;
int main(){
// 组数
int group;
cin >> group;
// 字符串
char str[1001];
// 不断地输入并处理每组数据
for(int iG = 0; iG < group; iG++){
cin >> str;
int cnt[27] = {0};
for(int i = 0; i < strlen(str); i++){
// 计数并累计至cnt数组中
cnt[str[i]-‘a‘]++;
}
// 找出出现最多的字母及其数量
int mostAlphabetIdx = 0;
int maxCnt = 0;
for (int i=0; i<26; i++) {
if (cnt[mostAlphabetIdx] < cnt[i]) {
mostAlphabetIdx = i;
}
}
maxCnt = cnt[mostAlphabetIdx];
cout << (char) (‘a‘ + mostAlphabetIdx)
<< " " << maxCnt << endl;
}
return 0;
}
2 1 5 8 10 5 1 1 2 3 4 3 5 6 3 100 1 1 0 1 0
2140.20 4427.80
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
// 多组数据
int numGroup = 0;
cin >> numGroup;
// 书的数量
int numBook = 10;
// price为每本书的价格
double price[] = {28.9, 32.7, 45.6, 78, 35, 86.2, 27.8, 43, 56, 65};
// 处理多组输入
for (int iGroup = 0; iGroup < numGroup; iGroup++){
// sum为总价
double sum = 0;
// 处理每一本书
for (int iBook = 0; iBook < numBook; iBook++){
int num;
cin >> num;
sum += num * price[iBook];
}
// 按要求输出
cout << fixed << setprecision(2) << sum << endl;
}
return 0;
}
5 4 3 4 2 3 91 88 72 69 56
2.52
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
// 成绩与绩点对应关系共10档
// SCORE记录每一档成绩的下限、对应的GP记录这一档成绩的绩点
int nCLASS = 10;
int SCORE[] = {90, 85, 82, 78, 75, 72, 68, 64, 60, 0};
double GP[] = {4, 3.7, 3.3, 3.0, 2.7, 2.3, 2.0, 1.5, 1.0, 0};
// credits记录每门课
int credits[10] = {0};
// 课程数
int nCourse = 0;
cin >> nCourse;
// 输入所有课的学分
for (int iCourse = 0; iCourse < nCourse; iCourse++)
cin >> credits[iCourse];
// 初始化总学分和总绩点(乘以权重)
// 即 totalGP = sum over all courses {credit * gp}
int totalCredit = 0;
double totalGP = 0;
// 输入每门课的成绩并判断绩点,并累加至totalGP中
for (int iCourse = 0; iCourse < nCourse; iCourse++){
int tmpScore = 0;
cin >> tmpScore;
totalCredit += credits[iCourse];
// 判断这个成绩落入哪个绩点类别
for (int iClass = 0; iClass < nCLASS; iClass++){
if (tmpScore >= SCORE[iClass]){
totalGP += credits[iCourse] * GP[iClass];
break;
}
}
}
cout << fixed << setprecision(2) << (totalGP / totalCredit) << endl;
}
2 3 3.46
5.00 5.00 5.92
#include<iostream>
#include <iomanip>
using namespace std;
int main(){
double time;
while(cin >> time){
double cost = 0;
if(time <= 3)
cost = 5.00;
else if(time <= 24){
cost = 5.00 + (time - 3)*2;
if(cost > 40)
cost = 40;
}
cout << fixed << setprecision(2) << cost << endl;
}
return 0;
}
5
225
#include <iostream>
#include<iomanip>
using namespace std;
int main(){
int k;
cin >> k;
int sum = 1;
for(int i = 2; i <= k; i++){
sum += i*i*i;
}
cout << sum << endl;
return 0;
}
对一个整数n,如果其各个位数的数字相加得到的数m能整除n,则称n为自整除数.例如21,21%(2+1)==0,所以21是自整除数.现求出从10到n(n < 100)之间的所有自整除数.
47
10 12 18 20 21 24 27 30 36 40 42 45
#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
for(int i = 10; i <= n; i++)
if(i % (i / 10 + i % 10) == 0)
cout << i << endl;
return 0;
}
#include <iostream>
using namespace std;
int main(){
for(int i = 2; i <= 100; i++){
for(int j = i; j <= 100; j++){
int k = i*i + j*j;
for(int p = 2; p <= 100; p++){
if(p*p == k)
cout << i << ‘*‘ << i << " + " << j << ‘*‘ << j << " = " << p << ‘*‘ << p << endl;
}
}
}
return 0;
}
10 41.0 18467.0 6334.0 26500.0 19169.0 15724.0 11478.0 29358.0 26962.0 24464.0
18467.000000 / 6334.000000 = 2.915535
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int n; // 数字的个数
cin >> n;
float maxq = 0; // maxq记录截止当前的最大商,初始为0。注意:这道题请用float
float maxd = 0; // maxd记录最大商的分子
float maxn = 0; // maxn记录最大商的分母
float last2; // last2记录当前数的前两个数
float last; // last记录当前数
cin >> last; // 输入第一个数,且无需比较
for (int i = 1; i < n; i++) {
// 更新本次读入的数
last2 = last;
cin >> last;
if (last2 / last > maxq) {
maxq = last2 / last;
maxd = last2;
maxn = last;
}
}
cout << fixed << setprecision(6) << maxd << " / " << maxn << " = " << maxq << endl;
return 0;
}
一个最简单的计算器,支持+, -, *, / 四种运算。仅需考虑输入输出为整数的情况,数据和运算结果不会超过int表示的范围。
1 2 +
3
#include<iostream>
using namespace std;
int main(){
int a, b;
char op;
cin >> a >> b >> op;
if (op == ‘+‘)
cout << a + b << endl;
else if (op == ‘-‘)
cout << a - b << endl;
else if (op == ‘*‘)
cout << a * b << endl;
else if (op == ‘/‘) {
if (b == 0)
cout << "Divided by zero!"<< endl;
else if (b != 0)
cout << a / b << endl;
} else
cout << "Invalid operator!" << endl;
}
abcab eee 12343 555
abceeeab 12345553
#include <iostream>
using namespace std;
int main() {
char string[20], substr[10];
// 处理多组数据
while (cin >> string){
cin >> substr;
// get max ASCII char & location index
int maxChar = 0;
int maxIndex = 0;
// 遍历数组,并获得相关信息
for (int i = 0; i < 11; i++) {
if (string[i] == ‘\0‘) break;
if (string[i] > maxChar){
maxIndex = i;
maxChar = string[i];
}
}
// 输出,先输出str的前半部分、再输出substr、最后输出str的后半部分
for (int i = 0; i <= maxIndex; i++) {
cout << string[i];
}
cout << substr;
for (int i = maxIndex + 1; i < 11; i++) {
if (string[i] == ‘\0‘) break;
cout << string[i];
}
cout << endl;
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/guyueliushang/p/4244561.html