输入没有在C ++中的scanf中终止

时间:2014-03-05 06:44:49

标签: c++ input scanf

输入未终止输入

5  
1 10  
10 8  
8 2  
2 1  
1 10  

但是对于其他输入,例如

5   
1 1  
1 2   
1 3   
1 4   
1 5  

工作正常。 我已经尝试了各种方法,比如使用if条件来终止循环,但它不起作用。

#include<utility>
#include<iostream>
#include<set>
#include<cstdio>
// where a==breadth b== index
#define MP(a,b)  make_pair(-a,b)
#define INDEX it->second
#define K1 a[INDEX][2]
#define getL(i,k)  a[(i)][0]
#define getB(i,k)  a[(i)+(k)-1][1]
#define COST(a,b,c)   ((a)*(b)*(c))
#define REMOVE(a,i) s.erase(s.find(MP(a,i)))
#define PUSH(a,i) s.insert(MP(a,i))
#define FIND(a,i) s.find(MP(a,i))
using namespace std;

set  < pair <  int , int > > s;
set < pair <  int , int > >::iterator it,p;


#define MAX 1000
int a[MAX][3];

int solve(int n){
    int i,idx,idx2,k1,k2,last=n-2;
    int value=0;
    a[n-1][2]=1;
    for(i=0;i<n-1;i++)
    {
        a[i][2]=1;
        s.insert(MP(a[i][1],i));//the value will be added on the basis of hte
    }
    cout << s.size();
    while(!s.empty())
    {
        cout << "the minum";
        it=s.begin();
        idx=INDEX;
        k1=K1;
        idx2=idx+k1;
        k2=a[idx2][2];//matrix with which the idx matrix has to be multiplied

        s.erase(it);
        cout << getB(idx2 ,k1);
        cout << idx2;
        value+=COST(getL(idx,k1),getB(idx,k1),getB(idx2,k2));
        a[idx][2]=k1+k2;// all the values will be update by now
        a[idx2][2]=k1+k2;
        if(idx==last)// if the index has to be equal to the last
        {// multiply the matrix but don't put it in the tree
            last=idx-a[(idx-1)][2];//
        }
        else
        {
            REMOVE(getB(idx2,k2),idx2);
            PUSH(getB(idx2,k2),idx);
        }
    }

    return value;
}


int main(){
    int n,i;
    printf("Enter the number of matrix\n");
    scanf("%d",&n);
    printf("Now Enter the dimensions of the matrix\n");
    //  the output is not terminating in the loop
    //
    for(i=0;i<n;i++)
    {
        scanf("%d %d",&a[i][0],&a[i][1]);
    }
    cout << "Input terminated";

    printf("\nThe approx number of calculations required are %d",solve(n));

    return 0;
}

1 个答案:

答案 0 :(得分:0)

不使用带有cout的endl可能会导致很多次问题。我不知道确切的原因,但我之前遇到过这种情况,特别是在使用printf或scanf之前使用cout的print语句时。