https://cs50.harvard.edu/college/psets/1/

1. Hello

1
2
3
4
5
6
7
8
#include <stdio.h>
#include <cs50.h>
 
int main(void)
{
    string name = get_string("What is your name?\n");
    printf("hello, %s\n", name);
}
 

2a. Mario less

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <cs50.h>
#include <stdio.h>
 
int main(void)
{
    int stairs = get_int("Height: ");
    while(!(stairs >= 1 && stairs <= 8)) stairs = get_int("Height: ");
    for(int i = 0; i < stairs; i++){
        for(int j = stairs; j > i + 1; j--){
            printf(" ");
        }
        
        for(int j = 0; j <= i; j++){
            printf("#");
        }
        printf("\n");
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

2b. Mario more

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <cs50.h>
#include <stdio.h>
 
int main(void)
{
    int stairs = get_int("Height: ");
    while(!(stairs >= 1 && stairs <= 8)) stairs = get_int("Height: ");
    
    for(int i = 0; i < stairs; i++){
        for(int j = stairs; j > i + 1; j--){
            printf(" ");
        }
        
        for(int j = 0; j < i + 1; j++){
            printf("#");
        }
        
        printf("  ");
        
        for(int j = 0; j < i + 1; j++){
            printf("#");
        }
        
        /*for(int j = stairs; j > i + 1; j--){
            printf(" ");
        }*/
        
        printf("\n");
    }
}
 
 

3a. Cash

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
#include <math.h>
#include <cs50.h>
 
int main(void){
    float change = get_float("Change owed: ");
    while(!(change >= 0.00)) change = get_float("Change owed: ");
    
    int coins = round(change * 100);
    
    int count = 0;
    while(coins != 0){
        if(coins >= 25){
            int temp = coins/25;
            coins = coins - (temp * 25);
            count += temp;
        }
        if(coins >= 10){
            int temp = coins/10;
            coins = coins - (temp * 10);
            count += temp;
        }
        if(coins >= 5){
            int temp = coins/5;
            coins = coins - (temp * 5);
            count += temp;
        }
        count += coins;
        coins = 0;
    }
    
    printf("%i\n", count);
}
 
 
 

3b. Credit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <cs50.h>
#include <stdio.h>
 
int main(void){
    long number = get_long("Number: ");
    char *answer = NULL;
    
    if((number/1000000000000 > 1 && (number/1000000000000) % 10 == 4||
      (number/1000000000000000 > 1 && (number/1000000000000000) % 10 == 4)) {
        
        answer = malloc(5 * sizeof(char));
        answer = "VISA";
    }
    else if((number/100000000000000> 1 && 
            ((number/10000000000000) % 100 == 34 || (number/10000000000000) % 100 == 37)){
       
        answer = malloc(5 * sizeof(char));
        answer = "AMEX";
    } 
    else if((number/100000000000000> 1 && 
            ((number/100000000000000) % 100 >= 51 && (number/100000000000000) % 100 <= 55)) {
        
        answer = malloc(10 * sizeof(char));
        answer = "MASTERCARD";
        printf("master?\n");
    } else {
        printf("INVALID\n");
        return 0;
    }
    
    int forSum = 0;
    for(long i = 10; i <= number; i *= 100){
        int divNum = ((number % (i * 10))/i) * 2;
        if(divNum >= 10){
            int temp = divNum % 10;
            divNum /= 10;
            forSum += temp + divNum;
        } else {
            forSum += divNum;
        }
        //printf("%i, %ld forSum, %i\n", forSum, i, divNum);
    }
    
    int restSum = 0;
    for(long i = 10; i <= number * 10; i *= 100){
        int restNum = (number % (i))/(i/10);
        restSum += restNum;
        //printf("%i, %ld restSum, %i\n", restSum, i, restNum);
    }
    if((forSum + restSum) % 10 == 0){
        printf("%s\n", answer);
    } else {
        printf("INVALID\n");
    }
    
}
 
 

'ETC' 카테고리의 다른 글

Algorithms(week3, problemset)  (0) 2020.01.06
Arrays(week 2, problem set)  (0) 2019.12.30
Memory (week3)  (0) 2019.12.27
Arrays and Sorting Algorithms (week2)  (0) 2019.12.26
C Programming Language(week 1)  (0) 2019.12.25

+ Recent posts