Skip to content

Instantly share code, notes, and snippets.

@0xeuclid
Created August 28, 2012 16:26
Show Gist options
  • Select an option

  • Save 0xeuclid/3499961 to your computer and use it in GitHub Desktop.

Select an option

Save 0xeuclid/3499961 to your computer and use it in GitHub Desktop.
interview test
#include<stdio.h>
int main(){
int i = 0 ;
printf("Plz enter a number:") ;
scanf("%d",&i) ;
printf("Base_10: %d\n",i) ;
printf("Base_8: %o\n",i) ;
printf("Base_16: %x\n",i) ;
return 0;
}
#include<stdio.h>
int main(){
float x,y,z ;
scanf("%f %f %f",&x,&y,&z) ;
float tmp ;
if(y>x) tmp=x,x=y,y=tmp;
if(z>y) tmp=y,y=z,z=tmp;
if(y>x) tmp=x,x=y,y=tmp;
printf("%f > %f > %f\n",x,y,z) ;
return 0;
}
#include<stdio.h>
int f(int n){
if(n==1) return 0;
if(n==2) return 1;
return f(n-1) + f(n-2) ;
}
int main(int argc, char *argv[]){
if(argc != 2){
printf("Usage: ./fabonacci <Nth>\n") ;
exit(0) ;
}
int n = atoi(argv[1]);
printf("%d\n", f(n)) ;
return 0;
}
#include<iostream>
#include<cstdio>
using namespace std;
typedef struct linked_list {
int a ;
struct linked_list *pnext ;
} list ;
int main(){
list l1,l2,l3,l4 ;
l1.a = 1, l1.pnext = &l2 ;
l2.a = 2, l2.pnext = &l3 ;
l3.a = 3, l3.pnext = &l4 ;
l4.a = 4, l4.pnext = NULL ;
list *ptr = &l1 ;
while(ptr){
printf("Value: %d\n", ptr->a) ;
ptr = ptr->pnext ;
}
return 0;
}
#include<iostream>
using namespace std;
class A {
public:
A(){ cout << "A was borned." << endl ;}
~A(){ cout << "A was died." << endl ;}
virtual void print(){ cout << "A's print()" << endl ;}
void func(){ cout << "A's func" << endl ;}
};
class B : public A {
public:
B(){ cout << "B was borned." << endl ;}
~B(){ cout << "B was died." << endl ;}
virtual void print(){ cout << "B's print()" << endl ;}
void func(){ cout << "B's func" << endl ;}
};
class C : public B {
public:
C(){ cout << "C was borned." << endl ;}
~C(){ cout << "C was died." << endl ;}
virtual void print(){ cout << "C's print()" << endl ;}
void func(){ cout << "C's func" << endl ;}
};
int main(){
C c ;
B *b = (B*) &c ;
A *a = (A*) &c ;
b->print() ;
b->func() ;
a->print() ;
a->func() ;
return 0;
}
#include<iostream>
#include<cstdio>
using namespace std;
class Base {
protected:
int a,b,c ;
public:
Base(){
a=1;b=2;c=3;
}
int getA(){ return a; }
virtual int getB(){ return b; }
virtual int getC() = 0 ;
};
class Child : public Base {
protected:
int a,b,c ;
public:
Child(){
a=4;b=5;c=6;
}
int getA(){ return a; }
int getB(){ return b; }
int getC(){ return c; }
};
int main(){
Child o ;
Base *p = (Base*) &o ;
int i1,i2,i3 ;
i1 = p->getA() ;
i2 = p->getB() ;
i3 = p->getC() ;
printf("%d %d %d\n",i1,i2,i3) ;
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
srand(time(NULL)) ;
int i=0;
int j=0;
for(i=0;i!=6;++i){
j = (rand()%100000)+1 ;
printf("random number: %d\n",j) ;
}
return 0;
}
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std ;
int main(){
vector<int> v[10];
random_shuffle(v.begin(),v.end());
for(int i=0;i!=10;++i){
cout << v[0] << endl ;
rotate(v.begin(),v.begin()+1,v.end()) ;
}
return 0;
}
#include<stdio.h>
#include<string.h>
//how to reverse
void reverse(string &s){
int len = s.length() ;
string tmp(s) ;
for(int i=0; i!=len; ++i){
s[i] = tmp[ len-(i+1)] ;
}
}
int main(){
char result[100] ;
char hello[] = "Hello" ;
char world[] = " World" ;
strcat(result,hello) ;
strcat(result,world) ;
puts(result) ;
return 0;
}
#include<iostream>
#include<cstdio>
using namespace std;
void swap(int &a, int &b){
int tmp = a ;
a = b ;
b = tmp ;
}
int main(){
int x=1, y=2 ;
printf("x=%d, y=%d\n",x,y) ;
swap(x,y) ;
printf("x=%d, y=%d\n",x,y) ;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment