如何使这个程序工作? [英] How do I make this program work?

查看:51
本文介绍了如何使这个程序工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,我已经在家庭作业中获得了一个程序而且我已经尝试过解决它,但是我编写的程序并不能完成所有工作。



这是节目:



Temple Land



蛇想要为眼镜王蛇建造一座寺庙。他们正在寻找多条土地,但并非所有土地都适合。他们需要这片土地类似于盘绕的眼镜蛇。你需要找出哪些条带这样做。



正式地说,每一片土地都有一个长度。假设第i个条带的长度是Ni,那么将有Ni整数,Hi1,Hi2,... HiNi,它们按顺序表示条带各部分的地面高度。也就是说,将条带分成Ni部分并给出每个部分的高度。当且仅当满足所有这些条件时,此条带才有效:



- 应该有一个独特的中心部分。这是建造真正的寺庙的地方。按中心,我们的意思是该部分左侧和该部分右侧应该有相同数量的部分。

- Hi1 = 1

- 当你从最左边的部分移动到中间部分时,高度保持正好增加1。

- 当你从中间部分移动到最右边的部分时,高度应该保持正好减少1 。请注意,这意味着HiNi也应该是1.





你的工作是查看每个条带并查找它是否有效。





输入:



- 第一行包含单个整数,S,这是您需要查看的条带数。每个S条带的描述如下:

- 第i个条带描述的第一行将包含一个整数:Ni,它是分成的部分的长度和数量。

- 下一行包含Ni整数:Hi1,Hi2,..,HiNi。这些代表了第i个条带中各个部分的高度。





输出:

- 对于每个条带,在新行中,如果是有效条带,则输出是,如果不是,则输出否。





限制条件:



1≤S≤100

3≤Ni≤100

1≤ Hij≤100





例子



输入:

7

5

1 2 3 2 1

7

2 3 4 5 4 3 2

5

1 2 3 4 3

5

1 3 5 3 1

7

1 2 3 4 3 2 1

4

1 2 3 2

4

1 2 2 1







输出:



















说明

在第一个条带中,所有条件都是满意,因此它是有效的。



在t第二个条带,它不是以1开头,因此无效。



在第三个条带中,它不断增加甚至超过中心,而不是减少。因此无效。



第四条不会正确地增加和减少1.因此无效。



第五个满足所有条件,因此是有效的。



第六个和第七个条带没有中心部分。因为对于每个部件,其右侧比左侧更多,或者左侧比右侧更多。因此这两个条都是无效的。



我尝试过:



这就是我所做的:

Hey guys i have been given a program in homework and i've tried solving it but the program that i've written doesn't do the full job.

Here's the Program:

Temple Land

The snakes want to build a temple for Lord Cobra. There are multiple strips of land that they are looking at, but not all of them are suitable. They need the strip of land to resemble a coiled Cobra. You need to find out which strips do so.

Formally, every strip of land, has a length. Suppose the length of the i-th strip is is Ni, then there will be Ni integers, Hi1, Hi2, .. HiNi, which represent the heights of the ground at various parts of the strip, in sequential order. That is, the strip has been divided into Ni parts and the height of each part is given. This strip is valid, if and only if all these conditions are satisfied:

- There should be an unique 'centre' part. This is where the actual temple will be built. By centre, we mean that there should be an equal number of parts to the left of this part, and to the right of this part.
- Hi1 = 1
- The heights keep increasing by exactly 1, as you move from the leftmost part, to the centre part.
- The heights should keep decreasing by exactly 1, as you move from the centre part to the rightmost part. Note that this means that HiNi should also be 1.


Your job is to look at every strip and find if it's valid or not.


Input:

- The first line contains a single integer, S, which is the number of strips you need to look at. The description of each of the S strips follows
- The first line of the i-th strip's description will contain a single integer: Ni, which is the length and number of parts into which it has been divided.
- The next line contains Ni integers: Hi1, Hi2, .., HiNi. These represent the heights of the various parts in the i-th strip.


Output:
- For each strip, in a new line, output "yes" if is a valid strip, and "no", if it isn't.


Constraints:

1 ≤ S ≤ 100
3 ≤ Ni ≤ 100
1 ≤ Hij ≤ 100


Example

Input:
7
5
1 2 3 2 1
7
2 3 4 5 4 3 2
5
1 2 3 4 3
5
1 3 5 3 1
7
1 2 3 4 3 2 1
4
1 2 3 2
4
1 2 2 1



Output:
yes
no
no
no
yes
no
no


Explanation
In the first strip, all the conditions are satisfied, hence it is valid.

In the second strip, it does not start with a 1, and hence is invalid.

In the third strip, it keeps increasing even past the centre, instead of decreasing. Hence invalid.

The fourth strip does not increase and decrease by exactly 1. Hence invalid.

The fifth satisfies all conditions and hence is valid.

The sixth and seventh strip do not have a 'centre' part. Because for every part, there are either more parts to its right than its left, or more parts on its left than its right. Hence both the strips are invalid.

What I have tried:

Here's what i've done:

#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

void reverseStr(string &str)
{
    int n = str.length();
    for (int i=0; i<n/2; i++)
       swap(str[i], str[n-i-1]);
}


int main(){
	int num;
	cin>>num;
	if(num>=1 && num<=100){
		while(num!=0){
			int n;
			cin>>n;
			if(n>=3 && n<=100){
				string s,r="",str="";
				cin.ignore(); 
				getline(cin, s);
				if(s.at(0) == '1' && s.at(s.length()-1) == '1'){
					for(int j=0;j<s.length();j++){
						char ch = s.at(j);
						if(ch != ' '){
							r=r+ch;
						}
					}
					int len = r.length();
					if(n!=len){
						cout<<"no"<<endl;
						break;
					}
					str = r;
					reverseStr(str);
					if(r.compare(str) == 0){
						cout<<"yes"<<endl;
					}else{
						cout<<"no"<<endl;
					}
				}else{
					cout<<"no"<<endl;
				}
			}else{
				cout<<"no"<<endl;
			}
			num--;
		}
	}else{
		cout<<"no"<<endl;
	}
	return 0;
}



但它只是看0-9的数字。我怎样才能使这个程序为两位数


But it is only looking at digits of 0-9. How can i make this program for two digits also

推荐答案

如果你需要使用代表两位数的字符串你必须使用substr 功能,您可以使用比较



if you need to work with string which represent double digits you must use the substr function and you can validate with compare.

string str = "1234";
string str2 = str.substr(1,2); 
if (str2.compare("23") == 0) {
 //we found the correct sub string
}





如你所见字符串类有引擎盖下的一些东西。因此,阅读课程文档并搜索一些示例代码是个好主意。



提示:制作不同的无输出,以便更好地区分案例。最好不要重复错误信息



祝你好作业。



As you see the string class has some goodies under the hood. So it is a good idea to read the class documentation and search for some sample code.

Tip: make different "no" outputs, to better distinguish the cases. It is best practise to NEVER repeat error messages

Good luck with your homework.


这篇关于如何使这个程序工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆