7-31 求整数段和 (15point(s))
给定两个整数A和B,输出从A到B的所有整数以及这些数的和。
输入格式:
输入在一行中给出2个整数A和B,其中−100≤A≤B≤100,其间以空格分隔。
输出格式:
首先顺序输出从A到B的所有整数,每5个数字占一行,每个数字占5个字符宽度,向右对齐。最后在一行中按Sum = X的格式输出全部数字的和X。
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
int main()
{
int a, b;
cin>>a>>b;
int sum = 0;
int n = 0;
for(int i = a; i <= b; i++)
{
sum += i;
n ++;
cout<<setw(5)<<std::right<<i;
if(n % 5 == 0)
{
n = 0;
cout<<endl;
}
}
if(n % 5)
{
cout<<endl<<"Sum = "<<sum;
}
else{
cout<<"Sum = "<<sum;
}
}
7-32 求符合给定条件的整数集 (15point(s))
给定不超过6的正整数A,考虑从A开始的连续4个数字。请输出所有由它们组成的无重复数字的3位数。
输入格式:
输入在一行中给出A。
输出格式:
输出满足条件的的3位数,要求从小到大,每行6个整数。整数间以空格分隔,但行末不能有多余空格。
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
int main()
{
char a;
string str= "";
cin>>a;
str += a;
str += a + 1;
str += a + 2;
str += a + 3;
int n = 0;
for(int i = 0; i < str.length(); i ++)
{
for(int j = 0; j < str.length(); j ++)
{
if(j != i)
{
for(int k = 0; k < str.length(); k ++)
{
if(k != j && k != i)
{
n ++;
cout<<str[i]<<str[j]<<str[k];
if(n % 6)
{
cout<<" ";
}else{
cout<<endl;
}
}
}
}
}
}
}
7-33 厘米换算英尺英寸 (15point(s))
如果已知英制长度的英尺foot和英寸inch的值,那么对应的米是(foot+inch/12)×0.3048。现在,如果用户输入的是厘米数,那么对应英制长度的英尺和英寸是多少呢?别忘了1英尺等于12英寸。
输入格式:
输入在一行中给出1个正整数,单位是厘米。
输出格式:
在一行中输出这个厘米数对应英制长度的英尺和英寸的整数值,中间用空格分开。
#include <iostream>
using namespace std;
int main()
{
int cm, foot, inch;
double m;
cin >> cm;
foot = cm / 30.48;
m = cm / 30.48 - foot;
inch = 12 * m;
cout << foot << " " << inch;
return 0;
}
7-34 12-24小时制 (15point(s))
编写一个程序,要求用户输入24小时制的时间,然后显示12小时制的时间。
输入格式:
输入在一行中给出带有中间的:符号(半角的冒号)的24小时制的时间,如12:34表示12点34分。当小时或分钟数小于10时,均没有前导的零,如5:6表示5点零6分。
提示:在scanf的格式字符串中加入:,让scanf来处理这个冒号。
输出格式:
在一行中输出这个时间对应的12小时制的时间,数字部分格式与输入的相同,然后跟上空格,再跟上表示上午的字符串AM或表示下午的字符串PM。如5:6 PM表示下午5点零6分。注意,在英文的习惯中,中午12点被认为是下午,所以24小时制的12:00就是12小时制的12:0 PM;而0点被认为是第二天的时间,所以是0:0 AM。
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
int a, b;
char ch;
cin>>a>>ch>>b;
if(a< 12)
{
cout<<a<<":"<<b<<" AM";
}else if(a > 12){
cout<<a % 12<<":"<<b<<" PM";
}else{
cout<<a<<":"<<b<<" PM";
}
}
7-35 到底有多二 (15point(s))
一个整数“犯二的程度”定义为该数字中包含2的个数与其位数的比值。如果这个数是负数,则程度增加0.5倍;如果还是个偶数,则再增加1倍。例如数字-13142223336是个11位数,其中有3个2,并且是负数,也是偶数,则它的犯二程度计算为:3/11×1.5×2×100%,约为81.82%。本题就请你计算一个给定整数到底有多二。
输入格式:
输入第一行给出一个不超过50位的整数N。
输出格式:
在一行中输出N犯二的程度,保留小数点后两位。
#include<iostream>
#include<string>
#include<algorithm>
#include<iomanip>
using namespace std;
int main()
{
string str;
cin>>str;
int num = count(str.begin(), str.end(), '2');
float zheng = 0;
float ou;
int L = str.length();
if((str[str.length() - 1] - '0') % 2 == 0)
{
ou = 2;
}
else{
ou = 1;
}
if(str[0] == '-')
{
zheng = 1.5;
L --;
}else{
zheng = 1;
}
cout<<fixed<<setprecision(2)<<zheng * ou * num * 100/ L<<"%";
}
7-36 谁先倒 (15point(s))
划拳是古老中国酒文化的一个有趣的组成部分。酒桌上两人划拳的方法为:每人口中喊出一个数字,同时用手比划出一个数字。如果谁比划出的数字正好等于两人喊出的数字之和,谁就输了,输家罚一杯酒。两人同赢或两人同输则继续下一轮,直到唯一的赢家出现。
下面给出甲、乙两人的酒量(最多能喝多少杯不倒)和划拳记录,请你判断两个人谁先倒。
输入格式:
输入第一行先后给出甲、乙两人的酒量(不超过100的非负整数),以空格分隔。下一行给出一个正整数N(≤100),随后N行,每行给出一轮划拳的记录,格式为:
甲喊 甲划 乙喊 乙划
其中喊是喊出的数字,划是划出的数字,均为不超过100的正整数(两只手一起划)。
输出格式:
在第一行中输出先倒下的那个人:A代表甲,B代表乙。第二行中输出没倒的那个人喝了多少杯。题目保证有一个人倒下。注意程序处理到有人倒下就终止,后面的数据不必处理。
#include<iostream>
#include<string>
#include<algorithm>
#include<iomanip>
using namespace std;
int main()
{
int m, n;
int x;
cin>>m>>n;
cin>>x;
int key = 1;
int m1 = m, n1 = n;
int p = 0;
for(int i = 0; i < x; i ++)
{
int a, b, c, d;
cin>>a>>b>>c>>d;
if(key)
{
if(b == a + c && d != a + c)
{
m1 --;
if(m1 < 0)
{
p = 1;
key = 0;
}
}
if(d == a + c && b != a + c)
{
n1 --;
if(n1 < 0)
{
key = 0;
}
}
}
}
if(p)
{
cout<<"A"<<endl<<n - n1;
}else{
cout<<"B"<<endl<<m - m1;
}
}
7-37 支票面额 (15point(s))
一个采购员去银行兑换一张y元f分的支票,结果出纳员错给了f元y分。采购员用去了n分之后才发觉有错,于是清点了余额尚有2y元2f分,问该支票面额是多少?
输入格式:
输入在一行中给出小于100的正整数n。
输出格式:
在一行中按格式y.f输出该支票的原始面额。如果无解,则输出No Solution。
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n;
cin>>n;
int k = 0;
for(int i = 0; i <= 100; i ++)
{
for(int j = 0; j <= 100; j ++)
{
if(98 * i - 199 * j == n)
{
k = 1;
cout<<j<<"."<<i<<endl;
// break;
}
}
}
if(!k)
{
cout<<"No Solution";
}
}
7-38 猜数字游戏 (15point(s))
猜数字游戏是令游戏机随机产生一个100以内的正整数,用户输入一个数对其进行猜测,需要你编写程序自动对其与随机产生的被猜数进行比较,并提示大了(“Too big”),还是小了(“Too small”),相等表示猜到了。如果猜到,则结束程序。程序还要求统计猜的次数,如果1次猜出该数,提示“Bingo!”;如果3次以内猜到该数,则提示“Lucky You!”;如果超过3次但是在N(>3)次以内(包括第N次)猜到该数,则提示“Good Guess!”;如果超过N次都没有猜到,则提示“Game Over”,并结束程序。如果在到达N次之前,用户输入了一个负数,也输出“Game Over”,并结束程序。
输入格式:
输入第一行中给出两个不超过100的正整数,分别是游戏机产生的随机数、以及猜测的最大次数N。最后每行给出一个用户的输入,直到出现负数为止。
输出格式:
在一行中输出每次猜测相应的结果,直到输出猜对的结果或“Game Over”则结束。
#include<stdio.h>
int main()
{
int a,n,c,i=0;
scanf("%d %d %d",&a,&n,&c);
while(c>=0&&i<n){
i++;
if(i==1&&a==c) {
printf("Bingo!");
break;
}
else if(i<=3&&a==c) {
printf("Lucky You!");
break;
}
else if(a==c) {
printf("Good Guess!");
break;
}else if(a<c) {
printf("Too big\n");
}else if(a>c) {
printf("Too small\n");
}
if(i<n) scanf("%d",&c);
}
if(a!=c) printf("Game Over");
}
7-39 字符串替换 (15point(s))
本题要求编写程序,将给定字符串中的大写英文字母按以下对应规则替换:
原字母 对应字母
A Z
B Y
C X
D W
… …
X C
Y B
Z A
输入格式:
输入在一行中给出一个不超过80个字符、并以回车结束的字符串。
输出格式:
输出在一行中给出替换完成后的字符串。
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string str;
getline(cin, str);
for(int i = 0; i < str.length(); i ++)
{
if(str[i] >= 'A' && str[i] <='Z')
{
str[i] = 'A' + 'Z' - str[i];
}
}
cout<<str;
}
7-40 念数字 (15point(s))
输入一个整数,输出每个数字对应的拼音。当整数为负数时,先输出fu字。十个数字对应的拼音如下:
0: ling
1: yi
2: er
3: san
4: si
5: wu
6: liu
7: qi
8: ba
9: jiu
输入格式:
输入在一行中给出一个整数,如:1234。
提示:整数包括负数、零和正数。
输出格式:
在一行中输出这个整数对应的拼音,每个数字的拼音之间用空格分开,行末没有最后的空格。如 yi er san si。
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string str[10] = {"ling", "yi", "er","san","si","wu","liu","qi","ba","jiu"};
string strx;
cin>>strx;
for(int i = 0; i < strx.length(); i ++)
{
if(strx[i] == '-')
{
cout<<"fu ";
continue;
}
if(i + 1 != strx.length())
{
cout<<str[strx[i] - '0']<<" ";
}else{
cout<<str[strx[i] - '0']<<endl;
}
}
}