Sample Code HW2

Back to Computer Programming (I)

tags: NTNU CSIE 必修 Programming(I)

hw0201

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Author: 蕭瀜

#include <stdio.h> #include <stdint.h> int main(){ int32_t a, b, c, tmp; scanf("%d %d %d", &a, &b, &c); if(a < b){ tmp = a; a = b; b = tmp; } if(a < c){ tmp = a; a = c; c = tmp; } if(a*a < b*b + c*c) printf("1\n"); else if(a*a == b*b + c*c) printf("2\n"); else if(a*a > b*b + c*c) printf("3\n"); }

hw0202

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Author: 蕭瀜

#include <stdio.h> #include <stdint.h> int main(){ int32_t d = 0, t = 0, night = 0, CNY = 0, money = 70; printf("Please enter the taxi driving distance (meter): "); scanf("%d", &d); printf("Please enter the taxi stopping time (second): "); scanf("%d", &t); printf("Is nighttime or not (0: No, 1: Yes): "); scanf("%d", &night); printf("Is Chinese New Year or not (0: No, 1: Yes): "); scanf("%d", &CNY); if(d > 1250) money += (d - 1250) / 250 * 5; money += t / 100 * 5 + night * 20 + CNY * 20; printf("Taxi fare: %d\n", money); }

Author: 高仔

#include <stdio.h> #include <stdint.h> int main() { float dis=0.0, sec=0.0; int32_t night=2, special=2, ans=0; printf("Please enter the taxi driving distance (meter): "); scanf("%f",&dis); printf("Please enter the taxi stopping time (second): "); scanf("%f",&sec); while(1) { printf("Is nighttime or not (0: No, 1: Yes): "); scanf("%d",&night); if( night == 0 || night == 1 ) break; else printf("Use 1 or 0 answer pls.\n"); } while(1) { printf("Is Chinese New Year or not (0: No, 1: Yes): "); scanf("%d",&special); if( special == 0 || special == 1 ) break; else printf("Use 1 or 0 answer pls.\n"); } if( dis <= 1250 ) ans = 70 + 5 * (sec / 100); else ans = 70 + 5 * (sec / 100) + (dis - 1250) / 250 * 5 ; if( night ) ans += 20 ; if( special ) ans += 20 ; printf("Taxi fare: %d\n",ans); return 0; }

hw0203

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Author: 蕭瀜

#include <stdio.h> #include <stdint.h> int main(){ int32_t state = 0, input = 0, i = 0; printf("Please enter 5 integers: "); for(i = 0; i < 5; i++){ scanf("%d", &input); switch(state){ case 0: if(input == 10 || input == 35) state = 1; else if(input == 11) state = 3; else if(input == 20 || input == 78) state = 5; break; case 1: if(input == 19) state = 2; else if(input == 12 || input == 36) state = 6; break; case 2: if(input == 43) state = 2; else if(input == 99) state = 7; else state = 0; break; case 3: state = 4; break; case 4: state = 6; break; case 5: if(input == 1) state = 4; else if(input == 2) state = 6; else state = 0; break; case 6: if(input == 108) state = 7; else state = 5; break; case 7: break; } } if(state == 0) printf("The user is in the first state.\n"); else if(state == 7) printf("The user is in the final state.\n"); else printf("The user is in the S%d state.", state); }

幹勒我寫的跟他一樣 高仔

hw0204

Author: 蕭瀜

#include <stdio.h> #include <stdint.h> int main(){ int32_t input = 0, sum = 0, cnt = 0, i = 0; printf("Please enter three cards: "); for(i = 0; i < 3; i++){ scanf("%d", &input); input = (input - 1) % 13 + 1; if(input == 1){ cnt++; sum += 11; } else if(input > 10) sum += 10; else sum += input; } while(sum > 21 || input == 0){ sum -= 10; input--; } printf("%d\n", sum); }

Author: 高仔

#include <stdio.h> #include <stdint.h> int main() { int32_t card[3], num=21, ace=0; printf("Please enter three cards: "); scanf("%d%d%d", &card[0], &card[1], &card[2]); for( int i = 0 ; i < 3 ; i++ ) { if( card[i] % 13 == 11 || card[i] % 13 == 12 || card[i] % 13 == 0 ) num -= 10; else if ( card[i] % 13 == 1) { num -= 1; ace++; } else num -= ( card[i] % 13 ); } if( num < 0 ) printf("bust\n"); else { if( ace != 0 && num >= 10 ) { for( ; ace > 0 ; ace-- ) { if( num >= 10 ) num -= 10 ; } } printf("%d\n", 21 - num ); } return 0; }

hw0205


Author: 蕭瀜

#include <stdio.h> #include <stdint.h> int main(){ int32_t card = 0, number[13] = {0}, suit[4] = {0}, find[2] = {0}; int32_t i = 0, j = 0, Ccnt = 0, cnt = 0, flush = 0; printf("Please enter 5 cards: "); for(i = 0; i < 5; i++){ scanf("%d", &card); // input 5 card if(card > 52 || card < 1){ // check wrong input printf("Wrong input\n"); return 0; } number[(card - 1) % 13]++; // count same number suit[(card - 1) / 13]++; // count same suit } if(suit[(card - 1) / 13] == 5) flush = 1; // if last card suit count == 5 for(i = 0; i < 13; i++){ if(number[i] && number[(i + 1) % 13]) Ccnt++; // check current number & next number both have card if(number[i] > 1) find[cnt++] = number[i]; // if same number more than 2 put in find } if(find[0] < find[1]){ // make find[0] >= find[1] i = find[0]; find[0] = find[1]; find[1] = i; } if(flush && Ccnt == 4) printf("Straight flush\n"); // print answer else if(find[0] == 4) printf("Four of a kind\n"); else if(find[0] == 3 && find[1] == 2) printf("Full house\n"); else if(flush == 1) printf("Flush\n"); else if(Ccnt == 4) printf("Staight\n"); else if(find[0] == 3) printf("Three of a kind\n"); else if(find[0] == 2 && find[1] == 2) printf("Two pair\n"); else if(find[0] == 2) printf("One pair\n"); else printf("High card\n"); }