牛客网_PAT乙级_1019. 数字黑洞 (20)
總結(jié)
注意!!
想要在函數(shù)中結(jié)束程序,一定不要用system("pause");,因為這樣只是讓程序暫停,結(jié)果就是運行時間超時,無法通過所有的測試用例。
那么,C++ 程序中什么函數(shù)可以立刻結(jié)束程序?
以下函數(shù)都是可以的。本題中使用了exit(0);
exit(0);ExitProcess(0);ExitThread(0);TerminateProcess(hProcess,0);關(guān)于用cmd打印輸出
ttt.exe > ttt.log把程序生成的exe文件保存到任意一個自定義目錄下,右鍵查看屬性,可看到絕對路徑
打開cmd,用cd..命令回到C目錄
把exe文件的絕對路徑粘貼進去,后面加上ttt.exe > ttt.log,這樣就可以將輸出結(jié)果保存為txt文件了
保存路徑就是粘貼之前的路徑(大概在C盤的最外層),找不到的話用everything搜索一下
應(yīng)該還有更方便的方法,目前知道的只有這些了
題目
來源:牛客網(wǎng)
給定任一個各位數(shù)字不完全相同的4位正整數(shù),如果我們先把4個數(shù)字按非遞增排序,再按非遞減排序,然后用第1個數(shù)字減第2個數(shù)字,將得到一個新的數(shù)字。一直重復(fù)這樣做,我們很快會停在有“數(shù)字黑洞”之稱的6174,這個神奇的數(shù)字也叫Kaprekar常數(shù)。
例如,我們從6767開始,將得到
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
… …
現(xiàn)給定任意4位正整數(shù),請編寫程序演示到達黑洞的過程。
輸入描述:
輸入給出一個(0, 10000)區(qū)間內(nèi)的正整數(shù)N。
輸出描述:
如果N的4位數(shù)字全相等,則在一行內(nèi)輸出“N - N = 0000”;否則將計算的每一步在一行內(nèi)輸出,直到6174作為差出現(xiàn),輸出格式見樣例,每行中間沒有空行。注意每個數(shù)字按4位數(shù)格
式輸出。
示例1
輸入
6767
輸出
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
代碼
#include<iostream> #define SWAP(a,b) {int tmp;tmp=a;a=b;b=tmp;} class Four_num { public:int before;int a[4] = { 0 };int after_12;int after_21;void part(int num_bf);//拆開int sort_12(int *p);//排序,返回排好的四位整數(shù)int sort_21(int *p);int cha = 0;Four_num(int cha);//構(gòu)造器void print(Four_num *num);void change(Four_num *num); }; //構(gòu)造器 Four_num::Four_num(int cha) {this->before = cha;part(before);after_12 = sort_12(&a[0]);after_21 = sort_21(&a[0]);this->cha = after_21 - after_12;this->print(this); } void change(Four_num *num) {num->before = num->cha;num->part(num->before);num->after_12 = num->sort_12(&num->a[0]);num->after_21 = num->sort_21(&num->a[0]);num->cha = num->after_21 - num->after_12;num->print(num); } void Four_num::part(int num_bf)//分割 {int i;for (i = 0; num_bf != 0; i++){a[i] = num_bf % 10;num_bf /= 10;} }int Four_num::sort_12(int *p)//ret最小 {int i, j;int ret = 0;for (i = 0; i < 4; i++){for (j = 0; i + j < 3; j++){if (a[j] < a[j + 1]){SWAP(a[j], a[j + 1]);}}ret *= 10;ret = ret + a[3 - i];}return ret; }int Four_num::sort_21(int *p)//ret最大 {int i, j;int ret = 0;for (i = 0; i < 4; i++){for (j = 0; j < 3; j++){if (a[j] > a[j + 1]){SWAP(a[j], a[j + 1]);}}ret *= 10;ret = ret + a[3 - i];}return ret; } void Four_num::print(Four_num *num) {if (num->after_21 == 0){std::cout << "0000";exit(0);}else if (num->after_21 < 10){std::cout << "000";}else if (num->after_21 < 100){std::cout << "00";}else if (num->after_21 < 1000){std::cout << "0";}std::cout << num->after_21;std::cout << " - ";if (num->after_12 == 0){std::cout << "0000";exit(0);}else if (num->after_12 < 10){std::cout << "000";}else if (num->after_12 < 100){std::cout << "00";}else if (num->after_12 < 1000){std::cout << "0";}std::cout << num->after_12;std::cout << " = ";if (num->cha == 0){std::cout << "0000";exit(0);}else if (num->cha < 10){std::cout << "000";}else if (num->cha < 100){std::cout << "00";}else if (num->cha < 1000){std::cout << "0";}std::cout << num->cha;std::cout << std::endl; } int main() {int cha;std::cin >> cha;Four_num num(cha);for (; num.cha != 6174;){change(&num);} }總結(jié)
以上是生活随笔為你收集整理的牛客网_PAT乙级_1019. 数字黑洞 (20)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++ 贪吃蛇 小游戏
- 下一篇: 牛客网_PAT乙级_1010月饼 (25