sql 算出下級銷售總和_找出總和字符串

sql 算出下級銷售總和

Description:

描述:

This is a standard interview problem to check that the given string is a sum string or not using backtracking.

這是一個標準的面試問題,用于檢查給定的字符串是否為總和字符串或不使用回溯。

Problem statement:

問題陳述:

There is a string given to you. You have to find out whether the given string is a sum string or not. For a string "12345168213" if it goes like this, "123" + "45" = "168" "45" + "168" = "213" then it is a sum string otherwise not.

有一個字符串給你。 您必須找出給定的字符串是否為求和字符串。 對于字符串“ 12345168213”,如果這樣, “ 123” + “ 45” = “ 168” “ 45” + “ 168” = “ 213” ,則為求和字符串,否則為非。

    Input:
Test case T
T no. of strings we have to check. 
E.g.
3
"12345168213"
"123581321"
"15643"
Output:
If the string is a sum-string then you have to print 
"It is a sum string"
and, if it is not then you have to print 
"It is not a sum string".

Example

    T=3
Str= "12345168213"
"123" + "45" = "168"
"45" + "168" = "213"
It is a sum string
Str= "123581321" 
"1" + "2" = "3"
"2" + "3" = "5"
"3" + "5" = "8"
"5" + "8" = "13"
"8" + "13" = "21"
It is a sum string
Str= "15643"
"1" + "5" = "6"
"5" + "6" = "11"
It is not a sum string

Explanation with example

舉例說明

To find out the actual length of the first two substrings is a problem of combination. We will solve the problem using the backtracking process. If we break the problem into sub-problems like,

找出前兩個子串的實際長度是組合的問題。 我們將使用回溯過程解決問題。 如果我們將問題分解為子問題,

  1. Find out the length of the first sub-string.

    找出第一個子字符串的長度。

  2. Find out the length of the second sub-string.

    找出第二個子字符串的長度。

  3. Add the two strings.

    添加兩個字符串。

  4. Check if the summation is the same as the next substring.

    檢查求和是否與下一個子字符串相同。

  5. If yes, we will continue the process otherwise the string is not a sum string.

    如果是,我們將繼續執行該過程,否則該字符串不是求和字符串。

    str.substring(i,j) + str.substring(j+1,k) = str.substring(k+1,l)
str.substring(j+1,k) + str.substring(k+1,l) = str.substring(l+1,m)

Where, i, j, k, l, m is the position index on the substring of the string.

其中, i , j , k , l , m是字符串的子字符串的位置索引。

For the string "12345168213"

對于字符串“ 12345168213”

To find out the length of the first substring we will look for the all possible combination of the sub-string of the string from the starting index.

為了找出第一個子字符串的長度,我們將從起始索引中查找該字符串的子字符串的所有可能組合。

"1" , "12" , "123" , "1234" , "12345" , "123451" etc.

“ 1”“ 12”“ 123”“ 1234”“ 12345”“ 123451”

To find out the length of the second substring we will look for all possible combinations of the sub-string of the string from the last index of the first substring.

為了找出第二個子字符串的長度,我們將從第一個子字符串的最后一個索引中查找該字符串的子字符串的所有可能組合。

Let the first index= "123" then the all possible combinations are "4", "45", "451", "4516" etc.

假設第一個索引= “ 123”,那么所有可能的組合都是“ 4”“ 45”“ 451”“ 4516”等。

After calculating the summation of the two sub-strings will also find out the length of the result and take the next sub-string who has a length equal to that length.

在計算完兩個子字符串的總和后,還將找出結果的長度,并獲取下一個長度等于該長度的子字符串。

If the two substrings are "123" and "45" after calculating the summation the result "168" we will calculate its length which is equal to 3 and take the next sub-string e.g. "168"

如果兩個子字符串分別是“ 123”“ 45” ,計算出結果“ 168”后,我們將計算其長度等于3,并取下一個子字符串,例如“ 168”

Both the resultant and the sub-string are the same therefore we will add up "45" and "168" and check with "213".

結果和子字符串都相同,因此我們將“ 45”“ 168”加起來并用“ 213”進行校驗。

C++ implementation:

C ++實現:

#include <bits/stdc++.h>
using namespace std;
//adding the numbers
string add(string str1, string str2)
{
int len1 = str1.length();
int len2 = str2.length();
int i = 1;
int carry = 0;
string str = "";
while ((len1 - i) >= 0 && (len2 - i) >= 0) {
int result = (str1[len1 - i] - '0') + (str2[len2 - i] - '0') + carry;
char ch = (result % 10) + '0';
str = ch + str;
carry = result / 10;
i++;
}
while ((len1 - i) >= 0) {
int result = (str1[len1 - i] - '0') + carry;
char ch = (result % 10) + '0';
str = ch + str;
carry = result / 10;
i++;
}
while ((len2 - i) >= 0) {
int result = (str2[len2 - i] - '0') + carry;
char ch = (result % 10) + '0';
str = ch + str;
carry = result / 10;
i++;
}
if (carry > 0) {
char ch = carry + '0';
str = ch + str;
}
return str;
}
bool checksumUtill(string str, int pos, int str1_len, int str2_len)
{
int n = str.length();
int i = str1_len, j = str2_len;
//if the total sum of the current position and
//both the strings is greater than total length
if (pos + str1_len + str2_len >= n)
return false;
//calculate the sum
string s = add(str.substr(pos, i), str.substr(pos + i, j));
//if the next substring of pos+i+j is equals to the sum
if (s == str.substr(pos + i + j, s.length())) {
if (pos + i + j + s.length() == n)
return true;
return checksumUtill(str, pos + i, j, s.length());
}
return false;
}
bool check_sum_string(string str)
{
if (str.length() == 0)
return false;
int str1_len = 1;
int str2_len = 1;
int pos = 0;
//go for all the combinations
for (int i = 1; i < str.length(); i++) {
for (int j = 1; j < str.length(); j++) {
if (checksumUtill(str, pos, i, j)) {
return true;
}
}
}
return false;
}
int main()
{
int t;
cout << "Test Case : ";
cin >> t;
while (t--) {
string str;
cout << "Enter the String : ";
cin >> str;
if (check_sum_string(str)) {
cout << "It is a sum string\n";
}
else {
cout << "It is not a sum string\n";
}
}
return 0;
}

Output

輸出量

Test Case : 3
Enter the String : 12345168213
It is a sum string
Enter the String : 123581321
It is a sum string
Enter the String : 15648
It is not a sum string

翻譯自: https://www.includehelp.com/icp/find-out-the-sum-string.aspx

sql 算出下級銷售總和

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377201.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377201.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377201.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Request 分別獲取具有相同 name 屬性表單元素值

html 中是允許多個具有相同name屬性的元素的&#xff0c;例如 <div> <input name"txtName" id"txtFirstName" type"text" /> <input name"txtName" id"txtMiddleName" type"text" /> <input…

《MySQL——redo log 與 binlog 寫入機制》

目錄binlog寫入機制redo log寫入機制組提交機制實現大量的TPS理解WAL機制如何提升IO性能瓶頸WAL機制告訴我們&#xff1a;只要redo log與binlog保證持久化到磁盤里&#xff0c;就能確保MySQL異常重啟后&#xff0c;數據可以恢復。 下面主要記錄一下MySQL寫入binlog和redo log的…

BBIAB的完整形式是什么?

BBIAB&#xff1a;再回來一點 (BBIAB: Be Back In A Bit) BBIAB is an abbreviation of "Be Back In A Bit". BBIAB是“ Be Back in A Bit”的縮寫 。 It is an expression, which is commonly used in messaging or chatting on social media networking sites lik…

字符串:KMP Eentend-Kmp 自動機 trie圖 trie樹 后綴樹 后綴數組

涉及到字符串的問題&#xff0c;無外乎這樣一些算法和數據結構&#xff1a;自動機 KMP算法 Extend-KMP 后綴樹 后綴數組 trie樹 trie圖及其應用。當然這些都是比較高級的數據結構和算法&#xff0c;而這里面最常用和最熟悉的大概是kmp&#xff0c;即使如此還是有相當一部分人也…

WPF CanExecuteChanged

繼承ICommand ,RelayCommand命令 1 public class RelayCommand : ICommand2 {3 private readonly Action _execute;4 private readonly Func<bool> _canExecute;5 public event EventHandler CanExecuteChanged;6 public RelayComma…

《MySQL——主備一致性六問六答》

目錄備庫為什么要設置為只讀模式&#xff1f;備庫設置為只讀&#xff0c;如何與主庫保持同步更新&#xff1f;A到B的內部流程如何&#xff1f;binlog內容是什么&#xff1f;row格式對于恢復數據有何好處M-M結構的循環復制問題以及解決方案備庫為什么要設置為只讀模式&#xff1…

代碼管理工具

http://blogs.msdn.com/b/visualstudio/archive/2012/06/11/world-of-samples-at-your-fingertips.aspx轉載于:https://www.cnblogs.com/hebeiDGL/archive/2012/09/25/2700961.html

fyi 在郵件里是什么意思_FYI的完整形式是什么?

fyi 在郵件里是什么意思僅供參考&#xff1a;供您參考 (FYI: For Your Information) FYI is an acronym of "For Your Information". It is a widespread internet slang used these days in text messaging, instant messaging, and chatting on Facebook, WhatsApp…

Hyper-V 替換 vmwp

要激活 Hyper-V 下的虛機 最簡單的方法是用帶證書的vmwp替換掉原來的 帶證書的vmwp參見&#xff1a;http://bbs.pcbeta.com/viewthread-1408240-1-1.html 下載后腰替換 先把 Hyper-V 的倆服務停止掉 然后找到 C:\Windows\System32\vmwp.exe 右鍵--安全 替換掉所有者 然后給自己…

《MySQL——主備切換流程與主備延遲》

目錄主備切換主備延遲的原因可靠性優先策略的主備切換流程可用性優先策略的主備切換流程主備切換 主備切換分為主動運維與被動操作。 軟件升級、主庫所在機器按計劃下線為主動運維。 主庫所在機器掉電為被動操作。 同步延遲 1、主庫A執行完一個事務&#xff0c;寫入binlog…

ejb模式_EJB的完整形式是什么?

ejb模式EJB&#xff1a;企業Java Bean (EJB: Enterprise Java Bean) EJB is an abbreviation of Enterprise Java Bean. EJB is one of many Java application programming interfaces (API) for flexible and manageable structuring of Java Platform, Enterprise Edition (J…

Android之PreferenceActivity

http://www.cnblogs.com/wservices/archive/2010/07/08/1773449.html 看到很多書中都沒有對PreferenceActivity做介紹&#xff0c;而我正好又在項目中用到&#xff0c;所以就把自己的使用的在這總結一下&#xff0c;也方便日后查找。 PerferenceActivity是什么&#xff0c;看下…

淺談算法和數據結構: 七 二叉查找樹

前文介紹了符號表的兩種實現&#xff0c;無序鏈表和有序數組&#xff0c;無序鏈表在插入的時候具有較高的靈活性&#xff0c;而有序數組在查找時具有較高的效率&#xff0c;本文介紹的二叉查找樹(Binary Search Tree&#xff0c;BST)這一數據結構綜合了以上兩種數據結構的優點。…

scala部分應用函數_Scala中的部分函數

scala部分應用函數Scala部分功能 (Scala partial functions) A partial function is a function that returns values only for a specific set of values i.e. this function is not able to return values for some input values. This function is defined so that only som…

《MySQL——備庫多線程復制策略。》

目錄備庫并行復制能力MySQL5.6版本 并行復制策略MariaDB 并行復制策略MySQL5.7版本 并行復制策略MySQL5.7.22版本 并行復制策略總結備庫并行復制能力 主要涉及兩個方面的并行度&#xff1a; 1、客戶端寫入主庫的能力 2、備庫上sql_thread執行中轉日志relay log 1的并行能力…

人臉是門大生意

我們正處在一個新時代的入口。人有70%的能量是被大腦消耗&#xff0c;大腦90%的能量用來處理視覺信息&#xff0c;人臉則承載了絕大部分的視覺信息。我們要討論的是一個比Google Glass更酷的世界。文/程苓峰-云科技網易郵箱的用戶已經可以用人臉而不是密碼來驗證登陸。安卓4.0實…

【SQL】sql版Split函數。用于拆分字符串為單列表格

【SQL】sql版Split函數。用于拆分字符串為單列表格 功能與.net版string.Split函數類似&#xff0c;只不過.net返回的是數組&#xff0c;這個返回的是一個單列表格&#xff0c;每個拆分出來的子串占一行。可選是否移除空格子串和重復項。市面上類似的函數不算少&#xff0c;但大…

線描算法

線描算法 (Line drawing algorithms) The equation for a straight line is ymxb 直線方程為y mx b In this m represent a slope of a line which can be calculated by the my2-y1/x2-x1 where (x1, y1) are the starting position of the points and (x2, y2) are the end…

為移動端網頁構造快速響應按鈕

背景 在谷歌&#xff0c;我們不斷地推測手機網頁應用的可能性。像HTML5這樣的技術使我們網頁版的應用以及運行在手機設備上的原生應用。而這些技術的成就之一就是我們開發了一種新的創建按鈕的方法&#xff0c;使按鈕的響應時間遠遠快于一般的HTML按鈕。在此之前的按鈕或者其他…

Red Gate系列之一 SQL Compare 10.4.8.87 Edition 數據庫比較工具 完全破解+使用教程

Red Gate系列之一 SQL Compare 10.4.8.87 Edition 數據庫比較工具 完全破解使用教程 Red Gate系列文章&#xff1a; Red Gate系列之一 SQL Compare 10.4.8.87 Edition 數據庫比較工具 完全破解使用教程 Red Gate系列之二 SQL Source Control 3.0.13.4214 Edition 數據庫版本控制…