#include<iostream>
using namespace std;
int getNum(int n, int k)
{
int x;
intcount = 0;
if (n == 0 && k == 0)
return1;
for (int i = 1; x=n/ i; i *= 10)
{
int high = x / 10;
if (k == 0)
{
if (high)
high--;
else
{
count++;
break;
}
}
count += high*i;
int current = x % 10;
if (current > k)
count += i;
elseif (current == k)
count += n - x*i + 1;
}
returncount;
}
int main()
{
int n;
while (cin >> n)
cout << getNum(n,1) << endl;
return0;
}
2. 数组中最小的K个数
#include<iostream>
using namespace std;
int partion(int *arr, intleft, intright)
{
int start = left;
intend = right;
int key = arr[left];
while (start < end)
{
while (start<end&&arr[end]>=key)
--end;
while (start < end&&arr[start] <= key)
++start;
swap(arr[end], arr[start]);
}
swap(arr[left], arr[start]);
return start;
}
void getArr(int *arr, intlen, int k, int *res)
{
intend = len - 1;
int start = 0;
int index = partion(arr, start, end);
while (index != k - 1)
{
if (index > k - 1)
{
end = index - 1;
index = partion(arr, start, end);
}
else
{
start = index + 1;
index = partion(arr, start, end);
}
}
for (int i = 0; i < k; i++)
res[i] = arr[i];
}
void GetLength(int *arr, intlen, int k)
{
if (arr == nullptr || len <= 0 || k <= 0 || k >len)
return;
int *res = newint[k];
getArr(arr, len, k, res);
for (int i = 0; i < k; i++)
{
cout << res[i] << " ";
}
cout << endl;
}
int main()
{
int arr[] = { 1, 2, 3, 65, 0,5, 7, 43, 2 };
intlen = sizeof(arr) / sizeof(arr[0]);
int k;
while (cin>>k)
GetLength(arr, len,k);
}
3. 连续子数组的最大和
#include<iostream>usingnamespacestd;
int getMax(int *arr, int len)
{
int max = 0;
int sum = 0;
for (int i = 0; i < len; i++)
{
sum += arr[i];
if (sum>max)
max = sum;
elseif (sum <0)
sum = 0;
}
return max;
}
int main()
{
int arr[] = { 1, -2, 3, 10, -4, 7, 2, -5 };
int len = sizeof(arr) / sizeof(arr[0]);
cout << getMax(arr, len) << endl;
system("pause");
}
4. 数字序列中某一位的数字
// 《剑指Offer——名企面试官精讲典型编程题》代码// 作者:何海涛//==================================================================// 面试题44:数字序列中某一位的数字// 题目:数字以0123456789101112131415…的格式序列化到一个字符序列中。在这// 个序列中,第5位(从0开始计数)是5,第13位是1,第19位是4,等等。请写一// 个函数求任意位对应的数字。#include<iostream>
using namespace std;
int countOfIntergers(int digits)
{
if (digits == 1)
return10;
intcount = (int)pow(10, digits - 1);
return9 * count;
}
int beginNumber(int digits)
{
if (digits == 1)
return0;
return (int)pow(10, digits - 1);
}
int digitAtIndex(intindex, int digits)
{
int number = beginNumber(digits) + index / digits;
int indexFromRight = digits - index%digits;
for (int i = 1; i < indexFromRight; i++)
number /= 10;
return number % 10;
}
int digitAtIndex(intindex)
{
if (index < 0)
return -1;
int digits = 1;
for (;;)
{
int numbers = countOfIntergers(digits);
if (index < numbers*digits)
return digitAtIndex(index, digits);
index -= digits*numbers;
digits++;
}
return -1;
}
int main()
{
int n;
while (cin >> n)
cout << digitAtIndex(n) << endl;s
}
5. 把数字数组排成最小的数
// 面试题45:把数组排成最小的数// 题目:输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼// 接出的所有数字中最小的一个。例如输入数组{3, 32, 321},则打印出这3个数// 字能排成的最小数字321323。#include<iostream>usingnamespacestd;
constint g_MaxNumberLength = 10;
char *comb1 = newchar[g_MaxNumberLength * 2 + 1];
char *comb2 = newchar[g_MaxNumberLength * 2 + 1];
int compare(constvoid *strNumber1, constvoid *strNumber2)
{
strcpy(comb1, *(constchar **)strNumber1);
strcat(comb1, *(constchar **)strNumber2);
strcpy(comb2, *(constchar **)strNumber2);
strcat(comb2, *(constchar **)strNumber1);
returnstrcmp(comb1, comb2);
}
void PrintMinNumber(int *arr, int length)
{
if (arr == nullptr || length <= 0)
return;
char **strNumbers = (char **)newint[length];
for (int i = 0; i < length; ++i)
{
strNumbers[i] = newchar[g_MaxNumberLength + 1];
sprintf(strNumbers[i], "%d", arr[i]);
}
qsort(strNumbers, length, sizeof(char *), compare);
for (int i = 0; i < length; ++i)
printf("%s", strNumbers[i]);
printf("\n");
for (int i = 0; i < length; ++i)
delete[] strNumbers[i];
delete[]strNumbers;
}
6. 把数字翻译成字符串
/*******************************************************************
Copyright(c) 2016, Harry He
All rights reserved.
Distributed under the BSD license.
(See accompanying file LICENSE.txt at
https://github.com/zhedahht/CodingInterviewChinese2/blob/master/LICENSE.txt)
*******************************************************************///==================================================================// 《剑指Offer——名企面试官精讲典型编程题》代码// 作者:何海涛//==================================================================// 面试题46:把数字翻译成字符串// 题目:给定一个数字,我们按照如下规则把它翻译为字符串:0翻译成"a",1翻// 译成"b",……,11翻译成"l",……,25翻译成"z"。一个数字可能有多个翻译。例// 如12258有5种不同的翻译,它们分别是"bccfi"、"bwfi"、"bczi"、"mcfi"和// "mzi"。请编程实现一个函数用来计算一个数字有多少种不同的翻译方法。#include <string>#include <iostream>usingnamespacestd;
int GetTranslationCount(conststring& number);
int GetTranslationCount(int number)
{
if(number < 0)
return0;
string numberInString = to_string(number);
return GetTranslationCount(numberInString);
}
int GetTranslationCount(conststring& number)
{
int length = number.length();
int* counts = newint[length];
int count = 0;
for(int i = length - 1; i >= 0; --i)
{
count = 0;
if(i < length - 1)
count = counts[i + 1];
else
count = 1;
if(i < length - 1)
{
int digit1 = number[i] - '0';
int digit2 = number[i + 1] - '0';
int converted = digit1 * 10 + digit2;
if(converted >= 10 && converted <= 25)
{
if(i < length - 2)
count += counts[i + 2];
else
count += 1;
}
}
counts[i] = count;
}
count = counts[0];
delete[] counts;
return count;
}
// ====================测试代码====================void Test(conststring& testName, int number, int expected)
{
if(GetTranslationCount(number) == expected)
cout << testName << " passed." << endl;
elsecout << testName << " FAILED." << endl;
}
void Test1()
{
int number = 0;
int expected = 1;
Test("Test1", number, expected);
}
void Test2()
{
int number = 10;
int expected = 2;
Test("Test2", number, expected);
}
void Test3()
{
int number = 125;
int expected = 3;
Test("Test3", number, expected);
}
void Test4()
{
int number = 126;
int expected = 2;
Test("Test4", number, expected);
}
void Test5()
{
int number = 426;
int expected = 1;
Test("Test5", number, expected);
}
void Test6()
{
int number = 100;
int expected = 2;
Test("Test6", number, expected);
}
void Test7()
{
int number = 101;
int expected = 2;
Test("Test7", number, expected);
}
void Test8()
{
int number = 12258;
int expected = 5;
Test("Test8", number, expected);
}
void Test9()
{
int number = -100;
int expected = 0;
Test("Test9", number, expected);
}
int main(int argc, char* argv[])
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
Test7();
Test8();
Test9();
return0;
}
1. 1-n中某数字出现的次数#include<iostream>using namespace std;int getNum(int n,int k){ int count = 0; int x; for (int i = 1; x=n/i; i *= 10) { int high = x / 10; if (k == 0)