我正在尝试解决SPOJ问题"Ones and zero":
某些正整数的十进制表示只包含1和0,并且至少有一个数字1,例如101.如果正整数没有这样的属性,可以尝试将其乘以某个正整数以查明是否该产品具有此属性.
我解决这个问题的方法就是做BFS.取字符串只包含'1'然后用它做BFS并在每一步添加'1'和'0'.到目前为止,以字符串形式和余数跟踪数字.当余数为零时,找到该数字.
我的问题是:我的代码花了太长时间用于测试用例,例如9999或99999.如何改善算法的运行时间?
// Shashank Jain
/*
BFS
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <climits>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#define LL long long int
using namespace std;
LL n;
string ans;
void bfs()
{
string str,first,second;
str+='1'; // number will start with '1' always
if(n==1)
{
ans=str;
return;
}
queue<pair<string,LL> >q; // pair of STRING(number) and long long int …Run Code Online (Sandbox Code Playgroud) #include<stdio.h>
int main()
{
struct test
{
char c;
int y;
float r;
double d;
} t1;
printf("%d\n",sizeof(t1));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:24我的gcc 4.3.2 Ubuntu 12.04
我的解释: 我认为24是对的.如果我错了,请纠正我?
从而 Size = 1(char) + 3(Padded in Char case) + 4(int) + 4(float) + 4(padded for Double) + 8(double) = 24
清晰度:
由于Chunks采用4字节采集.所以每个地址都是4的倍数.对于double,Next地址必须是1012.但它不是8的倍数.所以填充它!并从1016年开始
我的问题 - 24是正确的,我的解释是正确的还是我的解释是错的,请解释一下?
class outer
{
public void outerfunction()
{
private class inner // Line-1
{
public void showinner()
{
System.out.println("I am in Inner Class");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
第1行:此行给出错误.
我知道如果我为课堂内部写抽象或最终.那很好.为什么班内不能私有?
#include<stdio.h>
#include<string.h>
struct node
{
int a;
char *p;
};
int main()
{
struct node X,Y;
char s[5] = "Adam";
char t[5] = "Jack";
X.a = 5;
X.p = s;
Y = X;
Y.a = 10;
strcpy(Y.p,t);
printf("%d %s\n",X.a,X.p);
printf("%d %s\n",Y.a,Y.p);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在这个问题中,结构X有"a = 5"和"P指向亚当".然后将其复制到另一个结构Y.并对Y进行更改.但是当strcpy(Yp,t)完成时.
输出是:
5 Jack
10 Jack
Run Code Online (Sandbox Code Playgroud)
这种变化应该仅在Y中,但这些变化也反映在X.如何?
我的问题是"当一个结构成员被复制时,如何改变另一个结构成员"?