// 300.cpp : 定义控制台应用程序的入口点。
//
#include 'stdafx.h'
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;
class CCipher
{public:
string decode(string cipherText, int shift)
{
string ret = '';
int len = cipherText.size();
for(int i=0; i<len; i++)
{
char tmp='Z'-cipherText[i]+shift;
tmp=tmp%26;
ret+=('Z'-tmp);
}
return ret;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
CCipher c;
string ci ='TOPCODER';
string ret = c.decode(ci,2);
copy(ret.begin(),ret.end(),ostream_iterator<char>(cout,'
'));
return 0;
}
Problem Statement
|
| |
Julius Caesar used a system of cryptography, now known as Caesar
Cipher, which shifted each letter 2 places further through the
alphabet (e.g. 'A' shifts to 'C', 'R' shifts to 'T', etc.). At the
end of the alphabet we wrap around, that is 'Y' shifts to 'A'.
We can, of course, try shifting by any number. Given an encoded
text and a number of places to shift, decode it.
For example, 'TOPCODER' shifted by 2 places will be encoded as
'VQREQFGT'. In other words, if given (quotes for clarity)
'VQREQFGT' and 2 as input, you will return 'TOPCODER'. See example
0 below.
|
Definition
|
| |
| Class: |
CCipher |
| Method: |
decode |
| Parameters: |
string, int |
| Return |
|
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;
#define N 10
class bp
{
public:
bool operator()(int a,int b)
{
return (a+b==13);
}
};
class CircleGame
{
private:
vector<int> change(string deck)
{
int len =deck.size();
int val;
vector<int> v(len);
for(int i=0 ; i < len ;i++)
{
if(deck[i]=='A')
val=1;
else if(deck[i]=='T')
val=10;
else if(deck[i]=='J')
val=11;
else if(deck[i]=='Q')
val=12;
else if(deck[i]=='K')
val=13;
else
Problem Statement
|
| |
Tommy is learning a simple card game called Circle. To play the
game, the single player shuffles a deck of cards. He or she then
flips through the deck, removing all instances of the 'K' card, and
all consecutive pairs of cards that add up to 13. The deck does
wrap around, so that if the last card remaining in the deck and the
first card remaining in the deck add up to 13, they are both
removed. The player keeps cycling through the deck until no more
cards can be removed.
Create a class CircleGame containing the method cardsLeft that
takes a string deck representing a not-necessarily complete
nor correct deck of cards. Each character of deck represents
the value of a card without the suit. The values shown on the card
represent the following numerical values:
'A' - 1 '2' - 2 '3' - 3 '4' - 4 '5' - 5 '6' - 6 '7' - 7 '8' - 8 '9'
|
// 1000_2.cpp : 定义控制台应用程序的入口点。
//
#include 'stdafx.h'
#include <vector>
#include <algorithm>
#include <iostream>
#include <set>
#include <iterator>
using namespace std;
class BridgeCrossing
{
public:
int minTime(vector <int> times)
{
//if (times.size() <= 1) {
// return times[0];
//}
//int persons = times.size();
//vector<int> left(times.begin(),
times.end());
//vector<int> right(persons, 0);
//int mintime = 0;
//int lp = persons;
//int tmp = 0;
//sort(left.begin(), left.end());
//while (lp > 0) {
// right[0] = left[0]; lp--;
// right[1] = left[1]; lp--;
// mintime +=
|
一群人晚上过桥,每次只能过2个人,并且需要一盏灯。 每个人过桥时间不同。计算最短时间
给出是过桥时间如{1,2,5,10},计算出最小时间17
首先 1,2 过去 时间 2
1 回来
时间 1
5,10 过去 时间 10
2
回来 时间 2
1,2 过去 时间 2
总共 17
Problem Statement
|
| |
A well-known riddle goes like this: Four people are crossing an
old bridge. The bridge cannot hold more than two people at once. It
is dark, so they can't walk without a
|
程序员常常需要实现回调。本文将讨论函数指针的基本原则并说明如何使用函数指针实现回调。注意这里针对的是普通的函数,不包括完全依赖于不同语法和语义规则的类成员函数(类成员指针将在另文中讨论)。
声明函数指针
回调函数是一个程序员不能显式调用的函数;通过将回调函数的地址传给调用者从而实现调用。要实现回调,必须首先定义函数指针。尽管定义的语法有点不可思议,但如果你熟悉函数声明的一般方法,便会发现函数指针的声明与函数声明非常类似。请看下面的例子:
void f();// 函数原型
上面的语句声明了一个函数,没有输入参数并返回void。那么函数指针的声明方法如下:
void (*) ();
让我们来分析一下,左边圆括弧中的星号是函数指针声明的关键。另外两个元素是函数的返回类型(void)和由边圆括弧中的入口参数(本例中参数是空)。注意本例中还没有创建指针变量-只是声明了变量类型。目前可以用这个变量类型来创建类型定义名及用sizeof表达式获得函数指针的大小:
// 获得函数指针的大小
unsigned psize = sizeof (void (*)
一行中方格的个数为m,则这一行中有m个(单方格),m-2+1个(两个方格连在一起)
m-n+1个(n各方格在一起)
class RectangularGrid
{
public:
long long countRectangles(int x,int y)
{
int i,j;
long long r=0;
for(i=1; i<=x; i++)
for(j=1;j<=y;j++)
if(i!=j)
r+=(x-i+1)*(y-j+1);
return r;
}
};
Problem Statement
Given the width and height of a rectangular grid, return the total
number of rectangles (NOT counting squares) that can be found on
this grid.
For example, width = 3, height = 3 (see diagram below):
__ __ __
|__|__|__|
|__|__|__|
|__|__|__|
In this grid, there are 4 2x3 rectangles, 6 1x3 rectangles and 12
1x2 rectangles. Thus there is a total of 4 + 6 + 12 = 22
rectangles. Note we don't count 1x1, 2x2 and 3x3 rectangles because
they are squares.
Definition
Class:
RectangularGrid
Method:
countRectangles
Parameters:
int, int
Returns:
long long
Method signature:
long long countRectangles(int width, int height)
(be sure your method is public)
Notes
-
rectangles with equals sides (squares) should not be counted.
Constraints
-
width and h
4.技巧题
试题1:请写一个C函数,若处理器是Big_endian的,则返回0;若是Little_endian的,则返回1
解答:
int checkCPU()
{
{
union w
{
int a;
char b;
} c;
c.a = 1;
return (c.b == 1);
}
} |
剖析:
嵌入式系统开发者应该对Little-endian和Big-endian模式非常了解。采用Little-endian模式的CPU对操作数的存放方
式是从低字节到高字节,而Big-endian模式对操作数的存放方式是从高字节到低字节。例如,16bit宽的数0x1234在Little-
endian模式CPU内存中的存放方式(假设从地址0x4000开始存放)为:
| 内存地址 |
存放内容 |
| 0x4000 |
0x34 |
| 0x4001 |
0x12 |