PDA

Показать полную графическую версию : *Example* | Помогите по C++ (Массивы)


Tier9
09-03-2005, 02:21
В массиве х(х1, х2...xn) найти максимальный элемент и поделить его на все остальные элементы?
Алглоритм простой, я не знаю как написат программный код на С++

mrcnn
09-03-2005, 03:31
#define N 12

#include <stdio.h>

void precise_division(int first,int second,int precision);

void main(void){
int x[N]={1,2,3,34,4,5,6,7,8,9,10,11};
//double b[N];
int i=0;
int result=x[i];
for(i++; i<N;i++)
if(x[i]>result)
result=x[i];printf("Maximum: %d\n",result);
if(result !=0){
for (i=0; i<N;i++)
//b[i]=(double)x[i]/(double)result;
precise_division(x[i],result,20);
}
}


void precise_division(int first,int second,int k){
int c=0;
int ost;
int tmp;
int cnt=0;
int t=0;

tmp=first;

if(first>=second)
while (first>=second)
{
first-=second;
c++;
}
else if (first<second)
c=0;

if(first==0)
printf ("first/second = %d\n",c);
else
{
first=tmp;
printf ("first/second = %d,",c);

ost=first%second;

while(cnt<k)
{
if( ost<second)
{
if (t>=1)
{
printf ("0");
cnt++;
}
t++;
ost*=10;
}
else
{
printf("%d",ost/second);
ost%=second;
cnt++;
t=0;
}
}
}
printf ("\n");
}

pva
09-03-2005, 11:08
Я бы посоветовал воспользоваться алгоритмами (писанины меньше и понятней)

#include <stdexcept>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;

// must be declared
class doSmth_divMax {
double max;
public:
doSmth_divMax(const double& d) : max(d)
{
}
void operator()(double& d) const
{
d = max/d;
}
};

void doSmth(vector<double>& vd)
{
vector<double>::iterator imax = max_element(vd.begin(), vd.end());

if (vd.end()!=imax) {
for_each(vd.begin(), vd.end(), doSmth_divMax(*imax));
return;
}

throw logic_error("there is no maximum here");
}


мне кажется, что в фразе и поделить его на все остальные кроится какая-то ошибка. Если так, то программа будет ещё проще.

Tier9
11-03-2005, 09:57
Спасибо всем!!!




© OSzone.net 2001-2012