如何根据自己的位置,一个完整=数字分成两组(偶数或奇数) [英] How to split a full=number into two groups according to their position(even or odd)

查看:186
本文介绍了如何根据自己的位置,一个完整=数字分成两组(偶数或奇数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个txt文件分割成2个数组
TXT文件包含完整number.Can我做没有字符串?
例如,对于输入

  4(有多少行)
2 1
3 7
8 0
3 7

欲阵列1包含(在一条线的firt数)

  {2
3
8
3}

阵列2包含(在一条线的第二数目)

  {1
 7
 0
 7}

我怎么能这样做呢?只是好奇......这里是一个code不工作。

  ifstream的IFS(sth.txt);
INT克;
IFS>>克;
INT女孩[G]。
INT男孩[G]。
对(INT I = 0; I&2 * G,I ++){
如果(我%2 == 0)IFS>>吉[G]。
如果(我%2 == 1)IFS>>男孩[G];}
COUT&所述;&下;男孩[1];


解决方案

  ifstream的IFS(sth.txt);
INT克;
IFS>>克;
INT女孩[G]。
INT男孩[G]。
的for(int i = 0; I< G,I ++){
    IFS>>的女孩[我]
    IFS>>男孩[I]
}
COUT&所述;&下;男孩[0];

您正在阅读到的女孩[G] 男孩[G] 而不是 0 ..(G-1)

我也改变阅读:两个整数的insted的1循环的一个迭代

最后我改第一计数(索引0),而不是男孩

的第二个元素

I need to split a txt file into 2 arrays The txt file contains full number.Can I do it without string? For example,for input

4(how many line)
2 1
3 7
8 0
3 7

I want array 1 contains (firt number in a line)

{2 
3 
8 
3}

array 2 contains (second number in a line)

{1 
 7
 0 
 7}

How can I do that?Just curious...Here is a code which does not work..

ifstream ifs("sth.txt");
int g;
ifs>>g;
int girl[g];
int boy[g];
for(int i=0;i<2*g,i++;){
if (i%2==0)ifs>>gil[g];
if (i%2==1)ifs>>boy[g];}
cout<<boy[1];

解决方案

ifstream ifs("sth.txt");
int g;
ifs>>g;
int girl[g];
int boy[g];
for(int i=0;i<g,i++;){
    ifs>>girl[i];
    ifs>>boy[i];
}
cout<<boy[0];

You were reading to girl[g] and boy[g] instead of 0..(g-1).

I also changed reading: two ints insted of 1 in one iteration of the loop.

At the end I changed counting first (index 0) instead of second element of boy.

这篇关于如何根据自己的位置,一个完整=数字分成两组(偶数或奇数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆