C语言.由键盘输入10个整数存入数组a中,再任意输入一个整数k,然后在数组中查找k出现的次数

如题所述

这样:

#include<stdio.h>

#define LEN 10

int main(void)

{

int a[LEN], i, k;

int same = 0;

puts("请输入10个整数:");

for(i=0; i<LEN; i++)

scanf("%d", &a[i]);

while(getchar() != '\n')

continue;

puts("再输入一个整数k");

scanf("%d", &k);

for(i=0; i<LEN; i++)

{

if(k == a[i])

same++;

}

printf("k一共出现%d次。", same);

getchar();

return 0;

}

扩展资料:

注意事项

循环输出1-100的所有整数,用整数%10,等于9,即求出了所有各位是9的个数,用整数除10,得到了所有十位是9的个数,有一个数字比较特殊,99应该算两次,所以,应该用两个判断语句进行判断。

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <math.h>

int main() {

int i = 0;

int count = 0;//定义一个记录次数的变量

for (i = 1; i <= 100; i++) {

if (i % 10 == 9) {

count++;//99进来一次count++

}

if (i / 10 == 9) {

++count;//99进来count++

}

}

printf("1-100中出现9的次数有%d次\n", count);

system("pause");

return 0;

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-11-21
//#include "stdafx.h"//If the vc++6.0, with this line.
#include "stdio.h"
int main(void){
    int a[10],i,x,k;
    printf("Input 10 integers...\n");
    for(i=0;i<10;scanf("%d",a+i++));
    printf("Input k(int)...\nk=");
    scanf("%d",&k);
    for(x=i=0;i<10;i++)
        if(a[i]==k)
            x++;
    printf("The result is %d\n",x);
    return 0;
}

本回答被网友采纳
第2个回答  2017-08-13
#include<stdio.h>
#define LEN 10
int main(void)
{
int a[LEN], i, k;
int same = 0;
puts("请输入10个整数:");
for(i=0; i<LEN; i++)
scanf("%d", &a[i]);
while(getchar() != '\n')
continue;
puts("再输入一个整数k");
scanf("%d", &k);
for(i=0; i<LEN; i++)
{
if(k == a[i])
same++;
}
printf("k一共出现%d次。", same);
getchar();
return 0;
}
相似回答