十道入门题
题目来源,题目,简单解析,代码,输入输出
目录
前言
一,汉诺塔
二,判断闰年
三,大写变小写
四,破译密码
五,反向数相加
六,Excel表中的列号
七,饮料兑换
八,角谷猜想
九,数字统计
十,素数
总结
欢迎小白和我一起学习,按部就班投入时间,比平均水平多学100个小时,你就是top30%,比平均多学1000个小时,你就是top1%
3个月前,一道入门题中的入门题我都要半小时甚至一小时才做的出,经过最近一个月认真的刷题和算法学习,现在一道入门题可能就是3分钟(读题目+草稿纸模拟)+ 2分钟(敲代码)(ง •_•)ง
当然,现在只能快速做出70%的入门题,依旧有30%知识盲区,所以,补基础来了
比如判断两线段相交的叉乘,让你5分钟内敲出来,是否可以呢,还是冥思苦想 + 百度,足足20分钟才能做出来呢?
P1042 - 汉诺塔问题 - New Online Judge (ecustacm.cn)
题目
输入样例
2
输出样例
3
a->b
a->c
b->c
思路
1, hanoi()中的a, b, c参数要用char,因为输入的是字符
2, 最后先输出移动次数,再输出移动过程
3, n == 1表示这条柱子上就剩一个圆盘
4, void hanoi(int n, char a, char b, char c); 表示将柱子a上的n个圆盘, 移动到柱子c, b作为中转
难以理解的,可以在本子上模拟一下四个圆盘的情况
代码
#include
#include //printf()
using namespace std;
void hanoi(int n, char a, char b, char c)
{ //n == 1时, 最大的圆盘从a移到cif(n == 1) printf("%c->%c\n", a, c);else {hanoi(n - 1, a, c, b); //n-1个圆盘从a移到b, c是中转printf("%c->%c\n", a, c);hanoi(n - 1, b, a, c); //n-1个圆盘从b移到c, a是中转}
}
int main()
{int n, ans = 1;cin>>n;for(int i = 2; i <= n; ++i) ans = ans * 2 + 1;cout<
3
7
a->c
a->b
c->b
a->c
b->a
b->c
a->c
P1043 - 判断是否是闰年 - New Online Judge (ecustacm.cn)
#include
using namespace std;
int main()
{int year;while(cin>>year) {if(year%400 == 0 || (year%100 != 0 && year%4 == 0))cout<<"YES"<
P1044 - 大写变小写 - New Online Judge (ecustacm.cn)
题目
输入样例
A S D F G H J K L
123+@
a
输出样例
asdfghjkla
思路
重点在于
1,多次输入字符串
2,输入的字符串包含空格
C++输入包含空格的字符串,输出最大值出现次数_码龄11天的博客-CSDN博客
我们可以声明char数组,也可以声明string类,当然输入和求长度也会不一样
声明为string的话, (输入)string str; getline(cin, str); (求长度)str.length()
声明为char的话, (输入)char str[1000]; gets(str); (求长度)strlen(str)
头文件#include
代码
#include
#include //scanf(), gets()
#include //strlen()
using namespace std;
int main()
{char s[1000];string s2;while(gets(s)) {for(int i = 0; i < strlen(s); ++i) {if(s[i] >= 'a' && s[i] <= 'z')s2 += s[i];else if(s[i] >= 'A' && s[i] <= 'Z')s2 += s[i] + 'a' - 'A';}}cout<
A S D F G H J K L
123+@
a
^Z
asdfghjkla
考虑到连续输入,想结束时,只需先回车, 再(ctrl+Z), 再回车
(注意, ctrl+Z要位于行首)
P1045 - 译密码 - New Online Judge (ecustacm.cn)
题目
思路
在草稿纸一试,发现w,x,y,z及对应大写,ASCII值-22即可;其他字母ASCII值+4即可
同时需要注意char数组和String类的输入,求长度的区别
代码
下面我展示两种代码
1
#include
using namespace std;
int main()
{string s;while(cin>>s) {for(int i = 0; i < s.size(); ++i) {if((s[i]>='W'&&s[i]<='Z')||(s[i]>='w'&&s[i]<='z'))s[i] -= 22;else if((s[i]>='A'&&s[i]<='V')||(s[i]>='a'&&s[i]<='v'))s[i] += 4;}for(int i = 0; i < s.size(); ++i) cout<
2
#include
#include //strlen()
using namespace std;
int main()
{char s[510];while(cin>>s) {for(int i = 0; i < strlen(s); ++i) {if((s[i]>='W'&&s[i]<='Z')||(s[i]>='w'&&s[i]<='z'))s[i] -= 22;else if((s[i]>='A'&&s[i]<='V')||(s[i]>='a'&&s[i]<='v'))s[i] += 4;}for(int i = 0; i < strlen(s); ++i) cout<
P1046 - 反向数相加 - New Online Judge (ecustacm.cn)
题目
思路
1,嫌整型数组麻烦,直接对整型操作
2,看到题目第一时间想到,构造个函数起到f()的作用
3,考虑到,两个10^9反转再相加会超出int范围,我们用long long长整型声明
代码
有个小坑,构造反向的函数rever()时,第一次我声明int rever(),虽然其他整型全声明为long long,依旧Accepted 77%,第二次改long long rever()才Ac
#include
typedef long long LL;
using namespace std;
LL rever(LL x)
{LL y = 0;while(x) {y *= 10;y += x % 10;x /= 10;}return y;
}
int main()
{LL a, b, c;while(cin>>a>>b) {c = rever(rever(a) + rever(b));cout<
P1047 - Excel表中的列号 - New Online Judge (ecustacm.cn)
题目
思路
经过草稿纸详细模拟,一个字母26种可能,2个字母26*26种,3个字母26*26*26种,相加后 ==18278,验证正确
代码
#include
#include //strlen()
using namespace std;int main()
{char s[4];while(cin>>s) {int ans = 0;for(int i = 0; i < strlen(s); i++) {ans *= 26;ans += s[i] - 'A' + 1;}cout<
P1048 - 饮料兑换 - New Online Judge (ecustacm.cn)
入门题但通过率只有14%,涉及到简单的贪心,很多新手确实做不出来
题目
分析
1,注意10^18,必须用long long
2,ans(wer)声明在while(cin>>n>>k)里,否则会一直叠加
代码
#include
using namespace std;
int main()
{long long n, k;while(cin>>n>>k) {long long ans = 0; //声明在里面ans += n;while(n) {if(n >= k) {ans += n / k;n = n / k + n % k;}else break;}cout<
P1049 - 角谷猜想 - New Online Judge (ecustacm.cn)
题目
代码
#include
#include //printf()
using namespace std;
int main()
{int n, a;cin>>n;while(n != 1) {if(n % 2 == 1) {a = n;n = n * 3 + 1;printf("%d*3+1=%d\n", a, n);}else {a = n;n /= 2;printf("%d/2=%d\n", a, n);}}cout<<"End";return 0;
}
P1050 - 数字统计 - New Online Judge (ecustacm.cn)
题目
代码
#include
using namespace std;
int main()
{int L, R, x, ans = 0;cin>>L>>R>>x;for(int i = L; i <= R; ++i) {int j = i;while(j) {if(j % 10 == x) ans++;j /= 10;}}cout<
P1051 - 素数 - New Online Judge (ecustacm.cn)
这也是一道通过率14%的题目
题目
分析
1,直接从2遍历到n,可能会超限,易于发现遍历到平方根即可判断素数
2,注意flag的位置
3,换行不要漏
代码
#include
using namespace std;
int main()
{int n;while(cin>>n) {int flag = 1;if(n == 1) cout<<"No"<
经过10道题,我发现了
1,
过往的一点点比赛中,很多复杂点的题目,都是在入门题基础上演变来的,或者说有入门题的影子,因为入门题都没怎么做过以及比赛从未参加过,一开始就处于劣势,做不出来或者说做很久才Ac就不是没原因了
2,
相对于学习从未接触过的算法来说,刷入门题真的很放松,不会有冥思苦想,绞尽脑汁记公式的时候,不会有苦苦思索一道经典算法的情况,所以劳逸结合吧,比如最近学的Floodfill算法和水管工游戏,学一会就做点入门题放松下,不失为好办法
3,
刷入门题甚至只是回顾,不是没有效果的,比如你在个很简单的点上,卡了5分钟,这放在复杂点的大题里是致命的,一个大题里出现2~3个简单失误,意味着本来半小时能AC的题,足足花了一个小时才解决,甚至半途而废,所以,打牢基础