Program that checks whether another number is contained within it
我最近才开始涉足编码,我遇到了一个我已经有好几天没能解决的问题,我在网上找到的最接近的东西是一个程序,它检查一个数字是否包含一个特定的数字,但我认为这在我的情况下并不适用。问题是让用户输入两个正数,并检查第二个数字的倒数是否包含在第一个数字中。例如,如果输入654321和345,它会说它包含它,因为345的反面是543,654321包含它。这是我一直在尝试的,但这是一场灾难。
P.S:变量在程序中应该保持整数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | #include <iostream> using namespace std; bool check(int longer, int shorter) { int i = 1; int rev=0; int digit; while (shorter > 0) { digit = shorter%10; rev = rev*10 + digit; shorter = shorter/10; } cout << rev << endl; bool win=0; int left = longer / 10; //54321 int right = longer % 10; // 65432 int middle = (longer /10)%10; // 5432 int middle1; int middle2; int trueorfalse = 0; while (left > 0 && right > 0 && middle1 > 0 && middle2 >0) { left = longer / 10; //4321 //321 right = longer % 10; //6543 //654 middle1 = middle%10; //543 middle2= middle/10; //432 if (rev == left || rev == right || rev == middle1 || rev == middle2 || rev == middle) { win = true; } else { win = false; } } return win; } int main () { int longer; int shorter; int winorno; cout <<"Please enter two numbers, first of which is longer:"; cin >> longer; cin >> shorter; winorno = check(longer,shorter); if (winorno==true) { cout <<"It works."; } else { cout <<"It doesn't work."; } return 0; } |
The more you overthink the plumbing, the easier it is to
stop up the drain. -- Scotty, Star Trek III.
如果将此任务分为两部分,这将变得更容易:
倒转整数中的数字。
在第二个整数中搜索由第一部分计算的反向整数。
对于第一部分,假设
1 2 3 4 5 6 7 8 | int modulo=1; int reversed_n=0; do { reversed_n = reversed_n * 10 + (n % 10); modulo *= 10; } while ( (n /= 10) != 0); |
最终结果是,如果
这种循环结构的原因是有意的。如果原来的数字是0,那么我们希望以
现在,我们可以用类似的方法来搜索第二个数字,叫做
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | for (;;) { if ((search % modulo) == reversed_n) { std::cout <<"Yes" << std::endl; return 0; } if (search < modulo) break; search /= 10; } std::cout <<"No" << std::endl; |
完整程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <iostream> int main() { int search=654321; int n=345; int modulo=1; int reversed_n=0; do { reversed_n = reversed_n * 10 + (n % 10); modulo *= 10; } while ( (n /= 10) != 0); for (;;) { if ((search % modulo) == reversed_n) { std::cout <<"Yes" << std::endl; return 0; } if (search < modulo) break; search /= 10; } std::cout <<"No" << std::endl; return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | #include <iostream> #include <cmath> using namespace std; int calculateNumLength(int num){ int length = 0; while (num > 0) { num = num / 10; length++; } return length; } bool check(int longer, int shorter){ int reversed = 0; int digit; int shortLength = calculateNumLength(shorter); int longLength = calculateNumLength(longer); int diffrence = longLength - shortLength; int possibleValues = diffrence + 1; int possibleNums[possibleValues]; while ( shorter > 0 ) { digit = shorter % 10; rev = ( rev * 10 ) + digit; shorter = shorter / 10; } int backstrip = pow(10, diffrence); int frontstrip = pow(10, longLength-1); int arrayCounter = 0; while ( longer > 0 ){ possibleNums[arrayCounter++] = longer/backstrip; if ( backstrip >= 10 ){ backstrip = backstrip / 10; }else{ break; } longer = longer % frontstrip; frontstrip = frontstrip / 10; } for (int i=0;i<possibleValues;i++){ if (possibleNums[i] == rev ){ return true; } } return false; } int main() { std::cout << check(654321,123) << std::endl; return 0; } |