Cattle exercises off the net

222. Music Research:

Problem-solving ideas:

First, the first stage must be used to get all the data but also continuously, so that we have found as long as the first number of the second stage, the phase loop back is found to minus minimum

Code:

#include<iostream>
 #include<string>
 #include<algorithm>
 #include<math.h>
 #include<cmath>

 #define INT_MAX 2147483647
 #define INT_MIN (-INT_MAX - 1)

 using namespace std;

 const int maxn=1003;
 int a[maxn],b[maxn];

 int main() {
     int m,n;
     while(cin>>n) {
         long long res=INT_MAX;       //必须要在循环时,重置为初始值
         for(int i=0;i<n;i++) {
             cin>>a[i];
         }
         cin>>m;
         for(int i=0;i<m;i++) {
             cin>>b[i];
         }
         int temp=0;
         for(int i=0;i<m-n;i++) {
             int k=i;
             for(int j=0;j<n;j++) {
                 int temp2=abs(a[j]-b[k++]);
                 temp+=temp2*temp2;
             }
             if(res>temp) {
                 res=temp;
             }
             temp=0;
         }
         cout<<res<<endl;
     }
     return 0;
 }

to sum up:

After figured logic code is not difficult, and when the cycle in calculating the minimum input res is worth to note the time to reset thanks to a: res to give initial values ​​and INT_MAX temp = 0.

 

 

Released nine original articles · won praise 0 · Views 755

Guess you like

Origin blog.csdn.net/foreverboss/article/details/104213106