fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int solve(int,int);
  5. int solve(int n,int m){
  6. for(int i = 0;i <= n;i++){
  7. if( 4 * i + 2 * ( n - i ) == m ){
  8. return i;
  9. }
  10. }
  11. return -1;
  12.  
  13. }
  14.  
  15. int main(){
  16. int n;
  17. int m;
  18. int count = 0;
  19.  
  20. /* 文字列関係はWikipediaのfgetsの項目を参考にしました。*/
  21. char temp[128];
  22. printf("input n:");
  23. fgets(temp, 128, stdin);
  24. fflush(stdin);
  25. if (sscanf(temp, "%d", &n) != 1) {
  26. return EXIT_FAILURE;
  27. }
  28.  
  29. printf("input m:");
  30. fgets(temp, 128, stdin);
  31. fflush(stdin);
  32. if (sscanf(temp, "%d", &m) != 1) {
  33. return EXIT_FAILURE;
  34. }
  35.  
  36. count = solve(n,m);
  37. if(count < 0){
  38. printf("解なし\n");
  39. return EXIT_SUCCESS;
  40. }
  41.  
  42. printf("鶴:%d\n",count);
  43. printf("亀:%d\n",n - count);
  44.  
  45.  
  46. }
  47.  
  48.  
Success #stdin #stdout 0s 1724KB
stdin
8
26
stdout
input n:input m:鶴:5
亀:3