c语言中的小小白-CSDN博客c语言中的小小白关注算法,c++,c语言,贪心算法,链表,mysql,动态规划,后端,线性回归,数据结构,排序算法领域.https://blog.csdn.net/bhbcdxb123?spm=1001.2014.3001.5343
给大家分享一句我很喜欢我话:
知不足而奋进,望远山而前行!!!
铁铁们,成功的路上必然是孤独且艰难的,但是我们不可以放弃,远山就在前方,但我们能力仍然不足,所有我们更要奋进前行!!!
今天我们更新了xxxxxxx内容,
🎉 欢迎大家关注🔍点赞👍收藏⭐️留言📝
一、题目一
这张图片是前几年微博上很火的一张图片,下面我们来看一下题目:
这里便是题目的描述和要求:下面我们来看一下这道题的代码:
#include<iostream>
#include<set>
#include<cstring>;
using namespace std;int main()
{char tele[20];cin >> tele;set<int, greater<int>> numbers;int len = strlen(tele);for (int i = 0; i < len; i++){numbers.insert(tele[i] - '0');}int len2 = numbers.size();int arr[12];for (int i = 0; i < len; i++){int count = 0;for (int j:numbers){if ((tele[i] - '0') == j){arr[i] = count;break;}count++;}}cout << "int[] arr = new int[]{";int a = 0;for (int i : numbers){if (a != 0)cout << "," << i;else cout << i;a++;}cout << "};"<<endl;cout << "int[] index = new int[]{";for (int i = 0; i < len; i++){if (i != 0)cout << "," << arr[i];else cout << arr[i];}cout << "};";return 0;
}
二、题目二
再来看一下这道题,看似是简单的A+B,实则不然,输入的数不一定是整数,可能是字母,或者其他的什么东西,所以看一下代码吧:
#include<iostream>
#include<cstring>int is_integer(const char* ptr)
{int len = strlen(ptr);int all = 0;if (len > 4)return -1;else {for (int i = 0; i < len; i++){if (ptr[i] >= '0' && ptr[i] <= '9'){all *= 10;all += (ptr[i] - '0');}else {return -1; }}}if (all < 1 || all>1000)return -1;else return all;}int main()
{char arr[100] = { 0 };char str[100] = { 0 };std::cin >> arr;std::cin.ignore();std::cin.getline(str, 100);int ret1 = is_integer(arr);int ret2 = is_integer(str);if (ret1 == -1 && ret2 == -1){std::cout << "? + ? = ?";}else if (ret1 == -1 && ret2 != -1){std::cout << "? + " << ret2 << " = ?";}else if (ret1 != -1 && ret2 == -1){std::cout << ret1 << " + ? = ?";}else if (ret1 != -1 && ret2 != -1){std::cout << ret1 << " + " << ret2 << " = " << ret1 + ret2;}return 0;
}
三、题目三
下面是代码:
#include<iostream>
#include<set>using namespace std;
int main()
{ int n=0; cin>>n; set<int> no_handsome; for(int i=0;i<n;i++) { int k=0; cin>>k; for(int j=0;j<k;j++) { int temp=0; cin>>temp; if(k>1) { no_handsome.insert(temp); } } } int m=0; cin>>m; set<int> handsome; bool flag=true; for(int i=0;i<m;i++) { int temp; cin>>temp; if(no_handsome.find(temp)==no_handsome.end()) { if(handsome.find(temp)==handsome.end()) { if(!flag) { cout<<" "; } printf("%05d",temp); flag=false; } handsome.insert(temp); } } if(handsome.size()==0) { cout<<"No one is handsome"; }
}
总结:
这三个题整体难度其实不大,但是很容易做错,所以各位好好看一下吧