CVTE729笔试C/C++编程交流

说明,下面的两个题的最初的思路及代码都是来自@Mr°骚年。写出来是为了给大家交流,小女子不才,报了大腿@Mr°骚年。特此说明。

第一道编程题是求无序字符数组的第n个最小未出现偶数

#include 
using namespace std;
unsigned int getMinEven(int *a, int len, int n)
{
    if (a==NULL)
    {
        return 0;
    }
    unsigned int target = n*2;
    for (int i=0; i<len; i++)
    {
        if (a[i]>0 && a[i]%2 == 0)
        {
            int k = a[i]/2;
            if (k<=n)
            {
                target = (++n)*2;
            }
        }
    }
    return target;
}
void main()
{
    int arr[4] = {4, -1, 1, 3};    
    cout<<getMinEven(arr, 4, 1)<<endl;
    system("pause");
    return;
}
#广州视源电子科技股份有限公司#
全部评论
两道题的代码显然都是错误的! 第一题没有考虑重复偶数的情况,举个例子,{-1, 2, 2, 3}, 4, 10。算出来是24,实际是22. 第二题s1 = "aba", s2 = "abb"。返回true
点赞 回复 分享
发布于 2017-07-29 23:37
class Solution { public: bool isMatched(const string s1, const string s2) { int len1 = s1.size(); int len2 = s2.size(); if (len1 == 0 && len2 == 0) return true; if (len1 != len2) return false; map<char, char> match; for (int i = 0; i < len1; i++) match[s1[i]] = '\0'; for (int i = 0; i < len1; i++) { if (match[s1[i]] == '\0') match[s1[i]] = s2[i]; else if (match[s1[i]] != s2[i]) return false; } return true; } void test() { cout << boolalpha << isMatched("loop", "feed") << endl; cout << boolalpha << isMatched("aba", "abb") << endl; } };
点赞 回复 分享
发布于 2017-07-30 12:03
第一题遍历数组用把正偶数往set里面插,去重。然后设result等于2然后查找set。result在set里面result加2,result不在里面n减1,当n等于0时result就是结果,n不等于0,result加2继续去set查找直到n等于0。不用set的话,就按上述result和n的更新思路不断查找数组,如果没读错题的话,思路应该是这样吧……。第二题应该先统计完在进行编码比较,比如look和food都是编码成1221。某大佬的事例则aba编码成212,abb编码成122,这题可以只通过比较统计结果来得出最终编码是不是一样
点赞 回复 分享
发布于 2017-07-30 01:22
第二道的代码应该是这样的,我之前想错了,后来想想应该这样才是正确的。 bool func(const string& s,const string& t) { if(s.empty() && t.empty()) return true; if(s.size() != t.size()) return false; int s_cnt[256]; int t_cnt[256]; for(int i=0;i<256;i++){ s_cnt[i] = t_cnt[i] = -1;  } for(int i=0;i<s.size();i++){ if(s_cnt[s[i]] != t_cnt[t[i]]){ return false; } s_cnt[s[i]] = t_cnt[t[i]] = i; } return true; }
点赞 回复 分享
发布于 2017-07-31 15:11
第一题,大神们给出建议 public static int find(int[] a,int n){ //Map<Integer, Integer> map=new HashMap<Integer, Integer>(); int count=0; Set<Integer> set=new HashSet<Integer>(); for(int i=0;i<a.length;i++){ set.add(a[i]); } for(int i=2;i<Integer.MAX_VALUE;i+=2){ if(set.contains(i)){ continue; }else{ count++; } if(count==n) return i; } return -1; }
点赞 回复 分享
发布于 2017-07-31 10:51
第一题和@沉思的橙子 说的一样,用set塞,去重。 第二题可以做一个编码的映射表,扫一遍就行。 if (s.size() != t.size()) return false; char dict[128] = {0}; for (int i = 0;i < s.size(); ++i) { if (dict[s[i]] == 0) dict[s[i]] = t[i]; else if (dict[[si]] != t[i]) return false; } return true;
点赞 回复 分享
发布于 2017-07-30 13:54
第一题答案,请指教: #include <iostream> using namespace std; unsigned int getMinEven(int *a, int len, int n) { int res, count; res = 0; count = 0; int tmp = 1; while(count != n) { bool flag = false; for(int i = 0; i < len; i++) { if(a[i] > 0 && a[i] == tmp * 2) { flag = true; break; } } if(flag != true) { res = tmp * 2; count++; } tmp++; } return (static_cast<unsigned int>(res)); } int main(int argc, const char * argv[]) { // insert code here... int arr[4] = {-1, 2, 2, 3}; cout << getMinEven(arr, 4, 10) << endl; return 0; }
点赞 回复 分享
发布于 2017-07-30 12:37
#include <iostream> #include <string> using namespace std; char find_char(string s, int index, char ch, string res) { for(int i = 0; i < index; i++) { if(s[i] == ch) { return res[i]; } } return (++res[res.size() - 1]); } bool isMatched(string s1, string s2) { if(s1.size() != s2.size()) { return false; } string res1, res2; res1.push_back('0'); for(int i = 1; i < s1.size(); i++) { res1.push_back(find_char(s1, i, s1[i], res1)); } res2.push_back('0'); for(int i = 1; i < s2.size(); i++) { res2.push_back(find_char(s2, i, s2[i], res2)); } if(res1 == res2) { return true; } else { return false; } } int main(int argc, const char * argv[]) { // insert code here... cout << isMatched("aba", "abb") << endl; return 0; }
点赞 回复 分享
发布于 2017-07-30 11:19
@Mr°骚年更新的第二题的代码。 /* 作者:Mr°骚年 链接:https://www.nowcoder.com/discuss/30398 来源:牛客网 */ #include <iostream> using namespace std; bool func(const string& s,const string& t) { if(s.empty() && t.empty()) return true; if(s.size() != t.size()) return false; int s_cnt[256]; int t_cnt[256]; for(int i=0;i<256;i++) { s_cnt[i] = t_cnt[i] = -1; } for(int i=0;i<s.size();i++) { if(s_cnt[s[i]] != t_cnt[t[i]]) { return false; } s_cnt[s[i]] = t_cnt[t[i]] = i; } return true; } void main() { cout<<func("book", "look")<<endl; cout<<func("aba", "abb")<<endl; system("pause"); return; }
点赞 回复 分享
发布于 2017-07-31 16:21
第一道题用计数排序的方法来做
点赞 回复 分享
发布于 2017-07-31 11:47
第二道题用求KMP的next数组做的,不知道可不可以
点赞 回复 分享
发布于 2017-07-31 11:41
我第二题前边和你一样,做了个256大小的散列表,之后把两个表排序,遍历,看是不是相同。。
点赞 回复 分享
发布于 2017-07-31 08:23
我第二题用了map
点赞 回复 分享
发布于 2017-07-29 23:41
我的第一个是树的最小深度,还有一个题目怪怪的。。没读懂
点赞 回复 分享
发布于 2017-07-29 23:16
本题的答案是参考Mr。骚年的代码
点赞 回复 分享
发布于 2017-07-29 23:08
你第一题程序是什么意思,没看懂。
点赞 回复 分享
发布于 2017-07-29 22:28
第二道编程题是,判断两个字符串拼接形式是否相等 #include <iostream> using namespace std; bool func(const string &s, const string &t) { if (s.empty() && t.empty()) { return true; } if (s.size() != t.size()) { return false; } unsigned int s_count[256] = {0}; unsigned int t_count[256] = {0}; for (int i=0; i<s.size(); i++) { ++s_count[s[i]]; ++t_count[t[i]]; if (s_count[s[i]] != t_count[t[i]]) { return false; } } return true; } void main() { string s = "book"; string t = "look"; cout<<func(s , t)<<endl; system("pause"); return; }
点赞 回复 分享
发布于 2017-07-29 22:01
是要全部写出来?
点赞 回复 分享
发布于 2017-07-29 21:54
我去 我就写了个函数
点赞 回复 分享
发布于 2017-07-29 21:54

相关推荐

05-08 14:40
广东白云学院
各位大佬方狠狠批评吧
凌offer:投私企,政治面貌不是党员或者预备党员就不要写了,什么族感觉也没啥要写的必要
点赞 评论 收藏
分享
评论
点赞
19
分享

创作者周榜

更多
牛客网
牛客企业服务