这是一个面试问题: -
编写一个C程序,在编译和运行时,打印出一条消息,指示编译它的编译器是否允许嵌套/**/comments.
这个问题的解决方案如下: -
Sol: - 你可以有一个整数变量nest:
int nest = /*/*/0*/**/1;
Run Code Online (Sandbox Code Playgroud)
如果它支持嵌套注释,则答案为1,否则答案为0.
这是怎么回事?我不明白变量声明.
给定一个元素数组,找到可以通过使用数组元素形成的最大可能数.
例如:10 9
ans:910
2 3 5 78
ans:78532
100 9
ans:9100
我知道这个问题有一个使用自定义字符串比较器的解决方案,但我不明白它是如何工作的.
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
bool compare ( string a, string b )
{
return atoi( (a+b).c_str() ) < atoi((b+a).c_str() );
}
int main()
{
vector<string> vs;
string s;
while ( cin >> s ) {
vs.push_back(s);
}
sort( vs.begin(), vs.end(), compare );
for ( int i = vs.size()-1; i >= 0; i-- ) {
cout << vs[i];
}
}
Run Code Online (Sandbox Code Playgroud)
谁能提出算法来解决这个问题?将理解上述比较器的说明.谢谢
我已经了解到,当我们将数组名称传递给sizeof时,数组的名称不会衰减到指向基址的指针.下面的代码通过给出答案10来验证这一事实.
#include <stdio.h>
int main(){
int arr[10];
printf("Size of array is %d" , sizeof(arr)/sizeof(int));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
然而,当我运行下面的代码时,答案是1.无论维度是否是原型编写,答案都是1.为什么会这样?
#include <stdio.h>
void dimension(int arr[]){
printf("Sizof array is %d" , sizeof(arr)/sizeof(int));
}
int main(){
int arr[10];
dimension(arr);
return 0;
}
Run Code Online (Sandbox Code Playgroud) public class Car
{
public char color;
public char getColor()
{
return color;
}
public void setColor(char color)
{
this.color = color;
}
}
public class MyCar
{
private Car car = null;
public MyCar()
{
this.car = new Car();
car.color = 'R';
}
}
Run Code Online (Sandbox Code Playgroud)
上述代码违反了哪个OOPS原则?
•抽象•封装•多态性•以上都不是
我知道Encapsulation是这个问题的答案.只是想知道其他选项是否也是如此.
考虑笛卡尔坐标系中的点P(n,n).机器人必须从原点开始并到达这一点.机器人可以采取的唯一步骤是:
机器人可以将多少条不同的路径指向P?
是否有指向P的最佳路径?(向上和向右步骤都会产生相同的成本).
#include <stdio>
int main(){
int x = 4;
int y = 3;
int z;
z = x---y;
printf("%d" , z);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Linux Mandriva中的gcc编译器将其评估为(x--)-y.我很困惑为什么会这样.它可能是x - (--y).
我知道有些答案会告诉我查看优先级表.我已经完成了所有这些,但仍然存在疑问.
请任何人澄清一下.
当我遇到这段代码时,我正试图在C中学习字符串.
#include <stdio.h>
int main(){
char s[] = "Hello world";
printf("%s" , s);
printf("%s" , &s);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
两者都给出了Hello World作为输出.根据我的理解,这个输出对于第一种情况是好的.如何为第二个工作?请澄清.