牛客竞赛语法入门班顺序结构习题C++版本参考代码及部分解析
牛客竞赛语法入门班顺序结构习题
C语言版本的参考代码
重点题:
1005 乘法表
1006 KiKi学程序设计基础
1017 水题再次来袭:明天星期几?
1018 开学?
1019 helloworld
1020 a+b
1029 计算三角形的周长和面积
1036 组队比赛
1038 长方体
1039 使徒袭来
1040 白兔的分身术
1042 Tobaku Mokushiroku Kaiji
1043 珂朵莉的假动态仙人掌
1044 旅游观光
1045 [NOIP2002]自由落体
1047 得不到的爱情[塞瓦维斯特定理]
定理例题: 2013蓝桥杯
1001 这是一道签到题
#include <iostream>
using namespace std;
int main()
{cout << "zhe" << endl;cout << "shi" << endl;cout << "yi" << endl;cout << "dao" << endl;cout << "qian" << endl;cout << "dao" << endl;cout << "ti" << endl;return 0;
}
1002 排列式
#include <iostream>
using namespace std;
int main()
{//每个cout打印一行cout << "4396 = 28 x 157" << endl;cout << "5346 = 18 x 297" << endl;cout << "5346 = 27 x 198" << endl;cout << "5796 = 12 x 483" << endl;cout << "5796 = 42 x 138" << endl;cout << "6952 = 4 x 1738" << endl;cout << "7254 = 39 x 186" << endl;cout << "7632 = 48 x 159" << endl;cout << "7852 = 4 x 1963" << endl;return 0;
}
OJ不会识别字符串里的换行,所以这种写法是错误的
#include <iostream>
using namespace std;
int main()
{cout << "4396 = 28 x 1575346 = 18 x 2975346 = 27 x 1985796 = 12 x 4835796 = 42 x 1386952 = 4 x 17387254 = 39 x 1867632 = 48 x 1597852 = 4 x 1963" << endl;return 0;
}
1003 小飞机
#include <iostream>
using namespace std;
int main()
{cout << " ** " << endl;cout << " ** " << endl;cout << "************" << endl;cout << "************" << endl;cout << " * * " << endl;cout << " * * " << endl;return 0;
}
1004 学姐的"Helloworld!"
#include <iostream>
using namespace std;
int main()
{cout << "Helo word!" << endl;return 0;
}
1005 乘法表 方法一
需要注意,两数相乘小于10的时候,可以利用空格占位。
#include <iostream>
using namespace std;
int main()
{for (int i = 1;i <= 9;i++){for (int j = 1;j <= i;j++){//这里需要注意的是打印顺序是j,i,i * j if (i * j < 10 ) printf("%d*%d= %d ",j,i,i * j);else printf("%d*%d=%d ",j,i,i * j);}printf("\n");}return 0;
}
仔细思考i
与j
分别代表的数字,如果打印顺序是i,j,i * j
,则结果如下:
1005 乘法表 方法二
OJ会自动忽略每对双引号之间的空格,故改写法等同于
1*1= 1\n1*2= 2 2*2= 4\n... ...
#include <iostream>
using namespace std;
int main()
{cout << "1*1= 1\n""1*2= 2 2*2= 4\n""1*3= 3 2*3= 6 3*3= 9\n""1*4= 4 2*4= 8 3*4=12 4*4=16\n""1*5= 5 2*5=10 3*5=15 4*5=20 5*5=25\n""1*6= 6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36\n""1*7= 7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49\n""1*8= 8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64\n""1*9= 9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81\n";return 0;
}
1006 KiKi学程序设计基础
在双引号“ ”
中再次出现单引号或者\时,需要在其前面再加上一个\
才可以输出其本身。例:
- 输出"的方法:
printf("\"");
- 输出\的方法:
printf("\\");
#include <iostream>
using namespace std;
int main()
{printf("printf(\"Hello world!\\n\");\n");printf("cout << \"Hello world!\" << endl;\n");return 0;
}
1007 疫情死亡率
%5.3f
表示结果占五位字符(包括小数点在内),其中小数部分占三位
#include <iostream>
using namespace std;
typedef long long ll;
int main()
{ll a,b;scanf("%lld %lld",&a,&b);double ans = 1.0 * b / a;//需要先乘以1.0转化为小数运算printf("%5.3f",ans * 100);printf("%");return 0;
}
1008 爱因斯坦的名言
于题号1006一样,在引号中再次出现引号时,需要在其前面再加上一个\
才可以输出其本身。
#include <iostream>
using namespace std;
int main()
{cout << "\"Genius is 1% inspiration and 99% perspiration.\"" << endl;return 0;
}
1009 字符串输出1.0
注意错误用法:while(3--)
while(x - -)
:其中,x要求是变量,不可以是常量
#include <iostream>
using namespace std;
int main()
{int x = 3;while (x--) cout << "Welcome to ACM / ICPC!" << endl;return 0;
}
1010 牛牛学说话之-整数
#include <iostream>
using namespace std;
int main()
{int a;cin >> a;cout << a << endl;return 0;
}
1011 牛牛学说话之-浮点数
保留x位小数的格式为%.xf
#include <iostream>
using namespace std;
int main()
{double d;scanf("%lf",&d);printf("%.3f",d);return 0;
}
1012 牛牛学加法
#include <iostream>
using namespace std;
int main()
{int a,b;cin >> a >> b;cout << a + b << endl;return 0;
}
1013 牛牛学除法
#include <iostream>
using namespace std;
int main()
{int a,b;cin >> a >> b;cout << a / b << endl;return 0;
}
1014 牛牛学取余
#include <iostream>
using namespace std;
int main()
{int a,b;cin >> a >> b;cout << a % b << endl;return 0;
}
1015 浮点除法
先乘以1.0转换为double类型的小数
#include <iostream>
using namespace std;
int main()
{int a,b;scanf("%d%d",&a,&b);double ans = 1.0 * a / b;printf("%.3f",ans);return 0;
}
1016 计算带余除法
#include <iostream>
using namespace std;
int main()
{int a,b;cin >> a >> b;cout << a / b << " " << a % b << endl;return 0;
}
1017 水题再次来袭:明天星期几
结论: 今天是星期x,则明天是星期x % 7 + 1
#include <iostream>
using namespace std;
int main()
{int a;cin >> a;cout << a % 7 + 1 << endl;return 0;
}
1018 开学
#include <stdio.h>
int main()
{int x,n;scanf("%d%d",&x,&n);x--;x = (x + n % 7) % 7;x %= 7;printf("%d",x + 1);return 0;
}
错误代码: 若a = 7,b = 9
,得到的结果应该是2,但实际上9 % 7 + 7 = 9,不符合实际
#include <iostream>
using namespace std;
int main()
{int a,b;cin >> a >> b;cout << b % 7 + a << endl;return 0;
}
1019 helloworld
注意:① hello world两个单词的中间有一个空格,不能忽略空格字符的下一个字符 ② 输出完空格之后不需要换行
#include <iostream>
using namespace std;
int main()
{cout << char ('h' + 1);cout << char ('e' + 1);cout << char ('l' + 1);cout << char ('l' + 1);cout << char ('o' + 1);cout << char (' ' + 1);cout << char ('w' + 1);cout << char ('o' + 1);cout << char ('r' + 1);cout << char ('l' + 1);cout << char ('d' + 1);return 0;
}
1020 a+b
cout << hex << a
与printf("%x",a)
等价,前者是C++的表示方法,后者是C语言的表示方法
方法一:
#include <iostream>
using namespace std;
int main()
{int a,b;cin >> a >> b;cout << hex << a + b << endl;return 0;
}
方法二:
#include <iostream>
using namespace std;
int main()
{int a,b;scanf("%d%d",&a,&b);printf("%x",a + b);return 0;
}
1021 整数的个位
#include <iostream>
using namespace std;
int main()
{int a; cin >> a;cout << a % 10;return 0;
}
1022 整数的十位
#include <iostream>
using namespace std;
int main()
{int a; cin >> a;cout << a / 10 % 10;return 0;
}
1023 反向输出一个四位数
#include <iostream>
using namespace std;
int main()
{int a;cin >> a;int qian = a / 1000;//得到千位数字1int bai = a / 100 % 10;//得到百位数字2int shi = a / 10 % 10;//得到十位数字3int ge = a % 10;//得到个位数字4cout << ge << shi << bai << qian;return 0;
}
题目要求输出四位数,故1000应该输出0001,若需要舍弃前导0,则取出各个位上的数字进行计算
#include <iostream>
using namespace std;
int main()
{int a;cin >> a;int qian = a / 1000;int bai = a / 100 % 10;int shi = a / 10 % 10;int ge = a % 10;int ans = ge * 1000 + shi * 100 + bai * 10 + qian;cout << ans << endl;return 0;
}
1024 总成绩和平均分计算
#include <iostream>
using namespace std;
int main()
{double a,b,c;scanf ("%lf %lf %lf",&a,&b,&c);printf("%.2f %.2f",a + b + c,(a + b + c) / 3);return 0;
}
1025 计算平均成绩
#include <iostream>
using namespace std;
int main()
{int a,b,c,d,e;scanf ("%d %d %d %d %d",&a,&b,&c,&d,&e);//需要先乘以1.0,否则结果是整数/整数 = 整数printf("%.1f",1.0 * (a + b + c + d + e) / 5);return 0;
}
1026 牛牛学梯形
#include <iostream>
using namespace std;
int main()
{int up,down,height;scanf("%d %d %d",&up,&down,&height);printf("%.3f",1.0 * (up + down) * height / 2);return 0;
}
1027 牛牛学矩形
#include <iostream>
using namespace std;
int main()
{int a,b;cin >> a >> b;cout << (a + b) * 2 << endl << a * b;return 0;
}
1028 牛牛学立体
#include <iostream>
using namespace std;
int main()
{int a,b,c;cin >> a >> b >> c;cout << 2 * a * b + 2 * a * c + 2 * b * c << endl << a * b * c;return 0;
}
1029 计算三角形的周长和面积
利用海伦公式求三角形的面积:已知三角形的三边分别长a,b,c,令p = (a + b + c) / 2
,S = sqrt (p * (p - a) * (p - b) * (p - c))
#include <iostream>
#include <cmath>
using namespace std;
int main()
{int a,b,c;scanf("%d %d %d",&a,&b,&c);double p = 1.0 * (a + b + c) / 2;//需要先乘以1.0printf("circumference=%.2f ",double (a + b + c));printf("area=%.2f",sqrt(p * (p - a) * (p - b) * (p - c)));return 0;
}
1030 你能活多少秒
不开long long泪两行
#include <iostream>
using namespace std;
int main()
{long long age;cin >> age;cout << age * 31560000;return 0;
}
1031 时间转换
#include <iostream>
using namespace std;
int main()
{int t;cin >> t;cout << t / 3600 << " " << t % 3600 / 60 << " " << t % 3600 % 60;return 0;
}
1032 温度转换
#include <iostream>
using namespace std;
int main()
{double f;scanf("%lf",&f);printf("%.3f",1.0 * 5 / 9 * (f - 32));//需要先乘以1.0return 0;
}
1033 计算机内存
#include <iostream>
using namespace std;
int main()
{int n;cin >> n;cout << n * 1024 * 1024 / 4;return 0;
}
1034 [NOIP2017]成绩
#include <iostream>
using namespace std;
int main()
{int a,b,c;cin >> a >> b >> c;cout << a * 0.2 + b * 0.3 + c * 0.5 << endl;return 0;
}
1035 KiKi的最高分
#include <iostream>
using namespace std;
int main()
{int a,b,c;cin >> a >> b >> c;cout << max(max(a,b),c);return 0;
}
1036 组队比赛
题目大意:给出四个整数a,b,c,d.选择两个数相加得到sum1,另外两个数相加得到sum2,计算sum1与sum相减的最小值(大于等于0)
思路:
① 首先,选取四个数字中的最大值和最小值,并计算最大值与最小值之和sum1
② 计算去除最大值与最小值之后,剩下两个数字相加之和sum2
③ 计算sum1与sum2之差即可,需要注意的是要保证差值大于0。如四个数字分别为1,5,6,7时,sum1 = 8,sum2 = 11,此时需要用sum2 - sum1
证明略。
写法一:
#include <iostream>
#include <algorithm>
using namespace std;int main()
{int a,b,c,d;cin >> a >> b >> c >> d;int maxn = max(max(a,b),max(c,d));//找出a,b,c,d,中的最大值int minn = min(min(a,b),min(c,d));//找出a,b,c,d,中的最小值int sum1 = maxn + minn;//最大值与最小值之和int sum2 = a + b + c + d - maxn - minn;//除去最大最小两数之和cout << max(sum1,sum2) - min(sum1,sum2) << endl;//保证结果是正值return 0;
}
数组写法:
需要注意的是fabs(x)
可能会返回double类型的值,故需要进行强制类型转换
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int score[5];
int main()
{int a,b,c,d;for (int i = 0;i < 4;i++) cin >> score[i];sort(score,score + 4);//从小到大排序int A = score[0] + score[3];int B = score[1] + score[2];int ans = (int)fabs(A - B);cout << ans;return 0;
}
1037 平方根
sqrt(x)
可以求出根号x的值,假设其为result,再将result设为int类型即可向下取整。
#include <iostream>
#include <cmath>
using namespace std;
int main()
{int n;cin >> n;int ans = sqrt(n);cout << ans;return 0;
}
1038 长方体
设长方体共享一个顶点的三条边的长度分别为x,y,z;
则其三个面的面积:
x * z = a;
y * z = b;
x * y = c;
不难发现,a * b / c = x * z * y * z / x / y = z * z = z ^ 2;
同理可以得到另外两条边长的平方。
结论:已知共享长方体一个顶点的三个面的面积,其中任意两个面积相乘之后除以第三个面积,可以得到共享长方体一个顶点的一条边的边长的平方。
#include <iostream>
#include <cmath>
using namespace std;
int main()
{int a,b,c;cin >> a >> b >> c;int l1 = a * b / c;int l2 = a * c / b;int l3 = b * c / a;cout << 4 * (sqrt(l1) + sqrt(l2) + sqrt(l3));return 0;
}
1039 使徒袭来
① a + b ≥ 2√ab,当a = b时,a + b取得最小值;
② 同理,a + b + c ≥ 3*三次根号下abc,当a = b = c时,a + b + c取得最小值;
#include <iostream>
#include <cmath>
using namespace std;
int main()
{int n;scanf("%d",&n);double x = pow(n,1.0 / 3);//注意此处不可以写成1/3,因为1/3 = 0double ans = 3 * x;printf("%.3f",ans);return 0;
}
1040 白兔的分身术
显然,一只兔子在第一轮之后变成了1p=p只兔子,第二轮之后变成了1p*p=p^2 只兔子… …第k轮之后变成 p ^ k只兔子,即 p ^ k = n;
对于指数函数a的x次方,当x越大时,函数增长就越快。相反,x越小时,函数增长就越慢;故当k = 1的时候,p可以取得最大值,且p的最大值就等于n,即p + k的最大值为n + 1。
#include <iostream>
using namespace std;
int main()
{long long n;cin >> n;cout << n + 1 << endl;return 0;
}
1041 纸牌
思路:第一轮减去的数等于纸牌上初始数字的一半即可(“对分最小思想”)
假设两张纸牌上的数字初始时均为4(奇数同理)。
第一轮:第一张纸牌上的数字变为4 - 2 = 2。(减去第二张纸牌上数字的一半)
第二轮:第二张纸牌上的数字变为4 - 2 = 2。(减去第一张纸牌上数字)
第二轮:第一张纸牌上的数字变为2 - 2 = 0。(减去第二张纸牌上数字)
代码如下,以p,q表示两张纸牌上的数字
#include <iostream>
using namespace std;
int main()
{int n;cin >> n;int b = n / 2;//b表示纸牌上数字的一半int p = n,q = n;//p,q分别表示第一、第二张纸牌上的数字p = p - b;//第一张纸牌减去第二张纸牌上数字的一半q = q - p;//第二张纸牌减去第一张纸牌上的数字p = p - q;//第一张纸牌减去第二张纸牌上的数字cout << p + q << endl;return 0;
}
1042 Tobaku Mokushiroku Kaiji
如图所示:
石头赢剪刀(a - e) :
① 如果kaiji有3个石头,对方有5个剪刀,则kaiji最多能赢3次(三个石头全用上);
② 如果kaiji有5个石头,对方有3个剪刀,则kaiji最多也能赢3次(三个剪刀全用上)。
剪刀赢布(b - f):
① 如果kaiji有3个剪刀,对方有5个布,则kaiji最多能赢3次(三个剪刀全用上);
② 如果kaiji有5个剪刀,对方有3个布,则kaiji最多也能赢3次(三个布全用上)。
布赢石头(c - d):
① 如果kaiji有3个布,对方有5个石头,则kaiji最多能赢3次(三个布全用上);
② 如果kaiji有5个布,对方有3个石头,则kaiji最多也能赢3次(三个石头全用上)。
写法一:
#include <iostream>
using namespace std;
int main()
{int a,b,c,d,e,f;cin >> a >> b >> c >> d >> e >> f;int m = (a - e > 0) ? e : a;int n = (b - f > 0) ? f : b;int q = (c - d > 0) ? d : c;cout << m + n + q;return 0;
}
写法二:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{int a,b,c,d,e,f;cin >> a >> b >> c >> d >> e >> f;cout << min(a,e) + min(b,f) + min(c,d);return 0;
}
1043 珂朵莉的假动态仙人掌
#include <iostream>
using namespace std;
int main()
{int n;;cin >> n;int day = 0;//第一天1个本子,第二天2个,第三天1个,第四天2个...//按1+2为一组,计算n个本子可以分为多少组,每组可以得到两天day += (n / 3 * 2);//计算还剩多少本子,n % 3的结果可能为0,1,2n %= 3;//只要n % 3大于0,就可以再送一天day += (n % 3 > 0) ? 1 : 0;cout << day;return 0;
}
1044 旅游观光
**分析:**已知从i到j的票价为(i+j)mod(n+1),不难发现,从1->10的票价为(1+10)mod(10+1)=11%11=0,即免费,同理2->9票价为0,3->8票价为0,4->7票价为0,5->6票价为0…(2+10)mod(10+1)=12%11=1,从2->10的票价为1,连接2->10,3->9,4->8,5->7,可得最小票价4,并且可由线路1 -> 10 -> 2 -> 9 -> 3 -> 8 -> 4 -> 7 -> 5 -> 6走完所有的点。
通过观察不难发现:
① 当n为偶数时,所需要的票价为n / 2 - 1;
② 当n为偶数时,所需要的票价为n / 2;
n为偶数:
n为奇数:
#include <iostream>
using namespace std;
int main()
{int n;cin >> n;if (n % 2) cout << n / 2;//奇数else cout << n / 2 - 1;//偶数return 0;
}
1045 [NOIP2002]自由落体 (看见物理的东西就头疼,这题不写了)
1046 挂科
#include <iostream>
using namespace std;
int main()
{int n,x,y;int maxn,minn;//maxn、minn分别表示同时挂两门课同学的最大值最小值cin >> n >> x >> y;if (x + y <= n)//10 3 5 {maxn = min(x,y);//maxn = 3minn = 0;//3个同学挂了高数,5个同学挂了物理,还有两个同学没挂科}else {maxn = min(x,y);//n = 10,x = 6,y = 7,maxn = 6minn = x + y - n;//minn = 13 - 10 = 3(6个人挂高数,4个人挂物理,6人里再选3人挂物理)}cout << maxn << " " << minn << endl;return 0;
}
1047 得不到的爱情
塞瓦维斯特定理介绍:
已知a,b为大于1的正整数,a、b的最大公约数为1或者gcd(a,b)=1或者a与b互质或者a与b互素,则使不定方程ax+by=C不存在的最大非负整数C=ab−a−b
定理证明(证明有点难,不会也没事,反正我不会)
例1:为什么输入的数要大于等于2?
假设a = 1,b = 2
C = (1 - 1) * 2 - 1 = -1
例2:a = 3,b = 4
C = (3 - 1) * 4 - 3 = 5
例3:a = 4,b = 7
C = (4 - 1) * 7 - 4;
根据规律:不存在的最大值C = (a - 1) * b - a = ab - b - a
本题中,N和M都取最大值50000,N x M = 2500000000超过了int的范围,故需要开long long
#include <iostream>
using namespace std;
int main()
{long long n,m;cin >> n >> m;cout << n * m - n - m << endl;return 0;
}
牛客竞赛语法入门班顺序结构习题C++版本参考代码及部分解析相关推荐
- 牛客竞赛语法入门班循环结构习题C++版本参考代码及部分解析
重点题 1001 上下金字塔 1002 数字三角形 1003 字符金字塔 1008 牛牛学数列3 1017 栗酱数数 1018 有趣的二进制 1019 [NOIP2006]数列 1024 买铅笔 10 ...
- 【python】牛客竞赛语法入门班顺序结构习题 python解法
题目链接:牛客竞赛语法入门班顺序结构习题_ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛_牛客竞赛OJ 目录 1001 这是一道签到题 1002 排列式 1003 小飞机 1004 学 ...
- 牛客竞赛语法入门班顺序结构习题(重现赛)
1039-使徒袭来 链接:登录-专业IT笔试面试备考平台_牛客网 来源:牛客网 神秘的使徒袭击了第三新东京市,少男少女们驾驶着决战兵器EVA守护着人类的和平. 牛可乐是NERV特务机关的指挥官,他必须 ...
- 牛客竞赛语法入门班顺序结构习题
1001~1047的题目及代码 1001 这是一道签到题 1001 代码 1002 排列式 1002 代码 1003 小飞机 1003 代码 1004 学姐的"Helloworld!&quo ...
- 牛客竞赛语法入门班顺序结构习题【完结】
题单地址:https://ac.nowcoder.com/acm/contest/18839?from=acdiscuss#question 入门题,太简单的直接代码,其他的有题目和代码 目录 这是一 ...
- 牛客竞赛语法入门班-循环结构习题代码(1)
链接:登录-专业IT笔试面试备考平台_牛客网 来源:牛客网 目录 1001 上下金字塔 1002 数字三角形 1003 字符金字塔 1004 涂小天与他的画 1005 箭形图案 1006 牛牛学数列 ...
- 牛客竞赛语法入门班循环结构习题【完结】
入门级别的题 题单地址:https://ac.nowcoder.com/acm/contest/19305?from=acdiscuss#question/%22page%22%3A1 目录 上下金字 ...
- 牛客竞赛语法入门班函数与递归习题【未完结】
题单地址:https://ac.nowcoder.com/acm/contest/19859?from=acdiscuss 目录 [NOIP2010]数字统计 日历中的数字 素数回文 数位五五 233 ...
- 牛客竞赛语法入门班数组字符串习题【完结】
题目地址:https://ac.nowcoder.com/acm/contest/19306?from=acdiscuss 目录 随机序列 [NOIP2013]记数问题 约瑟夫环 [NOIP2005] ...
最新文章
- Puppet学习之文件管理
- 手把手教你手动创建线程池
- centos6.5 设置ssh无密码登录
- 名企进名校精选IT人 07年毕业生就业看好
- 如何查找Power BI本地报表服务器产品密钥
- 有小数点是什么类型_「JAVA零基础入门系列」Day3 Java基本数据类型
- Borland Enterprise Core Object II (ECO II)第一次接觸
- 如何打开别人的Android项目
- 分形之谢尔宾斯基(Sierpinski)三角形
- Hibernate(6)——映射类型
- 视频网站视频倍速的方法(亲测B站、百度网盘)
- O2O新猜想:如果商家这样做,还需要团购平台吗
- NRF51822 回顾总结
- 考研英语近义词与反义词·二
- Flume 入门教程(超详细)
- 诺基亚联手迪信通 力推内置仙剑三版5230手机
- OpenCV利用滑动条实现一个开关
- F. chino with ball
- XP系统下安装SQL Server2000标准版/企业版
- Lisp的本质 - 从另一种角度洞悉Lisp之美