• ベストアンサー

Yesならこっちへ、NOならあっちへ(c++)

noname#30727の回答

  • ベストアンサー
noname#30727
noname#30727
回答No.2

おそらく入力そのものは、この手の課題としては重要ではないと思うので、例えば、一行単位で入力させて、最初の1文字で判断するなどの簡単な方法でもいいと思います。 char input[256]; int ans; for (ans = 0; ans == 0; ) { cout<<" y は YES, n は NO :" << endl; cin >> input; if (input[0] == 'y') { ans = 1; } else if (input[0] == 'n') { ans = 2; } } cherry_moon さんも書かれていますが、「1から100までの整数をUserに選ばせ」というのは、心の中で選ばせるという意味ですよね? 最初に数を入力させてしまったら、1回目で当たって当然と誰もが思うはずです。 書かれているプログラムを見ると、何度も mid より大きい? という部分がありますが、これは1箇所だけで済むように作れるはずです。あとは、min == 33, max == 34 などのときに、永遠に 33 以上ですか?と聞かないように工夫するくらいです。

関連するQ&A

  • C++の関数等を使った書き方

    C++OpenCVで関数等を使ってプログラムをみやすくまとめたいのですが、教えていただけないでしょうか。 プログラムの内容はHSV変換をしています。 これをmain文より外に出したい場合どのような書き方をすればいいでしょうか。 返り値はHSVの3つがほしいです。 struct _HSV { int H; int S; int V; }HSV[2000][2000]; main(void){ 宣言 src_img = cvLoadImage (filename, CV_LOAD_IMAGE_COLOR); for(y = 0; y < src_img->height; y++) { for(x = 0; x < src_img->width; x++) { p[0] = src_img->imageData[src_img->widthStep * y + x * 3]; // B p[1] = src_img->imageData[src_img->widthStep * y + x * 3 + 1]; // G p[2] = src_img->imageData[src_img->widthStep * y + x * 3 + 2]; // R if (p[0] >= p[1] && p[0] >= p[2]) {max = p[0];} //Bがmax else if (p[1] >= p[0] && p[1] >= p[2]) {max = p[1];} //Gがmax else {max = p[2];} //Rがmax if (p[0] <= p[1] && p[0] <= p[2]) {min = p[0];} //Bがmin else if (p[1] <= p[0] && p[1] <= p[2]) {min = p[1];} //Gがmin else {min = p[2];} //Rがmin if(max==min){ HSV[y][x].H = 0; HSV[y][x].S = 0; HSV[y][x].V = p[0]; //R } else { if(max==p[2]){ //Rがmax HSV[y][x].H = 60 * (p[1]-p[0])/(max-min); } else if(max==p[1]) //Gがmax { HSV[y][x].H = 60 * (p[0]-p[2])/(max-min); HSV[y][x].H += 120; } else //Bがmax { HSV[y][x].H = 60 * (p[2]-p[1])/(max-min); HSV[y][x].H += 240; } if(HSV[y][x].H < 0) { HSV[y][x].H += 360; } } if(x % 60== 0){ fprintf(fp, "%d ,%d\n", HSV[y][x].H, HSV[y][x].S); } } } } 初歩的な質問ですみません。 できれば、関数以外でもこうすれば見やすい等ご指摘頂けると助かります。 よろしくお願いします。

  • 最大値(c++)

    3つの数の最大値をArrayを使って求めるProgramを書いてます。何とかそれっぽくできたのですが、実行すると、一番初めに入れた数が最大値であると、表示されてしまいます。 なにが問題なのでしょうか? #include<iostream.h> int max(int n[], int size){ int max= n[0]; for (int i=1; i>size; i++) if(n[i]>max) max= n[i]; return max; } int main(){ int a,b,c; cout<<" 3つの数の最大値を求めます。順に3つの数字をいれてください。"<<endl; cin>>a>>b>>c; int d[]={ a,b,c }; cout<<"max :"<<max(d,sizeof d/ sizeof d[0])<<endl; return 0; }

  • VC++でプログラムの勉強をしています。

    プログラムは最近はじめたばかりです。While文とif文を使ってクイズを作ってみたところ、一個目のsinで入力を求めているところから無限ループになってしまいました。色々調べてcin.cler()とsin.ignore()を入れたりもしてみましたが上手くいきませんでした。どこを間違えているのでしょうか? //クイズ #include <iostream> using namespace std; int main()//cin.clear();cin.ignore();???? { int a; int b; while(1) { cout<<"ネコ型のロボットが出てくるアニメといえば?"<<endl; cout<<"A)ドラえもん B)ドラエもん C)ほりえもん D)サザエさん"<<endl; cin>>a; if(a=='A') { cout<<"ファイナルアンサー?"<<endl; cout<<"Y)Yes N)NO"<<endl; cin>>b; if(b=='Y'){break;} if(b=='N'){cout<<"ゆっくり考えてね!!"<<endl;} if(b!='Y'||'N'){cout<<"正しく入力してね!"<<endl;} } if(a=='B'||'C'||'D') { cout<<"ファイナルアンサー?"<<endl; cout<<"Y)Yes N)NO"<<endl; cin>>b; if(b=='Y'){cout<<"残念!!"<<endl;} if(b=='N'){cout<<"ゆっくり考えてね!!"<<endl;} } if(a!='A'||'B'||'C'||'D'){cout<<"正しく入力してね!"<<endl;} } cout<<"正解!!"<<endl; }

  • int型変数の簡潔なプログラム

    #include<iostream> using namespace std; int main(void){ int max = a; int min = a; if(a > b){ min = b; }else{ max = b; } cout << "小さい方の値は" << min << "です。\n"; cout << "大きい方の値は" << max << "です。\n"; } これの、    int max = a; int min = a; と     if(a > b){ min = b; }else{ max = b; } が解りません。 何故変数をaからbにチェンジしているのでしょうか 初心者なのでお手柔らかにお願いします。 よろしくお願いします。

  • プログラムの出力の説明を教えてください。

    出力が、画像のようになるのですが、最後に基本クラスのoperator=()が呼ばれるのがわからないです。 なぜ、そのようになるのか教えてください。 /* +,-,=をcoordクラスに関してオーバーロードし、 そのcoordをquadの基本クラスとして使用する */ #include <iostream> using namespace std; class coord{ public: int x, y; //座標 coord(int i = 0, int j = 0){ x = i; y = j; } void get_xy(int &i, int &j){ i = x; j = y; } coord operator+(coord ob2); coord operator-(coord ob2); coord operator=(coord ob2); }; //+をcoordクラスに関してオーバーロードする coord coord::operator+(coord ob2) { coord temp; cout << "coord operator+()を使用" << endl; temp.x = x + ob2.x; temp.y = y + ob2.y; return temp; } //-をcoordクラスに関してオーバーロードする coord coord::operator-(coord ob2) { coord temp; cout << "coord operator-()を使用" << endl; temp.x = x - ob2.x; temp.y = y - ob2.y; } //=をcoordクラスに関してオーバーロードする coord coord::operator=(coord ob2) { cout << "coord operator=()を使用" << endl; x = ob2.x; y = ob2.y; return *this; //代入先のオブジェクトを返す } class quad :public coord{ int quadrant; public: quad(){ x = 0; y = 0; quadrant = 0; } quad(int x, int y) :coord(x, y) { if (x >= 0 && y >= 0)quadrant = 1; else if (x<0 && y >= 0)quadrant = 2; else if (x<0 && y<0)quadrant = 3; else quadrant = 4; } void showq() { cout << "象限を示す:" << quadrant << endl; } quad operator=(coord ob2); }; quad quad::operator=(coord ob2) { cout << "quad operator=()を使用" << endl; x = ob2.x; y = ob2.y; if (x >= 0 && y >= 0)quadrant = 1; else if (x<0 && y >= 0)quadrant = 2; else if (x<0 && y<0)quadrant = 3; else quadrant = 4; return *this; } int main() { quad o1(10, 10), o2(15, 3), o3; int x, y; o3 = o1 + o2; //2つのオブジェクトの加算。演算子+()を呼ぶ o3.get_xy(x, y); o3.showq(); cout << "(o1+o2)X:" << x << ",Y:" << y << endl; o3 = o1 - o2; //2つのオブジェクトの減算 o3.get_xy(x, y); o3.showq(); cout << "(o1-o2)X:" << x << ",Y:" << y << endl; o3 = o1; //オブジェクトを代入する o3.get_xy(x, y); o3.showq(); cout << "(o3=o1)X:" << x << ",Y:" << y << endl; return 0; }

  • コマンドライン引数

    下記のリストの結果は自分の期待では「真」なのですが 「偽」となります。 なぜなのかわけが分からなくなって質問させて頂きまし た。 (なお後尾の2行はチェックするために入れただけです。) どなたかご教示願えませんでしょうか。 よろしくお願いします。 リスト #include <iostream> using namespace std; int main(int argc, char *argv[]) { if( argv[1] == "yes") { cout << "真です。" << endl; } else { cout << "偽です。" << endl; } cout << " argv[1] は " << argv[1] << " です。" << endl; cout << " argv[2] は " << argv[2] << " です。" << endl; }

  • C++の無限ループを解決してください

    アルゴリズムを勉強するときに以下のソースを書きました; void weighted_quick_union_algorithm() { static const int volume = 10; enum status { terminate_, union_, find_ }; string str; status sta; vector<int> system(volume, 0); vector<int> size(volume, 1); for (int index = 0; index != volume; ++index) { system[index] = index; } do { cout<<"cin"<<endl; cin >> str; for (string::size_type index = 0; index != str.size(); ++index) str[index] = toupper(str[index]); if (str == "UNION") sta = union_; else if (str == "FIND") sta = find_; else if (str == "TERMINATE") sta = terminate_; switch (sta) { case(0): { cout << str << endl; break; } case(1): { cout << str << sta << endl; int p(0), q(0), i(0), j(0); while (cin >> p) { cin >> q; for (i = p; i != system[i]; i = system[i]); for (j = q; j != system[j]; j = system[j]); if (i == j) continue; if (size[i] < size[j]) { system[i] = j; size[j] += size[i]; } else { system[j] = i; size[i] += size[j]; } cout << p << " - " << q << endl; } cout<<"break"<<endl; break; } case(2): { cout << str << sta << endl; break; } } } while (sta); } しかし unionを入力しあと ; でwhile(cin>>p)をブレイクしたら cin break UNION1 cin break Union1 で無限ループ 結構時間かかったが間違いがわかりません ちなみに最少は while(cin>>p>>q)と書いていましたが同じ結果です。 どうかお願いします

  • C++

    ICPCの過去問です。 問題文:http://www.deqnotes.net/acmicpc/p0003/ja 過去問を解いたのですが実行が上手くいかずに困っています。 私の書いたソースコード: #include <iostream> #include <string> using namespace std; class Line{ public: char* Read(){ char number[1024];//数字を入れる配列 static char sentence[76];//できあがった文字列を入れる配列 char word;//現在の文字 int i=0;//カウンタ int s=0;//sentenceのカウンタ int y=0;//数字が何回連続したかを数える char one[5]={'.',',','!','?',' '}; char two[3]={'a','b','c'}; char three[3]={'d','e','f'}; char four[3]={'g','h','i'}; char five[3]={'j','k','l'}; char six[3]={'m','n','o'}; char seven[4]={'p','q','r','s'}; char eight[3]={'t','u','v'}; char nine[4]={'w','x','y','z'}; int c; while(c = getchar() != '\n'){//数を一文字ずつ配列にいれていく number[i] = c; i++; } for(int x=0;x<(i+1);x++){ if(number[x]=='0'){ if(word == 'N'){} else{sentence[s]=word;s++;y=0;word='N';} } else{ if(number[x]=='1'){ word=one[y]; y++; y=y%5;} else if(number[x]=='2'){ word=two[y]; y++; y=y%3;} else if(number[x]=='3'){ word=three[y]; y++; y=y%3; } else if(number[x]=='4'){ word=four[y]; y++; y=y%3; } else if(number[x]=='5'){ word=five[y]; y++; y=y%3; } else if(number[x]=='6'){ word=six[y]; y++; y=y%3; } else if(number[x]=='7'){ word=seven[y]; y++; y=y%4; } else if(number[x]=='8'){ word=eight[y]; y++; y=y%3; } else{ word=nine[y]; y++; y=y%4; } } } sentence[i]='N'; return sentence;//配列の先頭のポインタを返す } }; int main(){ int n;//行数 cin>>n; getchar();//改行をとる char *first[n]; int i; int x=0; char ch; char *Fir; for(i=0;i<n;i++){ Line line; first[i] = line.Read(); } for(int j=0;j<i;j++){ Fir = first[i]; while((ch = Fir[x]) != 'N'){ cout << ch ; x++; } cout << '\n'; } return 0; } 実行結果: 1 20 Segmentation fault となってしまいます。 coutやコメントアウトで動きを追ってみたところ、下から8行目くらいのwhile文がなければ、segumentation faultはおきませんでした。ポインタの扱いが間違っているのかなとは思うのですが、どこが悪いのか考えても分かりません。どなたか教えてください。

  • do-while 文でコンパイルエラーがでます。。

    下記グローバル関数Run内のdo-while文で、 error C2059: syntax error : '}' というコンパイルエラーが起きるのですが、どうしても理由がわかりません; do-whileをコメントアウトしコンパイルしてみたところ正常に動くので、そこがおかしいのは間違いないと思います。 どなたかお力添えをお願いいたします。。 #include <iostream> #include "BinaryTree.cpp" using namespace std; void Run(); int main() { Run(); return 0; } void Run() { BinaryTree<int> *bt; char input; do { cout<<"Menu Display"<<endl; cout<<"---------------------"<<endl; cout<<"1> Enter data"<<endl; cout<<"2> Print Tree"<<endl; cout<<"3> Reverse Tree"<<endl; cout<<"4> Quit"<<endl; cout<<"Enter your choice: "; cin>>input; switch (input) { case '1': int elem; cout<<"Enter data for tree: "; cin>>elem; if (bt == NULL) bt = new BinaryTree<int>(elem); else bt->Insert(elem); break; case '2': if (bt == NULL) cout<<"The tree is empty."<<endl; else { cout<<"Tree size = "<<bt->Size(bt)<<endl; cout<<"---------------------"<<endl; bt->Inorder(bt); } break; case '3': if (bt == NULL) cout<<"The tree is empty."<<endl; bt->Reverse(bt); break; }while (input != '4'); } }

  • 配列の要素数に変数を入れたい

    配列に数の入力履歴を入れて最後にその数を出力したいのですが、変数を入れることはできないと勉強した記憶がありまさにその通りコンパイルエラーが出ました。 他に何か方法はありませんでしょうか。 /* 課題1-3 */ #include <time.h> #include <stdio.h> #include <stdlib.h> #include <math.h> int main(void) { int i; int no1; /* 範囲1 */ int no2; /* 範囲2 */ int max; /* 大きい乱数 */ int min; /* 小さい乱数 */ int y; /* 当てさせる数 */ int stage; /* 入力回数 */ int x; /* 読み込んだ値 */ int n; /* 入力制限 */ srand(time(NULL)); no1 = rand(); no2 = rand(); if(no1 > no2){ max = no1; min = no2; } else{ max = no2; min = no1; } y = min + rand() % (max-min); n = ceil(log(max-min)/log(2)); int a[n]; /*←配列の要素数をn個にしたい*/ printf("%d以上%d以下の整数を当ててください。\n", min, max); stage = 0; do{ printf("残り%d回。いくつでしょう:\n", n - stage); scanf("%d", &x); a[stage++] = x; if(y > x) printf("小さいです。\n"); else if(y < x) printf("大きいです。\n"); }while(y != x && stage < n); if(y != x) printf("残念でした。正解は%dです。", y); else printf("正解です。%d回目で正解しました。", stage); puts("\n---入力履歴---"); for(i=0; i<stage; i++) printf("%2d : %4d %+4d\n", i+1, a[i], a[i] - y); return (0); }