UVA Solution 263 - Number Chains - Solution in C++ | Volume 2
UVA Online Judge Solution 263 - Number Chains | Volume 2
UVA Problem Link - 263 - Number Chains https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=4&page=show_problem&problem=199
Problem Name: 263 - Number Chains
Problem Number : UVA - 263 - Number Chains
Online Judge : UVA Online Judge Solution
Volume: 2
Solution Language : C plus plus
UVA Solution 263 - Number Chains Code in CPP:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <map> using namespace std; int cmp1(const void *i, const void *j) { return *(char *)i - *(char *)j; } int cmp2(const void *i, const void *j) { return *(char *)j - *(char *)i; } int main() { int n; while(scanf("%d", &n) == 1 && n) { char des[20], asc[20]; sprintf(des, "%d", n); sprintf(asc, "%d", n); int x, y, chain = 0; map<int, int> record; printf("Original number was %d\n", n); record[n] = 1; while(true) { chain++; qsort(des, strlen(des), sizeof(char), cmp1); qsort(asc, strlen(asc), sizeof(char), cmp2); sscanf(asc, "%d", &x); sscanf(des, "%d", &y); n = x - y; printf("%d - %d = %d\n", x, y, n); if(record[n] == 1) break; record[n] = 1; sprintf(des, "%d", n); sprintf(asc, "%d", n); } printf("Chain length %d\n\n", chain); } return 0; }
No comments:
Post a Comment