c++考试中帮个忙。从键盘输入不少于五个整数,将其中的奇数存入文件e:odd.txt,将其中的偶数存入
2个回答
展开全部
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
int main()
{
int n, a[100], i, odd, even;
cout<<"输入整数的个数:"<<endl;
cin >> n;
cout<<"请输入"<<n<<"个整数"<<endl;
for(i=0; i<n; i++) {
cin >> a[i];
}
ofstream f1("e:\\odd.txt"), f2("e:\\even.txt");
odd = 0, even = 0;
for(i=0; i<n; i++) {
if(a[i] % 2 == 1) {
f1 << a[i]<<endl;
odd ++;
}
else {
f2 << a[i] <<endl;
even ++;
}
}
f1 <<odd<<endl;
f2 <<even<<endl;
f1.close();
f2.close();
system("pause");
}
#include <fstream>
#include <stdlib.h>
using namespace std;
int main()
{
int n, a[100], i, odd, even;
cout<<"输入整数的个数:"<<endl;
cin >> n;
cout<<"请输入"<<n<<"个整数"<<endl;
for(i=0; i<n; i++) {
cin >> a[i];
}
ofstream f1("e:\\odd.txt"), f2("e:\\even.txt");
odd = 0, even = 0;
for(i=0; i<n; i++) {
if(a[i] % 2 == 1) {
f1 << a[i]<<endl;
odd ++;
}
else {
f2 << a[i] <<endl;
even ++;
}
}
f1 <<odd<<endl;
f2 <<even<<endl;
f1.close();
f2.close();
system("pause");
}
展开全部
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
int main()
{
vector<int> ivec,qvec,ovec;
int n;
while(cin>>n)
{
ivec.push_back(n);
}
for(vector<int>::iterator it=ivec.begin();it!=ivec.end();++it)
{
if(*it%2==0)
ovec.push_back(*it);
else
qvec.push_back(*it);
}
ofstream outfile;
outfile.open("e:\even.txt");
if(!outfile)
cout<<"open failed!"<<endl;
for(vector<int>::iterator it=qvec.begin();it!=qvec.end();++it)
{
outfile<<*it<<" ";
}
outfile<<endl<<qvec.size();
outfile.close();
outfile.open("e:\odd.txt");
if(!outfile)
cout<<"open failed!"<<endl;
for(vector<int>::iterator it=ovec.begin();it!=ovec.end();++it)
{
outfile<<*it<<" ";
}
outfile<<endl<<ovec.size();
outfile.close();
return 0;
}
#include<fstream>
#include<vector>
using namespace std;
int main()
{
vector<int> ivec,qvec,ovec;
int n;
while(cin>>n)
{
ivec.push_back(n);
}
for(vector<int>::iterator it=ivec.begin();it!=ivec.end();++it)
{
if(*it%2==0)
ovec.push_back(*it);
else
qvec.push_back(*it);
}
ofstream outfile;
outfile.open("e:\even.txt");
if(!outfile)
cout<<"open failed!"<<endl;
for(vector<int>::iterator it=qvec.begin();it!=qvec.end();++it)
{
outfile<<*it<<" ";
}
outfile<<endl<<qvec.size();
outfile.close();
outfile.open("e:\odd.txt");
if(!outfile)
cout<<"open failed!"<<endl;
for(vector<int>::iterator it=ovec.begin();it!=ovec.end();++it)
{
outfile<<*it<<" ";
}
outfile<<endl<<ovec.size();
outfile.close();
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询