CodingBat 上存在一个名为repeatSeparator的问题。
Given two strings, word and a separator sep, return a big string made of count occurrences of the word, separated by the separator string.
repeatSeparator("Word", "X", 3) ? "WordXWordXWord"
repeatSeparator("This", "And", 2) ? "ThisAndThis"
repeatSeparator("This", "And", 1) ? "This"
Run Code Online (Sandbox Code Playgroud)
我知道如何解决它,但我的解决方案和我在互联网上找到的大多数解决方案都使用循环。有没有办法在没有循环的情况下解决这个问题?
我需要的伪代码return (word+rep)*count;不起作用,但有没有办法实现类似的结果?
我非常重视任何答复。
在 Java、C# 或 PHP 等编程语言中,我们不能使用未初始化的变量。这对我来说很有意义。
C++ dot com规定未初始化的变量在第一次被赋值之前具有不确定的值。但对于整数情况它是 0?
我注意到我们可以在不初始化的情况下使用它,并且编译器不会显示错误并且代码会被执行。
例子:
#include <iostream>
using namespace std;
int main()
{
int a;
char b;
a++; // This works... No error
cout<< a << endl; // Outputs 1
// This is false but also no error...
if(b == '0'){
cout << "equals" << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试用其他语言(例如 C#)复制上述代码,则会出现编译错误。我在官方文档中找不到任何内容。
我非常重视你的帮助。
我正在解决George And Accommodation问题,并提交了两个已接受的代码版本,略有差异。使用的编译器:GNU C++14
版本A (时间:15ms,内存:4kb)
#include <iostream>
using namespace std;
int main(){
int n = 0, p = 0, q = 0, a = 0;
cin >> n;
while(n--){
cin >> p >> q;
if(q - p >= 2) a++;
}
cout << a;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
版本B (时间:15ms,内存:8kb)
#include <iostream>
using namespace std;
int main(){
int n = 0, p = 0, q = 0, a = 0;
cin >> n;
while(n--){
cin >> p >> …Run Code Online (Sandbox Code Playgroud)