Integer Inquiry、All in All、Substitution Cypher、 Web Navigation、Perfect Pth Powers、HTML

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/summer2day/article/details/85230406

Integer Inquiry

One of the first users of BIT’s new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking various sums of those numbers.
This supercomputer is great,'' remarked Chip.I only wish Timothy were here to see these results.’’ (Chip moved to a new apartment, once one became available on the third floor of the Lemon Sky apartments on Third Street.)

Input

The input will consist of at most 100 lines of text, each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length, and will only contain digits (no VeryLongInteger will be negative).
The final input line will contain a single zero on a line by itself.

Output

Your program should output the sum of the VeryLongIntegers given in the input.

Sample Input
123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0

Sample Output
370370367037037036703703703670

#include<iostream>
using namespace std;
string stringadd(string s1,string s2)
{
	string s;
	//补成一样的长度 
	int n1=s1.length();
	int n2=s2.length();
	if(n1<n2)
	{
		for(int i=0;i<n2-n1;i++)
			s1="0"+s1;
	}
	else
	{
		for(int i=0;i<n1-n2;i++)
			s2="0"+s2;
	 } 
	 int n=s1.length();
	 int c=0;//进位
	 int temp;
	 for(int i=n-1;i>=0;i--)
	 {
	 	temp=s1[i]-'0'+s2[i]-'0'+c;
	 	c=temp/10;
	 	temp=temp%10;
	 	s=char(temp+'0')+s;
	  } 
	  if(c!=0)
	  	s=char(c+'0')+s;
	  return s;
}
int main()
{
	string s;
	string sum="0";
	while(1)
	{
		cin>>s;
		if(s=="0")
		{
			cout<<sum<<endl;
			break;
		}
			
		sum=stringadd(sum,s);

	}
}

All in All

You have devised a new encryption technique which encodes a message by inserting between its characters randomly generated strings in a clever way. Because of pending patent issues we will not discuss in detail how the strings are generated and inserted into the original message. To validate your method, however, it is necessary to write a program that checks if the message is really encoded in the final string.
Given two strings s and t, you have to decide whether s is a subsequence of t, i.e. if you can remove characters from t such that the concatenation of the remaining characters is s.
Input Specification
The input contains several testcases. Each is specified by two strings s, t of alphanumeric ASCII characters separated by whitespace. Input is terminated by EOF.
Output Specification
For each test case output, if s is a subsequence of t.
Sample Input
sequence subsequence
person compression
VERDI vivaVittorioEmanueleReDiItalia
caseDoesMatter CaseDoesMatter
Sample Output
Yes
No
Yes
No

#include<iostream>
using namespace std;
int main()
{
	string s,t;
	while(cin>>s>>t)
	{
		int i,j;
		for(i=0,j=0;i<s.length()&&j<t.length();)
		{
			if(s[i]==t[j])
			{
				i++;
				j++;
			}
			else
			{
				j++;
			}
		}
		if(i==s.length())
			cout<<"Yes"<<endl;
		else
			cout<<"No"<<endl;
	}
}

Substitution Cypher

Substitution cyphers are the simplest of cyphers where the letters of one alphabet are substituted for the letters of another alphabet. In one form or another, they’ve been in use for over 2000 years.

Input
A line containing the plaintext alphabet
A line containing the substitution alphabet
Several lines of text

Output
A line containing the substitution alphabet
A line containing the plaintext alphabet
The converted lines of text
Please note: All lines will be at most 64 characters, plus a trailing end-of-line character. Pass through all characters not found in the plaintext alphabet.

Sample Input
abcdefghijklmnopqrstuvwxyz
zyxwvutsrqponmlkjihgfedcba
Shar’s Birthday:
The birthday is October 6th, but the party will be Saturday,
October 5. It’s my 24th birthday and the first one in some
years for which I’ve been employed. Plus, I have new clothes.
So I have cause to celebrate. More importantly, though,
we’ve cleaned the house! The address is 506-D Albert Street.
Extra enticement for CS geeks: there are several systems in
the house, and the party is conveniently scheduled for 3 hours
after the second CSC programming contest ends (not to mention,
within easy walking distance)!

Sample Output
zyxwvutsrqponmlkjihgfedcba
abcdefghijklmnopqrstuvwxyz
Sszi’h Brigswzb:
Tsv yrigswzb rh Oxglyvi 6gs, yfg gsv kzigb droo yv Szgfiwzb,
Oxglyvi 5. Ig’h nb 24gs yrigswzb zmw gsv urihg lmv rm hlnv
bvzih uli dsrxs I’ev yvvm vnkolbvw. Pofh, I szev mvd xolgsvh.
Sl I szev xzfhv gl xvovyizgv. Mliv rnkligzmgob, gslfts,
dv’ev xovzmvw gsv slfhv! Tsv zwwivhh rh 506-D Aoyvig Sgivvg.
Ecgiz vmgrxvnvmg uli CS tvvph: gsviv ziv hvevizo hbhgvnh rm
gsv slfhv, zmw gsv kzigb rh xlmevmrvmgob hxsvwfovw uli 3 slfih
zugvi gsv hvxlmw CSC kiltiznnrmt xlmgvhg vmwh (mlg gl nvmgrlm,
drgsrm vzhb dzoprmt wrhgzmxv)!

#include<iostream>
#include<stdio.h>//有的编译器需要
#include<string.h>
using namespace std;
int main()
{
	char p[256],s[256];
	//输入明文和替换的
	gets(p);
	gets(s);
	//输出替换和明文
	puts(s);
	puts(p);
	int n=strlen(s);
	char ss[100];//一行 
	while(gets(ss)!=NULL)
    {
        for(int j=0;ss[j]!='\0';j++)
        {
        	for(int i=0;i<n;i++)
            {
                if(ss[j]==p[i])
				{
					ss[j]=s[i];
					break;
				}//如果需要转换,则用转换后的字母替换原先的字母
            } 
		}     
        puts(ss);//输出这一行的结果
    }

}

Web Navigation

Standard web browsers contain features to move backward and forward among the pages recently visited. One way to implement these features is to use two stacks to keep track of the pages that can be reached by moving backward and forward. In this problem, you are asked to implement this.
The following commands need to be supported:
BACK: Push the current page on the top of the forward stack. Pop the page from the top of the backward stack, making it the new current page. If the backward stack is empty, the command is ignored.
FORWARD: Push the current page on the top of the backward stack. Pop the page from the top of the forward stack, making it the new current page. If the forward stack is empty, the command is ignored.
VISIT : Push the current page on the top of the backward stack, and make the URL specified the new current page. The forward stack is emptied.
QUIT: Quit the browser.
Assume that the browser initially loads the web page at the URL http://www.acm.org/

Input
Input is a sequence of commands. The command keywords BACK, FORWARD, VISIT, and QUIT are all in uppercase. URLs have no whitespace and have at most 70 characters. You may assume that no problem instance requires more than 100 elements in each stack at any time. The end of input is indicated by the QUIT command.

Output
For each command other than QUIT, print the URL of the current page after the command is executed if the command is not ignored. Otherwise, print “Ignored”. The output for each command should be printed on its own line. No output is produced for the QUIT command.

Sample Input
VISIT http://acm.ashland.edu/
VISIT http://acm.baylor.edu/acmicpc/
BACK
BACK
BACK
FORWARD
VISIT http://www.ibm.com/
BACK
BACK
FORWARD
FORWARD
FORWARD
QUIT
Sample Output
http://acm.ashland.edu/
http://acm.baylor.edu/acmicpc/
http://acm.ashland.edu/
http://www.acm.org/
Ignored
http://acm.ashland.edu/
http://www.ibm.com/
http://acm.ashland.edu/
http://www.acm.org/
http://acm.ashland.edu/
http://www.ibm.com/
Ignored

#include<iostream>
#include<stack>
using namespace std;
int main()
{
	stack<string> s1,s2;
	s1.push("http://www.acm.org/");
	while(1)
	{
		string com;//指令
		cin>>com;	
		if(com=="QUIT")
			break;
		if(com=="VISIT")
		{
			string web;
			cin>>web;
			s1.push(web);
			cout<<s1.top()<<endl;
			//插入新页后清空s2 
			while(!s2.empty())
				s2.pop();
		}
		if(com=="BACK")
		{
			if(s1.size()>1)//注意这里判断为不空不对 
			{
				s2.push(s1.top());
				s1.pop();
				cout<<s1.top()<<endl;
				
			}
			else
				cout<<"Ignored"<<endl;
			
		}
		if(com=="FORWARD")
		{
			if(!s2.empty())
			{
				s1.push(s2.top());
				cout<<s2.top()<<endl;
				s2.pop();
			}
			else
				cout<<"Ignored"<<endl;
				
		}
	}
	
}

Perfect Pth Powers

We say that x is a perfect square if, for some integer b, x = b2. Similarly, x is a perfect cube if, for some integer b, x = b3. More generally, x is a perfect pth power if, for some integer b, x = bp. Given an integer x you are to determine the largest p such that x is a perfect pth power.
Each test case is given by a line of input containing x. The value of x will have magnitude at least 2 and be within the range of a (32-bit) int in C, C++, and Java. A line containing 0 follows the last test case.
For each test case, output a line giving the largest integer p such that x is a perfect pth power.
Sample Input
17
1073741824
25
0
Output for Sample Input
1
30
2

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
	
	while(1)
	{
		int x;
		cin>>x;
		if(x==0)
			break;
		if(x>0)
		{
			for(int p=31;p>=1;p--)
			{
				int b=(int)(pow(x,1.0/p)+0.1);
				int n=(int)(pow(b,p)+0.1);
				if(n==x)
				{
					cout<<p<<endl;
					break;
				}
			}
			
		}
		else if(x<0)
		{
			x=-x;
			for(int p=31;p>=1;p=p-2)
			{
				int b=(int)(pow(x,1.0/p)+0.1);
				int n=(int)(pow(b,p)+0.1);
				if(n==x)
				{
					cout<<p<<endl;
					break;
				}
			}
		}
			
				
	}
		
} 

HTML

If you ever tried to read a html document on a Macintosh, you know how hard it is if no Netscape is installed. 
Now, who can forget to install a HTML browser? This is very easy because most of the times you don't need one on a MAC because there is a Acrobate Reader which is native to MAC. But if you ever need one, what do you do? 
Your task is to write a small html-browser. It should only display the content of the input-file and knows only the html commands (tags) <br> which is a linebreak and <hr> which is a horizontal ruler. Then you should treat all tabulators, spaces and newlines as one space and display the resulting text with no more than 80 characters on a line. 
**Input Specification**
The input consists of a text you should display. This text consists of words and HTML tags separated by one or more spaces, tabulators or newlines. 
A word is a sequence of letters, numbers and punctuation. For example, "abc,123" is one word, but "abc, 123" are two words, namely "abc," and "123". A word is always shorter than 81 characters and does not contain any '<' or '>'. All HTML tags are either <br> or <hr>. 
**Output Specification**
You should display the the resulting text using this rules: 
If you read a word in the input and the resulting line does not get longer than 80 chars, print it, else print it on a new line. 
If you read a <br> in the input, start a new line. 
If you read a <hr> in the input, start a new line unless you already are at the beginning of a line, display 80 characters of '-' and start a new line (again). 
The last line is ended by a newline character. 
**Sample Input**

Hallo, dies ist eine 
ziemlich lange Zeile, die in Html
aber nicht umgebrochen wird.
<br>
Zwei <br> <br> produzieren zwei Newlines. 
Es gibt auch noch das tag <hr> was einen Trenner darstellt.
Zwei <hr> <hr> produzieren zwei Horizontal Rulers.
Achtung       mehrere Leerzeichen irritieren

Html genauso wenig wie


mehrere Leerzeilen.

**Sample Output**

Hallo, dies ist eine ziemlich lange Zeile, die in Html aber nicht umgebrochen
wird.
Zwei

produzieren zwei Newlines. Es gibt auch noch das tag
--------------------------------------------------------------------------------
was einen Trenner darstellt. Zwei
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
produzieren zwei Horizontal Rulers. Achtung mehrere Leerzeichen irritieren Html
genauso wenig wie mehrere Leerzeilen.
#include<iostream>
#include<string.h> 
using namespace std;
int main()
{
	string s;
	int index=0;
	while(cin>>s)
	{
		if(s=="<br>")
		{
			index=0;
			cout<<endl;
		}
			
		else if(s=="<hr>")
		{
			if(index)
				cout<<endl<<"--------------------------------------------------------------------------------"<<endl;
			else
				cout<<"--------------------------------------------------------------------------------"<<endl;
			index=0;
		}
		else
		{
			int n=s.length();
			if(index==0)
			{
				index=n;//记录长度
				cout<<s; 
			}
			else if(index+n+1>80)//加上两个之间的空格 长度大于80了,下一个单词另起一行 
			{
				index=n;
				cout<<endl<<s; 
			}
			else
			{
				index=index+n+1;//加上空格 
				cout<<" "<<s;
			}
		}
			
	}
	cout<<endl;

}

猜你喜欢

转载自blog.csdn.net/summer2day/article/details/85230406
ALL