[英]reverse and compare vector C++
Sorry for my awful English. 对不起,我的英语不好。 I tried to write the code doing this work: - Giving a number and try to count down to 1 as soon as possible by the way that i show you below: For example: giving 423 -> reverse all digit: 324 -> count down to 322 -> reverse:223 ->count down to 221 -> reverse: 122 -> 120 -> 21 (021) -> 12 -> countdown to 1. - My algorithm compare the first to the last digit of the number, if (first > last) I will reverse, if not I will count down until (first > last).
我试图编写代码来完成这项工作:-给出一个数字并尝试通过以下方式尽快递减至1:例如:给423->反转所有数字:324->递减计数到322->反转:223->向下计数到221->反转:122-> 120-> 21(021)-> 12->倒数到1。-我的算法将数字的第一位和最后一位相比较,如果(第一个>最后一个)我会反转,如果不是,我会倒数直到(第一个>最后一个)。 It worked well for any number like 121, 131, but not for 101. So I don't understand why?
它适用于121、131等任何数字,但不适用于101。所以我不明白为什么? This is my code:
这是我的代码:
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int nb_digit(int a)
{
return (int)floor(log10(a)) + 1;
}
int * separate(int a,int b)
{
int i=0;
static int digit[10];
while(b != 0)
{
b--;
digit[i] = a/(int)pow(10,b);
a = a-digit[i]*(int)pow(10,b);
i++;
}
return digit;
}
int main()
{
int a,b,i=0;
int *digit;
cout<<"a = ";
cin>>a;
b = nb_digit(a);
cout<<"a has "<<b<<" digit"<<endl;
digit = separate(a,b);
for(i=0;i<b;i++)
cout<<*(digit+i)<<" ";
cout <<endl;
std::vector<int> v (digit, digit + b );
/* reverse(v.begin(),v.end());
cout<<"reverse digit:"<<endl;
vector<int>::iterator it;
*/ vector<int>temp;
vector<int>::iterator it;
int count=9;
while(v.size()>1)
{
if( *(v.begin()) > *(v.end()-1) ) {
temp = v;
reverse(v.begin(),v.end());
if(v==temp) // check reverse not changed
(*(v.end()-1))--;
if(*(v.begin())==0)
v.erase(v.begin());
cout<<"new number, reverse:"<<endl;
for ( it= v.begin(); it != v.end(); ++it)
cout << *it<< ' ' ;
cout << endl;
}
else if ( *(v.begin()) == *(v.end()-1) ) {
if ( *(v.begin()+1) > *(v.end()-2) ) {
temp = v;
reverse(v.begin(),v.end());
if(v==temp) // check reverse not changed
(*(v.end()-1))--;
if(*(v.begin())==0)
v.erase(v.begin());
cout<<"new number, reverse:"<<endl;
for ( it= v.begin(); it != v.end(); ++it)
cout << *it<< ' ' ;
cout << endl;
}
else {
(*(v.end()-1))--;
cout<<"new number, not reverse:"<<endl;
for ( it= v.begin(); it != v.end(); ++it)
cout << *it<< ' ' ;
cout << endl;
}
}
else {
(*(v.end()-1))--;
cout<<"new number, not reverse:"<<endl;
for ( it= v.begin(); it != v.end(); ++it)
cout << *it<< ' ' ;
cout << endl;
}
count++;
}
cout << "count = "<<count<<endl;
cout << '\n';
return 0;
}
Thanks for explaining my answer! 感谢您解释我的答案!
There may be more, but there is a bug in your separate function. 可能还有更多,但单独的功能存在错误。 Try something like this:
尝试这样的事情:
int * separate(int a,int b)
{
static int digit[10];
while(b != 0)
{
b--;
digit[b] = a % 10; // processing lowest digit first and
// placing into array backward
a /= 10;
}
return digit;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.