アプリ版:「スタンプのみでお礼する」機能のリリースについて

このプログラミングの問題を教えて欲しいです。

キーボードから整数kを入力し、kが配列aの中に何個存在するかを
調べて、実行例のように表示するプログラムを作れ。存在しない場合は
nothing!と表示する。
但し、配列aは問1と同じ初期化がされているものとする。

実行例1)
k: 5
count=2

実行例2)
k: 2
nothing!

プログラム構成例)
#include <stdio.h>
void main(){
int a[9] = {7, 6, 12, 8, 3, 5, 10, 9, 5};
int i, k, count = 0;

//kのキーボード入力
//配列aでkと同じ値の要素の個数の算出(for文を使う)
//結果の表示

A 回答 (2件)

Visual Studio 2019のコンソールアプリでプロジェクトを立ち上げました。



#include<stdio.h>
#include<string>
#include<iostream>
#include<sstream>
using namespace std;


void main() {
int a[9] = { 7, 6, 12, 8, 3, 5, 10, 9, 5 };
int i, k, count = 0;
string kk;

//kのキーボード入力
cout << "k: ";
getline(cin, kk);
k = stoi(kk);

//配列aでkと同じ値の要素の個数の算出(for文を使う)
for (i = 0; i < 9; i++) {
if (k == a[i])count++;
}

//結果の表示
if (count) {
cout << to_string(count);
}
else {
cout << "nothing!";
}
cout << "\n";
cout << "\n";

system("pause");
}
    • good
    • 0
    • good
    • 0
この回答へのお礼

ありがとう

ありがとう

お礼日時:2022/12/20 09:25

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!