Arrange an Array to Form a Smallest Digit
来源:互联网 发布:openwrt 串口编程 编辑:程序博客网 时间:2024/05/15 09:50
#include <stdlib.h>#include <string.h>#include <iostream>#include <algorithm>#include "oj.h"using namespace std;bool comp(int iElem1, int iElem2){string str1;char ch1[100];memset(ch1, 0, sizeof(char) * 100);itoa(iElem1, ch1, 10);str1 = ch1;string str2;char ch2[100];memset(ch2, 0, sizeof(char) * 100);itoa(iElem2, ch2, 10);str2 = ch2;int iCur1 = 0; //str1的下标int iCur2 = 0;//str2的下标int len1 = str1.length();int len2 = str2.length();while (iCur1 < len1 && iCur2 < len2){if (str1[iCur1] < str2[iCur2]){return true;}else if(str1[iCur1] > str2[iCur2]){return false;}iCur1 ++;iCur2 ++;}if (iCur1 < len1){while (iCur1 < len1){iCur2 = (iCur2 + 1)% len2;if (str1[iCur1] < str2[iCur2]){return true;}else if(str1[iCur1] > str2[iCur2]){return false;}iCur1 ++;}}else if(iCur2 < len2){while (iCur2 < len2){iCur1 = (iCur1 + 1)% len1;if (str1[iCur1] < str2[iCur2]){return true;}else if(str1[iCur1] > str2[iCur2]){return false;}iCur2 ++;}}return false;}// 功能:将输入的数组排成最小的数// 输入: int a[]:整型数组// int nCount:数组长度// char * strRst 返回值// 输出:// 返回:成功返回0 异常返回-1int smallestDigit(int a[],int nCount,char * strRst){if (NULL == a){return -1;}sort(a, a + nCount , comp);char cTemp[100];for (int i = 0; i < nCount; ++i ){cout<<a[i]<<"";memset(cTemp, 0, sizeof(char) * 100);itoa(a[i], cTemp, 10);strcat(strRst, cTemp);}cout<<endl;return 1;}int main(){int a[] = {1,2};char szRst[100] = {0};char *strExpect = "12";smallestDigit(a,2,szRst);cout<<szRst<<endl;//CPPUNIT_ASSERT(0 == strcmp(szRst,strExpect));return 0;}
0 0
- Arrange an Array to Form a Smallest Digit
- Arrange an Array to Form a Smallest Digit
- Arrange an Array to Form a Smallest Digit
- Arrange an Array to Form a Smallest Digit
- [华为机试练习题]29.Arrange an Array to Form a Smallest Digit
- 华为机试题:Arrange an Array to Form a Smallest Digit
- Given an array of characters which form a sentence of words, give an efficient algorithm to reverse
- Resizing a Form to Fit an Image
- CareerCup Given an array of (unsorted) integers, arrange them such that a < b > c < d > e... etc.
- Converting a array to an array of ranges in ruby
- find ith smallest element in an array
- [leetcode]Kth Smallest Element in an Array
- A very efficient and economical way to shift an array
- Algorithm: Reverse a Integer to an array with recursion
- How to group anagrams in a string into an array
- pointer to ref of an array, and to the func that return a ref of an array
- 001:Why is it faster to process a sorted array than an unsorted array?
- Why is it faster to process a sorted array than an unsorted array?
- AngularJS
- Selain itu Tapi apa
- 中断处理“下半部”机制
- 如何科学增高
- Ng机器学习系列补充:6、集成学习算法AdaBoost(Adaptive Boosting)
- Arrange an Array to Form a Smallest Digit
- 模仿360主页实现的两种方法-frameset
- Android控件使用注意事项 ——> ImageView
- sql 不允许保存更改。您所做的更改要求删除并重新创建以下表。
- 网线线序排列
- poj2236 Wireless Network(并查集)
- 都不会个还是
- 角度看脚后
- 你的第一个中断程序!