首页 > 代码库 > PAT 1023 Have Fun with Numbers
PAT 1023 Have Fun with Numbers
#include <cstdio>#include <cstdlib>#include <vector>using namespace std;bool double_num(vector<char> &num) { int carry = 0; int i = num.size() - 1; while (i>=0) { int d = num[i] * 2 + carry; carry = d / 10; num[i]= d % 10; i--; } return carry < 1;}void stat_count(vector<char>& num, int* cnt) { for (int i=num.size() - 1; i>=0; i--) { cnt[num[i]]++; }}bool is_same_digits(vector<char>& num, int* cnt) { int cur_cnt[10] = {0}; stat_count(num, cur_cnt); for (int i=0; i<10; i++) { if (cnt[i] != cur_cnt[i]) return false; } return true;}void print(vector<char> &num) { int len = num.size(); for (int i=0; i<len; i++) { printf("%d", num[i]); } printf("\n");}int main() { const int MAX_DIGITS = 20; vector<char> num; char buf[MAX_DIGITS + 1]; int count[10] = {0}; scanf("%s", buf); for (int i=0; buf[i] != ‘\0‘; i++) { if (num.empty() && buf[i] == ‘0‘) continue; // skip leading zeros num.push_back(buf[i] - ‘0‘); } if (num.empty()) num.push_back(0); // stat original number digit count stat_count(num, count); bool found = false; bool no_carry = double_num(num); bool is_same = is_same_digits(num, count); if (is_same) { printf("Yes\n"); } else { printf("No\n"); } if (!no_carry) printf("1"); print(num); return 0;}
我觉得玩这种输出陷阱很没意思,给个反向例子会死么
PAT 1023 Have Fun with Numbers
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。