拼多多春招求解

拼多多春招第一题,加法转乘法:(求问,这到底哪里有问题?本地测试通过,提交case通过率为0???)
输入:一行string,均为加法与整数
输出:第一行,转换后的乘法式;第二行,算数结果
代码:#include<iostream>
#include<string>
using namespace std;

int exist(int a[], int size, int num){
    for (int i = 0; i < size; i++){
        if (a[i] == num)
            return i;
    }
    return -1;
}

void sort(int* a, int* c, int size){
    for (int i = 1; i < size; i++){
        int tempa = 0;
        int tempc = 0;
        if (a[i] < a[i - 1]){
            tempa = a[i];
            tempc = c[i];
            for (int j = i - 1; j >= 0; j--){
                if (tempa < a[j]){
                    a[j + 1] = a[j];
                    c[j + 1] = c[j];
                    if (j == 0){
                        a[0] = tempa;
                        c[0] = tempc;
                    }
                }
                else{
                    a[j + 1] = tempa;
                    c[j + 1] = tempc;
                }
            }
        }
    }
}

int main(){
    string str = "";
    int a[5000];
    int c[5000];
    cin >> str;
    int cnt = 0, num = 0, top = 0;
    while (cnt < str.size()){
        if (str[cnt] != '+'){
            num = num * 10 + str[cnt]-'0';
        }
        else{
            int index = exist(a, top, num);
            if (index != -1){
                c[index] ++;
                num = 0;
            }
            else{
                a[top] = num;
                c[top] = 1;
                top++;
                num = 0;
            }
        }
        cnt++;
    }
    int index = exist(a, top, num);
    if (index != -1){
        c[index] ++;
    }
    else{
        a[top] = num;
        c[top] = 1;
        top++;
    }
    sort(a, c, top);
    int res = 0;
    for (int i = 0; i < top-1; i++){
        cout << a[i] <<'*'<<c[i]<< '+';
        res += a[i] * c[i];
    }
    cout << a[top - 1]<< '*'<<c[top - 1] << endl;
    res += a[top - 1] * c[top - 1];
    cout << res;
    return 0;
}

#笔试题目##春招#
全部评论
第一题 做出来了,第二题通过了 60%,三 四 不会
点赞 回复
分享
发布于 2018-03-20 21:24
发帖真快。。我是第三题改好了然而时间到了,简直了
点赞 回复
分享
发布于 2018-03-20 21:07
联易融
校招火热招聘中
官网直投
同类型第一题,通过率只有93.33%。就是不知道剩下的为什么不通过。。。
点赞 回复
分享
发布于 2018-03-20 21:08
没带手机出门,结果7点回宿舍才看到笔试通知,结果晚了20分钟。然后我居然不知道可以在本地写代码,各种不习惯,于是写完第一题已经过8点了。
点赞 回复
分享
发布于 2018-03-20 21:17
我的通过测试了(供参考 #include<cstdio> #include<algorithm> #include<iostream> #include<sstream> #include<cstring> using namespace std; const int n = 10000; int hash_table[n] = {0}; int main() {     string s;     getline(cin,s);     s+='+';     string temp="";     for(int i=0;i<s.length();i++){         if(s[i] != '+'){             temp += s[i];             continue;         }         else{             int int_temp;             stringstream stream(temp);             stream>>int_temp;             //cout << int_temp << "debug" << endl;             hash_table[int_temp]++;             temp="";         }     }     int ans=0;     string str_ans="";     int k =0;     for(int i=0;i<n;i++){         if(hash_table[i] >= 1) {                 if(k>=1) str_ans+='+';                 stringstream ss;                 ss << i;                 str_ans+=ss.str();                 str_ans+= '*' ;                 stringstream ss2;                 ss2 << hash_table[i];                 str_ans+= ss2.str();                 k++;                 ans += hash_table[i] * i;         }     }     cout << str_ans << endl;     cout << ans <<endl;     return 0; }
点赞 回复
分享
发布于 2018-03-20 21:17
确实各种不习惯。。第一题写到了8点多,0case通过,改了很久,依旧不过,有哪些大佬帮我看看吗?第一题也没有什么临界值呀
点赞 回复
分享
发布于 2018-03-20 21:19
import java.util.Scanner; public class Main {     public static void main(String[] args) {         // TODO Auto-generated method stub         Scanner sc = new Scanner(System.in);         while(sc.hasNext()) {             int count=0;             String str=sc.nextLine();             String newStr="";             int []math=new int[10001];             for(int i=0;i<10001;i++) {                 math[i]=0;             }             String []arr=str.split("\\+");             for(int i=0;i<arr.length;i++) {                 math[Integer.parseInt(arr[i])]++;             }             for(int i=0;i<10001;i++) {                 if(math[i]!=0)                 {                     count+=i*math[i];                     newStr=newStr+String.valueOf(i)+"*"+String.valueOf(math[i])+"+";                 }                               }             newStr=newStr.substring(0, newStr.length()-1);             System.out.println(newStr);             System.out.println(count);         }     } } 
点赞 回复
分享
发布于 2018-03-20 21:24
我只有第一题是全过的,用TreeMap存出现的数字和次数,这样直接就有序了,然后就是计算了。 import java.util.Map; import java.util.Scanner; import java.util.TreeMap; public class ChengFaZhuanHuan { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Map<Integer,Integer> map = new TreeMap<>(); while(sc.hasNext()){ String str = sc.next(); str += '+'; int start = 0; for(int i=0;i<str.length();++i){ if(str.charAt(i)=='+'){ int key = Integer.valueOf(str.substring(start, i)); if(map.containsKey(key)){ int value = map.get(key); map.put(key, value+1); }else{ map.put(key, 1); } start = i+1; } } StringBuilder sb = new StringBuilder(); int res = 0; for(Integer k :map.keySet()){ int key = k; int value = map.get(key); sb.append(key); sb.append('*'); sb.append(value); sb.append('+'); res += key*value; } sb.deleteCharAt(sb.length()-1); System.out.println(sb.toString()); System.out.println(res); } } }
点赞 回复
分享
发布于 2018-03-20 21:46
第二题手动求解。。。感觉很无趣啊。 def f(seq):     n = len(seq)     cnt = 0     seq = sorted(seq)[::-1]     d = {}     for i in seq:         if i not in d:             d[i] = 1         else:             d[i] += 1     print(d)     if 4 in d:         cnt = d[4]     if 3 in d:         cnt += d[3]         print(cnt)     if 2 in d:         cnt += d[2] // 2         left2 = d[2] % 2     if 1 in d:         d[1] -= min(d[1], d[3])         if (d[1] + left2) % 4  == 0:             cnt += (d[1] + left2) // 4          else:             cnt += (d[1] + left2) // 4 +1     return cnt
点赞 回复
分享
发布于 2018-03-20 21:59
第四题动态规划,从前到后填一个数组 longcute。当最后一个数不保留的时候 longcute[n] = longcute[n-1], 最后一个数保留的时候,要从后往前找到最近的两个元素求和等于 seq[n], 其中离 n 较近的元素位置为 end, 然后 longcute[n] = longcute[end] + 1。 找到第一个长度为3 的最长可爱数组的时候 longcute 里面放1,所以最后要全部+2 def cuteseq(seq):     longcute = [0] * len(seq)     for i in range(2, len(seq)):         end = 0         flag = False         for j in reversed(range(i)):             for k in reversed(range(j)):                 if (seq[k] + seq[j]) == seq[i]:                     flag = True                     end = j                     break             if flag:                 break         if flag:             longcute[i] = max(longcute[i-1], longcute[end]+1)         else:             longcute[i] = max(longcute[i-1], 0)     return longcute[-1]+2
点赞 回复
分享
发布于 2018-03-20 22:04
你们都投的算法岗么?
点赞 回复
分享
发布于 2018-03-21 09:59

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务