Skip to content

Instantly share code, notes, and snippets.

@yonghanjung
Last active December 20, 2015 01:18
Show Gist options
  • Save yonghanjung/6047656 to your computer and use it in GitHub Desktop.
Save yonghanjung/6047656 to your computer and use it in GitHub Desktop.
Code Study
/* To study Other good Codes
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
char change (int x){
switch(x){
case 1: return '1'; break;
case 2: return '2'; break;
case 3: return '3'; break;
case 4: return '4'; break;
case 5: return '5'; break;
case 6: return '6'; break;
case 7: return '7'; break;
case 8: return '8'; break;
case 9: return '9'; break;
}
}
int main(){
int input[100];
int n= 0; int inputing; int temp;
scanf("%d", &inputing);
/* To store the input by digits, two way. The first is doing in this way,
and the second is use char array, as the char array only one character by each room
*/
for (int i=0; inputing > 0; i++){
input[n++] = inputing % 10;
inputing /= 10;
}
for (int i=0; i<n/2;i++){
temp = input[n-1-i];
input[n-1-i] = input[i];
input[i] = temp;
}
vector<char>vt;
for (int i=0; i<n; i++){
if (input[i] != 0){
vt.push_back(change(input[i]));
}
}
cout << n << endl;
cout << vt.size() << endl;
for (int i =0; i<=n-vt.size();i++){
vt.push_back('0');
}
for (int i=0;i<vt.size();i++){
printf("%c",vt[i]);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
using namespace std;
char input[1000000];
int main()
{
vector<int> vt;
vector<int> vt2;
int diff = 999999999;
fgets(input, 1000000, stdin);
/* This operator is the key to take acceptance of variables as the user wants */
/* --------------------fget -----------------------
char * fgets ( char * str, int num, FILE * stream );
Read chracters from stream ==> stores them into C-string until num-1 character has been read
Either New line reached ==> Then saved what has been read into the string.
str is the adress that where the string read is copied.
fget is used for accepting the stream having the space or tab
fget = cin>>char a[anynum] with space.
-------------------------------------------- */
char *p = strtok(input, " \n\t,");
/* char * strtok ( char * str, const char * delimiters );
it return the pointer that the first group of splited string */
do
{
vt.push_back(atoi(p));
} while (p = strtok(NULL, " \n\t,"));
/* strtok always needs do-while phrase to print the group
When the strtok is declared, from the next time it is OKAY to use NULL instread of "input"
by do-while we can scan whole splited strings
*/
int n;
scanf("%d",&n);
for (int i = 0; i < vt.size(); i++)
{
int dif2 = abs(n - vt[i]);
if (dif2 < diff)
{
vt2.clear();
vt2.push_back(vt[i]); // this is how to input the value into the vector
diff = dif2;
}
else if (dif2 == diff)
{
vt2.push_back(vt[i]);
}
} /* Unconditionally the roop started for the first time
and from the second time, vt2 became the vector with single value or the vector having all the same value.
In both cases, the value will be the biggest difference */
for (int i = 0; i < vt2.size(); i++)
{
printf(i ? " %d" : "%d", vt2[i]);
/* Logical Operator. "Condition ? A:B. True, A. False, B
This commands print out only one variable because it has only a single value
*/
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char *arr=NULL;
char str[100],*a[10];
int i=0,j=0,r=0;
char *t;
fgets(str, sizeof(str), stdin);
arr=strtok(str," ");
while(arr!=NULL)
{
if(r<strlen(arr))
{
t=arr;
r=strlen(arr);
}
a[i]=arr;
arr=strtok(NULL," ");
i++;
}
printf("%s", t);
return 0;
}
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
int main(){
char arrA[100];
char arrB[100];
char temp[100];
cin >> arrA;
cin >> arrB;
if (strlen(arrB) > strlen(arrA)){
strcpy(temp,arrA);
strcpy(arrA,arrB);
strcpy(arrB,temp);
}
int lenA = strlen(arrA),
lenB = strlen(arrB);
int idx = -1;
for ( int i = 0 ; i < lenA-lenB ; i++) {
int match = 1;
for ( int j = 0 ; j < lenB ; j++ )
{
if ( arrA[i+j] != arrB[j] ) {
match = 0;
break;
}
}
if ( match ) {
idx = i;
break;
}
}
//
if ( idx != -1 )
{
for ( int i = 0 ; i < lenA ; i++)
{
if ( i == idx )
{
i += lenB;
}
cout << arrA[i];
}
} else {
cout << arrA;
}
return 0;
}
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;
int main(){
char a[] = "Hello world";
cout << a << endl;
char *p;
p = strtok(a," "); // strtok return the pointer
cout << p << endl; // call whole array
while (p = strtok(NULL, " ")){
cout << p << endl;
}
}
/* This is the code review for remembering how to get the string as an input
in the form of char array */
string input;
getline (cin, input);
#include <iostream>
#include <math.h>
using namespace std;
int n; //집합내 원소들의 총 개수
char* items; //집합을 문자로 저장할 공간
void powerSet(char* aSet, int aSetLen, int current)
{
if(current==n)
{
cout << "{";
for(int i=0; i<aSetLen; i++){
cout << aSet[i] << " ";
}
cout << "}" << endl;
}
else {
current++;
powerSet(aSet, aSetLen, current);
current--;
aSet[aSetLen++]=items[current++];
powerSet(aSet, aSetLen, current);
}
}
int main(int argc, char **argv)
{
cout << "Input the SIZE of Set : " ;
cin >> n;
if(n < 0) {
cout << " size wrong !! " << endl;
return 0;
}
items = new char[n];
for(int i = 0; i < n; i++)
{
cout << "Insert a element - " << i+1 << " : ";
cin >> items[i];
}
char* aSet;
aSet = new char[n];
powerSet(aSet, 0, 0);
cout << "all subsets No : 2^" << n << "=" << pow(2.0,n) << endl;
return 0;
}
#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <conio.h>
using namespace std;
int main() {
char input1[100]; char input2[100]; char seps = '\n';
vector<int>vt;
char *p1; char *p2;
fgets(input1, 100, stdin);
p1 = strtok(input1," "); vt.push_back(atoi(p1));
while (p1 != NULL){
p1 = strtok(NULL, " " " \n ");
vt.push_back(atoi(p1));
}
fgets(input1, 100, stdin);
/* 여기서 중요한 것은 fgets를 어떻게 사용하는가.
왜인지 모르겠으나, fgets 는 배열 하나에 값을 다 받더라.
따라서, fgets를 갱신하는 방식으로 코드를 짰다.
*/
p1 = strtok(input1," "); vt.push_back(atoi(p1));
while (p1 != NULL){
p1 = strtok(NULL, " " " \n ");
vt.push_back(atoi(p1));
}
sort(vt.begin(), vt.end());
for(int i=2; i<vt.size(); i++){
cout << vt[i] << " ";
}
return 0;
}
#include<stdio.h>
#include<algorithm>
int n,a[99],i;
int main(){
while(scanf("%d",&a[n])>0)n++;
/*
1. Maybe it would be better to use scanf instead of cin
2. &a[n]<0 means that if enter put in, it is terminated // 동우야 이거 맞아?
3. We can get the number of input as we want by &a[n]>0
*/
std::sort(a,a+n);
while(i<n)printf("%d ",a[i++]);
}
#include <cstdio>
int main()
{
int n;
scanf("%dF",&n);
/* This is the high tech using scanf. If we want to taled the form of input, the wisest choice
is to use scanf in such a way
*/
printf("%dC",(n-30)/2);
}
#include<stdio.h>
char x,y;
int main(){
while(scanf("%c",&x)>0) /* while (stopping condition) */
if(++y&&x&1)
return 0 & printf("%d",y);
} /* 동우야 이 코드가 지금, X가 들어올 때마다 단계가 하나씩 올라가고 있는거지?
#include <algorithm>
#include <cstdio>
using namespace std;
int data[100], n;
int main(void)
{
scanf("%d", &n);
for(int i=0;i<n;i++) scanf("%d", data + i);
sort(data, data + n);
for(int i=0;i<n;i++) if(data[i] != i + 1) { printf("%d %d\n", i + 1, data[i]); break; }
return 0;
}
#include <cstdio>
int main()
{
int n,k,i,cnt[20000]={0,};
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&k);
cnt[k]++;
}
for(i=1;i<=n;i++) if(!cnt[i]) printf("%d ",i);
for(i=1;i<=n;i++) if(cnt[i]==2) printf("%d",i);
}
#include <iostream>
using namespace std;
int main() {
const int MAX_N = 50;
int list[MAX_N] = {0};
int N, n, val;
int drop = 0, dupl = 0;
cin >> N;
for(n = 1; n <= N; ++n) {
cin >> val;
++list[val];
}
for(n = 1; n <= N; ++n) {
if(list[n] == 0) drop = n;
else if(list[n] == 2) dupl = n;
if(drop && dupl) break;
}
cout << drop << ' ' << dupl << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment