锘??xml version="1.0" encoding="utf-8" standalone="yes"?> In 1949 the Indian mathematician D.R. Kaprekar discovered a class of numbers called self-numbers. For any positive integer n, define d(n) to be n plus the sum of the digits of n. (The d stands for digitadition, a term coined by Kaprekar.) For example, d(75) = 75 + 7 + 5 = 87. Given any positive integer n as a starting point, you can construct the infinite increasing sequence of integers n, d(n), d(d(n)), d(d(d(n))), .... For example, if you start with 33, the next number is 33 + 3 + 3 = 39, the next is 39 + 3 + 9 = 51, the next is 51 + 5 + 1 = 57, and so you generate the sequence 33, 39, 51, 57, 69, 84, 96, 111, 114, 120, 123, 129, 141, ... The number n is called a generator of d(n). In the sequence above, 33 is a generator of 39, 39 is a generator of 51, 51 is a generator of 57, and so on. Some numbers have more than one generator: for example, 101 has two generators, 91 and 100. A number with no generators is a self-number. There are thirteen self-numbers less than 100: 1, 3, 5, 7, 9, 20, 31, 42, 53, 64, 75, 86, and 97. Write a program to output all positive self-numbers less than 10000 in increasing order, one per line. When printing out a document, normally the first page is printed first, then the second, then the third, and so on until the end. However, when creating a fold-over booklet, the order of printing must be altered. A fold-over booklet has four pages per sheet, with two on the front and two on the back. When you stack all the sheets in order, then fold the booklet in half, the pages appear in the correct order as in a regular book. For example, a 4-page booklet would print on 1 sheet of paper: the front will contain page 4 then page 1, and the back will contain page 2 then page 3. Your task is to write a program that takes as input the number of pages to be printed, then generates the printing order. Each test case consists of a positive integer n on a line by itself, where n is the number of pages to be printed; n will not exceed 100. Output must be in ascending order by sheet, front first, then back. #include<cstdlib>
#include<iostream>
using聽namespace聽std;
聽
class聽B聽
{
public:
聽聽聽virtual聽int聽f1()
{cout<<"B::f1()"<<endl;return聽0;}
聽聽聽virtual聽void聽f2(聽int聽val)
{cout<<"B::f2(int)"<<endl;}
聽聽聽virtual聽int聽f3(聽int聽val聽)
{cout<<"B::f3(int)"<<endl;return聽0;}
};
聽
class聽D聽:聽public聽B聽
{
聽聽聽int聽f1()
{cout<<"D::f1()"<<endl;return聽0;}
聽聽聽virtual聽void聽f4()
{cout<<"D::f4()"<<endl;}
聽聽聽int聽f3(int聽val)
{cout<<"D::f3(int)"<<endl;return聽0;}
};
聽
int聽main(int聽argc,聽char聽*argv[])
{
聽聽聽B聽*bp聽=聽new聽D;
聽聽聽bp->f3(12);//D涓殑f3鏄痯rivate鐨勶紝鍙互璁塊棶#1
聽聽聽D聽*dp=new聽D;
聽聽聽dp->f3(12);//f3鏄痯rivate,璁塊棶涓嶄簡錛岀紪璇戦氫笉榪?/span>
聽聽聽system("PAUSE");
聽聽聽return聽EXIT_SUCCESS;
}
鍏跺疄榪欐槸涓涓叧浜庤闂潈闄愬喅瀹氭椂闂寸殑闂錛岀敱浜?u>璁塊棶鏉冮檺鏄紪璇戞椂闂村喅瀹氱殑錛岃屼笉鏄繍琛屾椂鍐沖畾鐨勩?br />B *bp = new D;聽聽// 姝ゆ椂bp鎵鎸囧悜鐨勭被鍨嬫槸B鑰屼笉鏄疍錛岃孊鐨刦3()鏄叕鏈夌殑錛屾墍浠ュ彲浠ヨ闂?br />D *dp = new D; // 姝ゆ椂dp鎵鎸囧悜鐨勭被鍨嬫槸D錛岃孌鐨刦3()鏄鏈夌殑錛屾墍浠ヤ笉鑳借闂?br />Output
1
3
5
7
9
20
31
42
53
64
|
| <-- a lot more numbers
|
9903
9914
9925
9927
9938
9949
9960
9971
9982
9993Solution
#include聽<iostream>
using聽namespace聽std;
const聽long聽N聽=聽10000;聽聽聽聽聽//鏈澶ц嚜鐒舵暟
char聽Arr[N聽+聽9*4]=
{0};聽聽聽//鏄惁鏄鎺掗櫎鐨勬暟瀛楋紵聽+9*4鏄負浜嗚鍐嶅鏀?浣嶆暟
long聽DealNum(long聽n)
{
聽聽long聽sum聽=聽n;
聽聽while聽(n聽!=聽0)
聽聽
{
聽聽聽聽sum聽+=聽n%10;
聽聽聽聽n聽/=聽10;
聽聽}
聽聽return聽sum;
}
int聽main()
{
聽聽int聽i;
聽聽for(i聽=聽1;聽i聽<聽N;聽i++)
聽聽
{
聽聽聽聽Arr[DealNum(i)]聽=聽1;
聽聽}
聽聽for(i聽=聽1;聽i聽<聽N;聽i++)
聽聽
{
聽聽聽聽if聽(!Arr[i])
聽聽聽聽聽聽聽聽cout<<i<<endl;
聽聽}
聽聽return聽0;
}
Front Back
------------- -------------
| | | | | |
| 4 | 1 | | 2 | 3 |
| | | | | |
------------- -------------Input聽
The input file contains one or more test cases, followed by a line containing the number 0 that indicates the end of the file.
Output聽
For each test case, output a report indicating which pages should be printed on each sheet, exactly as shown in the example. If the desired number of pages does not completely fill up a sheet, then print the word Blank in place of a number. If the front or back of a sheet is entirely blank, do not generate output for that side of the sheet.
Sample Input聽
1
14
4
0Sample Output聽
Printing order for 1 pages:
Sheet 1, front: Blank, 1
Printing order for 14 pages:
Sheet 1, front: Blank, 1
Sheet 1, back : 2, Blank
Sheet 2, front: 14, 3
Sheet 2, back : 4, 13
Sheet 3, front: 12, 5
Sheet 3, back : 6, 11
Sheet 4, front: 10, 7
Sheet 4, back : 8, 9
Printing order for 4 pages:
Sheet 1, front: 4, 1
Sheet 1, back : 2, 3Solution
#include聽<iostream>
using聽namespace聽std;
#define聽PAGES聽100
typedef聽struct聽side
{聽聽聽聽
聽聽聽聽int聽left,right;
}side;
typedef聽struct聽sheet
{
聽聽聽聽side聽front;
聽聽聽聽side聽back;聽聽聽聽
}sheet;
int聽numSides;
sheet聽sheets[PAGES];
void聽PrintPages(int聽numSides)
{
聽聽聽聽int聽numSidesNew;聽聽聽聽
聽聽聽聽int聽add,pages;
聽聽聽聽add聽=聽numSides%4;
聽聽聽聽if(add聽!=聽0)
{
聽聽聽聽聽聽聽聽numSidesNew聽=聽numSides聽+聽4聽-聽add;聽聽聽聽//聽澧炲姞鍚庣殑鎬婚潰鏁?numSides涓哄疄闄呯殑鎬婚潰鏁?/span>
聽聽聽聽}
聽聽聽聽else
聽聽聽聽聽聽聽聽numSidesNew聽=聽numSides;
聽聽聽聽pages聽=聽numSidesNew聽/聽4;聽聽聽聽//聽鎬葷焊寮犳暟
聽聽聽聽for(int聽i聽=聽0;聽i聽<聽pages;聽i++)
{
聽聽聽聽聽聽聽聽sheets[i].front.left聽=聽numSidesNew聽-聽2*i;
聽聽聽聽聽聽聽聽if(sheets[i].front.left聽>聽numSides)
{
聽聽聽聽聽聽聽聽聽聽聽聽sheets[i].front.left聽=聽0;聽聽聽聽//聽琛ㄦ槑搴斾負blank
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽sheets[i].front.right聽=聽2*i+1;
聽聽聽聽聽聽聽聽if(sheets[i].front.right聽>聽numSides)
{
聽聽聽聽聽聽聽聽聽聽聽聽sheets[i].front.right聽=聽0;聽聽聽聽//聽琛ㄦ槑搴斾負blank
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽sheets[i].back.left聽=聽2*(i+1);
聽聽聽聽聽聽聽聽if(sheets[i].back.left聽>聽numSides)
{
聽聽聽聽聽聽聽聽聽聽聽聽sheets[i].back.left聽=聽0;聽聽聽聽//聽琛ㄦ槑搴斾負blank
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽sheets[i].back.right聽=聽numSidesNew聽-聽2*i聽-聽1;
聽聽聽聽聽聽聽聽if(sheets[i].back.right聽>聽numSides)
{
聽聽聽聽聽聽聽聽聽聽聽聽sheets[i].back.right聽=聽0;
聽聽聽聽聽聽聽聽}
聽聽聽聽}
聽聽聽聽cout聽<<聽"Printing聽order聽for聽"聽<<聽numSides聽<<聽"聽pages:"聽<<聽endl;
聽聽聽聽for(int聽j聽=聽0;聽j聽<聽pages;聽j++)
{
聽聽聽聽聽聽聽聽if(sheets[j].front.left聽||聽sheets[j].front.right)
{
聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<聽"Sheet聽"聽<<聽j+1聽<<",聽front:聽";
聽聽聽聽聽聽聽聽聽聽聽聽if(sheets[j].front.left)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<聽sheets[j].front.left聽<<聽",";
聽聽聽聽聽聽聽聽聽聽聽聽else
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<聽"Blank,";
聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<聽"聽";
聽聽聽聽聽聽聽聽聽聽聽聽if(sheets[j].front.right)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<聽sheets[j].front.right;
聽聽聽聽聽聽聽聽聽聽聽聽else
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<聽"Blank,";
聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<聽endl;
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽if(sheets[j].back.left聽||聽sheets[j].back.right)
{
聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<聽"Sheet聽"聽<<聽j+1聽<<",聽back聽:聽";
聽聽聽聽聽聽聽聽聽聽聽聽if(sheets[j].back.left)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<聽sheets[j].back.left聽<<聽",";
聽聽聽聽聽聽聽聽聽聽聽聽else
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<聽"Blank,";
聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<聽"聽";
聽聽聽聽聽聽聽聽聽聽聽聽if(sheets[j].back.right)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<聽sheets[j].back.right;
聽聽聽聽聽聽聽聽聽聽聽聽else
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<聽"Blank";
聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<聽endl;
聽聽聽聽聽聽聽聽}
聽聽聽聽}
}
int聽main()
{
聽聽聽聽int聽numSides;
聽聽聽聽while(cin聽>>聽numSides)
{
聽聽聽聽聽聽聽聽if(numSides聽==聽0)
{
聽聽聽聽聽聽聽聽聽聽聽聽break;
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽PrintPages(numSides);
聽聽聽聽}
聽聽聽聽return聽0;
}
Write a program to calculate the Fibonacci Numbers.
The input to your program would be a sequence of numbers smaller or equal than 5000, each on a separate line, specifying which Fibonacci number to calculate.
Your program should output the Fibonacci number for each input value, one per line.
5
7
11
The Fibonacci number for 5 is 5
The Fibonacci number for 7 is 13
The Fibonacci number for 11 is 89
#include
<iostream>
using
namespace
std;
int
main()
{
聽聽
int
first,next,temp,n;
聽聽
while(cin >> n) {
聽聽聽聽聽
first = 0;
聽聽聽聽聽
next = 1;
聽聽聽聽聽
temp = 0;
聽聽聽聽聽
if(n == 0 || n == 1) {
聽聽聽聽聽聽聽
cout << "The Fibonacci number for" << " " << n << " " << "is" << " " << n << endl;
聽聽聽聽聽 }
聽聽聽聽聽
else {
聽聽聽聽聽聽聽
for(inti = 2; i <= n; i++) {
聽聽聽聽聽聽聽聽聽聽
temp = first + next;
聽聽聽聽聽聽聽聽聽聽
first = next;
聽聽聽聽聽聽聽聽聽聽
next = temp;
聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽
cout << "The Fibonacci number for" << " " << n << " " << "is" << " " << temp << endl;
聽聽聽聽聽 }
聽聽 }
聽聽
return 0;
}
Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In this problem you will be analyzing a property of an algorithm whose classification is not known for all possible inputs.
Consider the following algorithm:
1. input n
2. print n
3. if n = 1 then STOP
4. if n is odd then tex2html_wrap_inline44
5. else tex2html_wrap_inline46
6. GOTO 2
Given the input 22, the following sequence of numbers will be printed 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
It is conjectured that the algorithm above will terminate (when a 1 is printed) for any integral input value. Despite the simplicity of the algorithm, it is unknown whether this conjecture is true. It has been verified, however, for all integers n such that 0 < n < 1,000,000 (and, in fact, for many more numbers than this.)
Given an input n, it is possible to determine the number of numbers printed (including the 1). For a given n this is called the cycle-length of n. In the example above, the cycle length of 22 is 16.
For any two numbers i and j you are to determine the maximum cycle length over all numbers between i and j.
The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 1,000,000 and greater than 0.
You should process all pairs of integers and for each pair determine the maximum cycle length over all integers between and including i and j.
You can assume that no opperation overflows a 32-bit integer.
For each pair of input integers i and j you should output i, j, and the maximum cycle length for integers between and including i and j. These three numbers should be separated by at least one space with all three numbers on one line and with one line of output for each line of input. The integers i and j must appear in the output in the same order in which they appeared in the input and should be followed by the maximum cycle length (on the same line).
1 10
100 200
201 210
900 1000
1 10 20
100 200 125
201 210 89
900 1000 174
#include
<iostream>
using
namespace
std;
int
cycle(intm)
{
聽聽
int
i = 1;
聽聽
while (m != 1){
聽聽聽聽聽
if(m%2)
聽聽聽聽聽聽聽
m = m*3 + 1;
聽聽聽聽聽
else
聽聽聽聽聽聽聽
m /= 2;
聽聽聽聽聽
i++;
聽聽 }
聽聽
return
i;
}聽聽
int
main()
{
聽聽
int
m,n,max,temp;
聽聽
int
mOriginal,nOriginal;
聽聽
int
i;
聽聽
while (cin >> m >> n){
聽聽聽聽聽
mOriginal = m;
聽聽聽聽聽
nOriginal = n;
聽聽聽聽聽
if (m > n){
聽聽聽聽聽聽聽
temp = m;
聽聽聽聽聽聽聽
m = n;
聽聽聽聽聽聽聽
n = temp;
聽聽聽聽聽 }
聽聽聽聽聽
max = cycle(m);
聽聽聽聽聽
for (i = m+1; i <= n; i++){
聽聽聽聽聽聽聽
temp = cycle(i);
聽聽聽聽聽聽聽
if (temp > max){
聽聽聽聽聽聽聽聽聽聽
max = temp;
聽聽聽聽聽聽聽 }
聽聽聽聽聽 }聽
聽聽聽聽聽
cout << mOriginal << " " << nOriginal << " " << max << endl;
聽聽 }
聽聽
return 0;
}
a and b
a+b
1 5
6