日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

牛客网_PAT乙级_1019. 数字黑洞 (20)

發布時間:2024/2/28 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 牛客网_PAT乙级_1019. 数字黑洞 (20) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

總結

注意!!
想要在函數中結束程序,一定不要用system("pause");,因為這樣只是讓程序暫停,結果就是運行時間超時,無法通過所有的測試用例。

那么,C++ 程序中什么函數可以立刻結束程序?

以下函數都是可以的。本題中使用了exit(0);

exit(0);ExitProcess(0);ExitThread(0);TerminateProcess(hProcess,0);

關于用cmd打印輸出

ttt.exe > ttt.log

把程序生成的exe文件保存到任意一個自定義目錄下,右鍵查看屬性,可看到絕對路徑
打開cmd,用cd..命令回到C目錄
把exe文件的絕對路徑粘貼進去,后面加上ttt.exe > ttt.log,這樣就可以將輸出結果保存為txt文件了
保存路徑就是粘貼之前的路徑(大概在C盤的最外層),找不到的話用everything搜索一下
應該還有更方便的方法,目前知道的只有這些了

題目

來源:牛客網

給定任一個各位數字不完全相同的4位正整數,如果我們先把4個數字按非遞增排序,再按非遞減排序,然后用第1個數字減第2個數字,將得到一個新的數字。一直重復這樣做,我們很快會停在有“數字黑洞”之稱的6174,這個神奇的數字也叫Kaprekar常數。

例如,我們從6767開始,將得到

7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
… …

現給定任意4位正整數,請編寫程序演示到達黑洞的過程。

輸入描述:
輸入給出一個(0, 10000)區間內的正整數N。

輸出描述:
如果N的4位數字全相等,則在一行內輸出“N - N = 0000”;否則將計算的每一步在一行內輸出,直到6174作為差出現,輸出格式見樣例,每行中間沒有空行。注意每個數字按4位數格
式輸出。

示例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);//排序,返回排好的四位整數int sort_21(int *p);int cha = 0;Four_num(int cha);//構造器void print(Four_num *num);void change(Four_num *num); }; //構造器 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);} }

總結

以上是生活随笔為你收集整理的牛客网_PAT乙级_1019. 数字黑洞 (20)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。