【cs50 2022】lab1 && problem set1
|lab1|
#include <cs50.h> #include <stdio.h> int main(void) { // TODO: Prompt for start size int start_size; do{ start_size = get_int("Start size: "); } while(start_size<9); // TODO: Prompt for end size int end_size; do{ end_size = get_int("End size: "); }while(end_size < start_size); // TODO: Calculate number of years until we reach threshold int year = 0; int born = 0; int pass = 0; while(start_size < end_size){ born = start_size / 3; pass = start_size / 4; start_size = start_size + born - pass; year++; } // TODO: Print number of years printf("Years: %i\n",year); }
|problem set1|
#include <cs50.h> #include <stdio.h> int main(void) { int height = 0; do{ height = get_int("Height: "); }while(height < 1 || height > 8); int n = 1; for(int i = 0;i < height;i++){ for(int j = 0;j<height-n;j++){ printf(" "); } for(int m=0;m<n;m++){ printf("#"); } n++; printf("\n"); } }
#include <cs50.h> #include <stdio.h> int main(void) { int height = 0; do{ height = get_int("Height: "); }while(height < 1 || height > 8); int n = 0; for(int i=0;i<height;i++){ for(int j=0;j<height-n;j++){ printf(" "); } for(int m=0;m<n;m++){ printf("#"); } printf(" "); for(int k=0;k<n;k++){ printf("#"); } n++; printf("\n"); } }
cash
#include <cs50.h> #include <stdio.h> #include <math.h> int get_cents(void); int calculate_quarters(int cents); int calculate_dimes(int cents); int calculate_nickels(int cents); int calculate_pennies(int cents); int main(void) { // Ask how many cents the customer is owed int cents = get_cents(); // Calculate the number of quarters to give the customer int quarters = calculate_quarters(cents); cents = cents - quarters * 25; // Calculate the number of dimes to give the customer int dimes = calculate_dimes(cents); cents = cents - dimes * 10; // Calculate the number of nickels to give the customer int nickels = calculate_nickels(cents); cents = cents - nickels * 5; // Calculate the number of pennies to give the customer int pennies = calculate_pennies(cents); cents = cents - pennies * 1; // Sum coins int coins = quarters + dimes + nickels + pennies; // Print total number of coins to give the customer printf("%i\n", coins); } int get_cents(void) { int cents = 0; do{ cents = get_int("Change owed: "); }while(cents < 0); // TODO return cents; } int calculate_quarters(int cents) { // TODO return cents / 25; } int calculate_dimes(int cents) { // TODO return cents / 10; } int calculate_nickels(int cents) { // TODO return cents / 5; } int calculate_pennies(int cents) { // TODO return cents; }
credit
#include <cs50.h> #include <stdio.h> #include <math.h> void show_result(long number); int main(void) { long number = 0; do{ number = get_long("Number: "); }while(number<0); show_result(number); } void show_result(long number){ int len = 0; long count_number = number; while(count_number>0){ len++; count_number /=10; } //printf("lenth:%i\n",len); int card[20]; long sum_number = number; // printf("show card\n"); for(int i=len-1;i>0;i--){ card[i] = sum_number %10; sum_number /= 10; } card[0] = sum_number; /*for(int i=0;i<len;i++){ printf("%i",card[i]); }*/ // printf("\n"); int sum = 0; for(int i=0;i<len;i+=2){ card[i]*=2; if(card[i]>9){ sum += card[i] % 10; sum += card[i] / 10; } else{ sum += card[i]; } } for(int i=1;i<len;i+=2){ sum += card[i]; } if(sum % 10 == 0){ printf("VISA\n"); }else{ printf("INVALID\n"); } }