华为第三道题目

第三题怎么改都只有37.5,有大佬100AC的吗,要求的输出顺序到底是什么呀?大雾
全部评论
百分之75路过。。
点赞 回复 分享
发布于 2017-09-20 21:09
没用一题100AC  也是服了
点赞 回复 分享
发布于 2017-09-20 21:11
75%的路过。
点赞 回复 分享
发布于 2017-09-20 21:11
// 华为第三题.cpp : 定义控制台应用程序的入口点。// include "stdafx.h" include <iostream> include <string> include <vector> using namespace std; int main(){ string input; string src; string dest; cin >> input>>src>>dest; string mytemp; int num = 0; for (int i = 0; i < input.size(); i++) { if (input[i] == src[0]) { num++; mytemp.push_back('0'); } } string stard; for (int i = 0; i < num; i++) { stard.push_back('1'); } vector<string> res; for (int i = 0; i < pow(2, num); i++) { string restemp = input; int cnt = 0; for (int j = 0; j < input.size(); j++) { if (restemp[j] == src[0]) { if (src[0] > dest[0]) { if (mytemp[cnt] == '1') { restemp[j] = dest[0]; } } else { if (mytemp[cnt] == '0') { restemp[j] = dest[0]; } } cnt++; } } res.push_back(restemp); int k = 1; int jinwei = 1; while (jinwei == 1 && mytemp!=stard) { if (mytemp[num - k] == '0') { mytemp[num - k] = '1'; k++; jinwei = 0; } else if (mytemp[num - k] == '1') { mytemp[num - k] = '0'; jinwei = 1; k++; } } } for (int i = 0; i < res.size()-1; i++) { cout << res[i] <<','; } cout << res[res.size() - 1] << endl; cin.get(); cin.get(); return 0; }
点赞 回复 分享
发布于 2017-09-20 21:13
public class Main {     public static void main(String[] args) {         Scanner s = new Scanner(System.in);         String str = s.nextLine();         String[] strings = str.split(" ");                  TreeSet<String> ts = new TreeSet<String>(new Comparator<String>(){             @Override             public int compare(String o1, String o2) {                                  return o2.compareTo(o1);             }                      });         ts.add(strings[0]);                  new Test3().swap(ts,strings[0],strings[1],strings[2]);                           String last = ts.pollLast();         for (String string : ts) {             System.out.print(string+",");         }         System.out.println(last);                                    s.close();     }     public TreeSet<String> swap(TreeSet<String> ts,String str1,String str2,String str3){         for (int i = 0; i < str1.length(); i++) {             if(str2.equals(str1.charAt(i)+"")){                 StringBuilder sb = new StringBuilder(str1);                 String string = sb.replace(i, i+1, str3).toString();                 ts.add(string);                 swap(ts,string,str2,str3);             }         }         return ts;     } } 不知道对错啊,大神帮看一看
点赞 回复 分享
发布于 2017-09-20 21:18
#include <iostream> #include <cstring> #include <string> #include <cstdlib> #include <cstdio> #include <vector> #include <stack> #include <algorithm> #include <set> #include <queue>  #include <cctype> #include <cmath> #include <map> #include <unordered_map> #include <deque> #include <sstream> using namespace std; void replace(string str, const string &bitset, unordered_map<int, int> &record, char nc){     bool flag = false;     int idx = 1;     int i = bitset.size() - 1;     while(i >= 0) {         if(bitset[i] == '1') {             int strIdx = record[idx];             str[strIdx] = nc;             flag = true;         }         --i;         ++idx;     }     if(flag) {         cout << ',' << str;     } } bool nextBitSet(string &bitset) {     const int len = bitset.size();     if(bitset[len-1] == '0') {         bitset[len-1] = '1';         return true;     }     int curIdx = len-1;         while(curIdx >= 0) {         if(bitset[curIdx] == '0') {             bitset[curIdx] = '1';             break;         }         bitset[curIdx--] = '0';     }     if(curIdx < 0) return false;     return true; } int main() {          string str;     char oc, nc;     cin >> str >> oc >> nc;     oc = tolower(oc);     unordered_map<int, int> record;     const int len = str.size();     int idx = 1;     int maxIdx = -1;     bool found = false;     for(int i = len-1; i >= 0; --i) {         char ch = tolower(str[i]);         if(ch == oc) {             found = true;             record[idx++] = i;             maxIdx = (idx > maxIdx? idx : maxIdx);         }     }     if(found) {                 string bitset(maxIdx-1, '0');                 cout << str;         while(nextBitSet(bitset)) {             replace(str, bitset, record, nc);         }         cout << endl;             }else{         cout << str << endl;     }     return 0; } 100%通过
点赞 回复 分享
发布于 2017-09-20 21:19
75% +1. 检查半天看不出漏了哪种情况,题名还特意说明 没有非法输入。 最逗的是题目要求按升序,而例子是降序。
点赞 回复 分享
发布于 2017-09-20 21:22

相关推荐

程序员卤馆:加v细说
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务