首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

ACM中wrong answer C++解决方法

2013-03-14 
ACM中wrong answer C++Description已知两个整数数组f[]和g[],它们的元素都已经从小到大排列。例如f[]中可能

ACM中wrong answer C++
Description

已知两个整数数组f[]和g[],它们的元素都已经从小到大排列。例如f[]中可能有1,2,2,3,3,g[]中有1,2,2,2,3。 
请写一个程序,算出这两个数组彼此之间有多少组相同的数据。就以上例而言: 
f[0]于g[0]是第一组; 
f[1]于g[1]是第二组; 
f[2]于g[2]是第三组; 
f[3]于g[4]是第四组。


Input

第一行为两个整数m, n(1≤m, n≤1000),分别代表数组f[], g[]的长度。 
第二行有m个元素,为数组f[]。 
第三行有n个元素,为数组g[]。




Output

输出等值数目。


Sample Input



Original

Transformed


5 5
1 2 2 2 3
1 2 2 3 3



 Sample Output



Original

Transformed


4




 Hint

你能想出O(n+m)的算法吗?^_^ 
加油!



代码如下为什么是wrong answer
#include<iostream>
using namespace std;
int main()
{
int m,n,i,j=0,t=0;

cin>>m>>n;
int * f=new int[m];
int * g=new int[n];

for(i=0;i<m;i++)
cin>>f[i];

for(i=0;i<n;i++)
cin>>g[i];
for(i=0;i<m;i++)
for(;j<n;)
{

if(f[i]==g[j])
{
t++;
j++;
break;
}
 else if(f[i]<g[j])
 {
 j++;
 break;
 }
 else
 j++;
}
cout<<t<<endl;
return 0;
}









[解决办法]
 else if(f[i]<g[j])
 {
 j++;
 break;
 }

为什么j++

热点排行