我这个程序 请帮我改一下

sunsold 2009-04-05 11:23:11
我想问一下 数组输入的时候怎么能自己决定输入的长度 比如定义了输入50个数的数组 但是我输入了3个就够了想停了 怎么办?
还有我这个程序 输入的index数组 比如输入1和3了 然后后边计算出的是一个出现的次数7
我想让它以这种形式输出 1然后 后边是出现的次数4
1 4
3 3
....
....
#include <iostream>
#include <cstdlib>

using namespace std;

void main()
{
int a[16] = {-12,3,-12,4,1,1,-12,1,-1,1,2,3,4,2,3,-12};
int i,k,index[5];
int j=0;

for(i=0;i<16;i++)
cout<<" \n"<<a[i];
cout<<"input what do you want to find"<<endl;
for(k=0;k<5;k++)
cin>>index[k];
for(i=0;i<16;i++)
for(k=0;k<5;k++)
if(index[k]==a[i])
{
j=j++;
}
cout<<"the number is"<<j<<endl;

}
...全文
78 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
cnliunx 2009-04-06
  • 打赏
  • 举报
回复
学习一下
gengchenhui 2009-04-06
  • 打赏
  • 举报
回复
顶2楼~~~~
CqCoder 2009-04-06
  • 打赏
  • 举报
回复
2楼 精华帖 顶··
gxw145 2009-04-05
  • 打赏
  • 举报
回复
void main()
{
int a[16] = {-12,3,-12,4,1,1,-12,1,-1,1,2,3,4,2,3,-12};
int i,k,index[5];
int count[5]={0};

for(i=0;i <16;i++)
cout << a[i] << ", ";
cout << endl;
cout << "input what do you want to find" << endl;
for(k=0;k <5;k++)
cin>>index[k];
for(i=0;i <16;i++)
for(k=0;k <5;k++)
if(index[k]==a[i])
{
count[k]++;
}
for (k=0;k <5;k++)
{
cout << "the number is:" << index[k] << setw(5) << count[k] << endl;
}
}
Paradin 2009-04-05
  • 打赏
  • 举报
回复
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
int a[16] = {-12,3,-12,4,1,1,-12,1,-1,1,2,3,4,2,3,-12};
int i,k,index[5], m;
int j=0;
char c = 'Y';

for(i=0;i <16;i++)
cout << a[i] << "\n";
cout <<"input what do you want to find:" <<endl;
k = 0;
while (c == 'Y' || c == 'y')
{
cout << "input:";
cin >> index[k++];
cout << "is there more? (Y/N)" << endl;
cin >> c;
}

for(m=0;m <k;m++)
{
j = 0;
for(i=0;i <16;i++)
if(index[m]==a[i])
++j;
cout << index[m] << " : " << j << endl;
}
return 0;
}
  • 打赏
  • 举报
回复
但是我输入了3个就够了想停了 怎么办?

那可以让人家先选择长度啊,循环读取,到了比如3个就退出啊。



我想让它以这种形式输出 1然后 后边是出现的次数4

这个就遍历数组,统计不就可以了。

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧