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 算出下级销售总和

sql 算出下级销售总和_找出总和字符串相关推荐

  1. 典型的Top K算法_找出一个数组里面前K个最大数

    原文 典型的Top K算法_找出一个数组里面前K个最大数...或找出1亿个浮点数中最大的10000个...一个文本文件,找出前10个经常出现的词,但这次文件比较长,说是上亿行或十亿行,总之无法一次读入 ...

  2. python查找两个数组中相同的元素_找出两个数组的相同元素,最优算法?

    在做新旧接口交替过程中,遇到了老接口和新接口json数据有些不一致的情况,需要比较两个json对象,把相同的元素赋其中一个json对象中变量的值.而且其中一个json最后输出格式还需要改变下属性名,思 ...

  3. java数组出现次数最多的数_找出数组中出现次数最多的那个数——主元素问题...

    方法一:以空间换时间,可以定义一个计数数组int count[101],用来对数组中数字出现的次数进行计数(只能针对数组中数字的范围1~100),count数组中最大的元素对应的下标,即为出现次数最多 ...

  4. 查找txt中的中文字符_找出nginx请求日志中某个url请求总次数排名前十的ip地址...

    答案如下: # 利用linux自带命令:sort.awk.grep.head.uniq组合得出相应的答案 awk '{print $1}' | grep 'www.xxxx.com' access.l ...

  5. java文件出现字符串_找出三个文本文件中都出现的字符串,并输出到一个文本文件(菜鸟求救)...

    编写java程序 找出三个文本文件中都出现的字符串,并输出到一个文本文件 下面是三个文本文件,找出其中相同的字符串(用数组的方法) 第一个文件: EEF1A1 GAPDH LOC643334 SLC3 ...

  6. Linux Shell脚本专栏_找出占用CPU/内存过高的进程_05

    文章目录 找出占用CPU/内存过高的进程 1. 脚本 2. 运行脚本 3. 效果图 找出占用CPU/内存过高的进程 1. CPU 过高的进程2. 内存 过高的进程 1. 脚本 #/bin/bash e ...

  7. python进程占用cpu过高_找出占用cpu内存过高的进程

    找出占用cpu内存过高的进程 准备 //分析占用CPU最高的应用 [root@dy1 ~]# ps -eo user,pid,pcpu,pmem,args --sort=-pcpu |head -n ...

  8. 如何快速找出找出两个数组中的_找出JavaScript中两个数组之间的差异

    LeetCode今天面临的挑战是在数组中查找所有消失的数字. 蛮力 我们的输入包括一个缺少数字的实际数组.我们想将该数组与相同长度的数组进行比较,其中没有遗漏的数字.所以如果给定的话[4,3,2,7, ...

  9. python找出最小数_找出不除N的最小数

    COW,174个字节 oomMOOMMMmoOmoOmoOMMMmOomOoMoOMMMmoOmoOmoOMMMmOoMOOmoO MOomoOMoOmOoMOOmoOmoomoOMOOmOoMoOm ...

最新文章

  1. 聂聪:数据科学让我为城市规划注入创新价值 | 优秀毕业生专访
  2. POJ 1597 Function Run Fun
  3. mac pycharm打不开解决方法
  4. SAP移动类型详细说明
  5. VerilogHDL8位串行乘法器的分析(2)
  6. Python_大众点评网站数据爬虫
  7. 【5分钟 Paper】Reinforcement Learning with Deep Energy-Based Policies
  8. iso标准软件测试标准,软件质量-ISO9000标准+SQA简介
  9. 房友中介管理系统服务器地址,房友中介管理系统
  10. GOM引擎版本为什么玩家会自动掉线或闪退?
  11. php使用手册输出语句,php echo和print区别及语句用法是什么 - php完全自学手册 - php中文网手册...
  12. 电脑键盘灯光的调节方法
  13. zblog php 首页经常被篡改,浏览器首页经常被篡改,这样设置,自己也改不掉!...
  14. linux设置网卡开机自启,centos系统怎么设置网卡开机自启
  15. 概率编程——未来也可以这样预测
  16. Unity提供的消息推送机制
  17. 常见的几种服务器/客服端模型
  18. 收集了一些distinct性能相关的文章 希望有用
  19. Web学习笔记_01
  20. nginx和tomcat本地部署

热门文章

  1. 【工具使用】Xray与Burp联动--流量转发插件Passive Scan Client
  2. jpannel设置位置xy_实用的摄影技巧!10种常见摄影场景的单反相机设置技巧!
  3. 文字阴影-CSS Text-Shadow
  4. koa中上传文件到阿里云oss实现点击在线预览和下载
  5. css3制作广告栏效果的疑问?
  6. 纯css隐藏移动端滚动条解决方案(ios上流畅滑动)
  7. HDU 5025Saving Tang Monk BFS + 二进制枚举状态
  8. Sum of Even Numbers After Queries
  9. 并发编程-concurrent指南-阻塞队列BlockingQueue
  10. Python中生成器generator和迭代器Iterator的使用方法