- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我需要制作一个程序,让用户输入一串数字。
然后它输出分组的数字,以便在将数字放在一起时每个分组都比最后一个大。
如果剩余数字的总和小于前一组,则它们将被忽略。
例如:314159265
会输出 3 14 15 92
此处:3 < 14 <15 < 92 但省略了 65,因为它小于 92
或者9876543210
会输出 9 87 654 3210
此处:9 < 87 < 654 < 3210
它必须对长度为 1-20 个字符的字符串执行 5 次。
我的代码适用于较短的字符串,即上面的字符串,但是当它们超过 12 个字符时,最终输出就会困惑。
例如:98765432101234567898
输出 9 87 654 3211 12333
而不是 9 81 654 3210 12345 67898
这里:9 < 87 < 654 < 3211 < 12333 它应该输出 9 < 87 < 654 < 3210 < 12345 < 67898
我不知道为什么它不适用于较大的字符串,我们将不胜感激任何帮助。
#include<iostream>
#include<iomanip>
#include<math.h>
#include<string>
#include<stdlib.h>
using namespace std;
void input (string &a,string num[20]){
string numfinal,temp;
cout<<"Enter the string of numbers: ";
getline(cin, a);
int length=a.length();
for(int i=0;i<length;i++){
num[i]=a.substr(i,1);
}
for(int r=0;r<length;r++){
int n=atoi(temp.c_str());
int o=atoi(num[r].c_str());
int p=temp.length();
if((length-r<=p)&&(o<n)){
}
else if((o>n)||(r==0)){
temp=num[r];
numfinal=numfinal+temp+" ";
}
else if((o<n)||(o=n)){
int w=n;
temp=num[r]+num[r+1];
n=atoi(temp.c_str());
if(n<w){
int a=1;
int q=r+2;
while(n<w){
temp=temp+num[q];
n=atoi(temp.c_str());
p++;
a++;
}
numfinal=numfinal+temp+" ";
r=r+a;
}
else{
numfinal=numfinal+temp+" ";
r++;
}
}
}
cout<<numfinal<<endl;
}
int main(){
string a;
string num[20];
for(int r=0;r<5;r++){
input(a,num);
}
return 0;
}
最佳答案
此代码有效。但不要忘记编写简单的代码并使用 C++ 编程的新风格。
#include <vector>
using namespace std;
vector<string> Input( )
{
string a;
cin >> a;
vector<string> num;
string current("-1");
string str;
for(auto c : a)
{
str.append(1, c);
if (stoi(str) > stoi(current) )
{
num.push_back(str);
current = str;
cout << str << " ";
str = "";
}
}
cout << endl;
return num;
}
int main() {
for (int i = 0; i<5; i++) {
vector<string> num;
num = Input();
}
return 0;
}
关于c++ - 从一串数字 C++ 创建一个数字序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42124177/
我正在尝试创建一个程序,其中字符串的前三个字符重复给定次数,如下所示: foo('Chocolate', 3) # => 'ChoChoCho' foo('Abc', 3) # => 'AbcAbcA
我有以下字符串: std::string str = "Mode:AAA:val:101:id:A1"; 我想分离一个位于 "val:" 和 ":id" 之间的子字符串,这是我的方法: std::st
DNA 字符串可以是任意长度,包含 5 个字母(A、T、G、C、N)的任意组合。 压缩包含 5 个字母(A、T、G、C、N)的 DNA 字母串的有效方法是什么?不是考虑每个字母表 3 位,我们可以使用
是否有一种使用 levenstein 距离将一个特定字符串与第二个较长字符串中的任何区域进行匹配的好方法? 例子: str1='aaaaa' str2='bbbbbbaabaabbbb' if str
使用 OAuth 并使用以下函数使用我们称为“foo”(实际上是 OAuth token )的字符串加密 key public function encrypt( $text ) { // a
我是一名优秀的程序员,十分优秀!