Améliorez votre expérience CodeS-SourceS avec ce plugin:
http://codes-sources.commentcamarche.net/forum/affich-10000111-plugin-better-cs-2#cptpingu-signature
#include <iostream> int main() { bool stop = false; while (!stop) { unsigned long int nb; std::cout << "Entrez un nombre, 0 pour quitter" << std::endl; std::cin >> nb; if (nb == 0) stop = true; else { unsigned int i = 0; while (nb != 0) { std::cout << (nb % 10) << " " << i << std::endl; ++i; nb /= 10; } } } return 0; }
#include <iostream> #include <vector> int main() { bool stop = false; while (!stop) { std::vector<unsigned int> tab[10]; unsigned long int nb; std::cout << "Entrez un nombre, 0 pour quitter" << std::endl; std::cin >> nb; if (nb == 0) stop = true; else { unsigned int i = 0; while (nb != 0) { tab[nb % 10].push_back(i); ++i; nb /= 10; } for (unsigned int k = 0; k < 10; ++k) { if (!tab[k].empty()) { std::cout << k; for (unsigned int j = 0; j < tab[k].size(); ++j) std::cout << " " << tab[k][j]; std::cout << std::endl; } } } } return 0; }
#include <iostream> #include <vector> #include <map> int main() { typedef std::map<unsigned int, std::vector<unsigned int> > map_type; typedef map_type::const_iterator iter; bool stop = false; while (!stop) { map_type map; unsigned long int nb; std::cout << "Entrez un nombre, 0 pour quitter" << std::endl; std::cin >> nb; if (nb == 0) stop = true; else { unsigned int i = 0; while (nb != 0) { map[nb % 10].push_back(i); ++i; nb /= 10; } for (iter it = map.begin(); it != map.end(); ++it) { std::cout << it->first; for (std::vector<unsigned int>::const_iterator jt = it->second.begin(); jt < it->second.end(); ++jt) std::cout << " " << *jt; std::cout << std::endl; } } } return 0; }
Améliorez votre expérience CodeS-SourceS avec ce plugin:
http://codes-sources.commentcamarche.net/forum/affich-10000111-plugin-better-cs-2#cptpingu-signature
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question//programme affichage poids des chiffres dans un nombre
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
//déclarations et initialisations
unsigned long int nb,nbr;
int tab[9],i,j,k,u,m,r;
//saisie du nombre initial, sortie pour 0
cout <<"entrez un nombre, 0 pour quitter"<<endl;
cin>>nb;
//décomposition du nombre dans le tableau
while(nb!=0){
for (j=8;j>=1;j--){
k=0;
while(nb-k*pow(10,j)>=pow(10,j)-0)
{k++;
}
tab[j]=k;
nb=nb-(k*pow(10,j));
}
}
tab[0]=nb;
//valeurs du tableau non utilisées égales à 10
r=8;
while(tab[r]==0)
{tab[r]+=10;
r--;
}
//affichage du poids de chaque chiffre
for (i=0;i<=8;i++){
if (tab[i]<10){
cout<<tab[i]<<" "<<i<<" ";
for (u=i+1;u<=8;u++){
if (tab[i]==tab[u]){
cout<<u<<" ";
tab[u]+=10;}
}
cout<<endl;
}
}
//reconstitution du nombre
nbr=0;
for (m=0;m<=8;m++){
if (tab[m]>=10){
tab[m]-=10;
}
nbr+=tab[m]*pow(10,m);
}
nbr+=1;
cout<<nbr<<endl;
cout<<"entrez un nombre, 0 pour arrêter: ";
cin>>nb;
}
return 0;
}
Améliorez votre expérience CodeS-SourceS avec ce plugin:
http://codes-sources.commentcamarche.net/forum/affich-10000111-plugin-better-cs-2#cptpingu-signature
//programme affichage poids des chiffres dans un nombre
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
//déclarations et initialisations
unsigned long int nb,nbr;
int tab[9],i,j,k,l,m;
//saisie du nombre initial, sortie pour 0
cout <<"entrez un nombre, 0 pour quitter"<<endl;
cin>>nb;
//décomposition du nombre dans le tableau
while(nb!=0){
for (j=0;j<=8;j++)
{tab[j]=nb%10;
nb/=10;
}
//valeurs du tableau non utilisées égales à 10
l=8;
while(tab[l]==0)
{tab[l]+=10;
l--;
}
//affichage du poids de chaque chiffre
for (i=0;i<=8;i++){
if (tab[i]<10){
cout<<tab[i]<<" "<<i<<" ";
for (k=i+1;k<=8;k++){
if (tab[i]==tab[k]){
cout<<k<<" ";
tab[k]+=10; }//permet de ne pas réutiliser ce chiffre
}
cout<<endl;}
}
//reconstitution du nombre
nbr=0;
//réinitialisation du tableau aux valeurs d'origine
for (m=0;m<=8;m++){
if (tab[m]>=10){
tab[m]-=10;
}
nbr+=tab[m]*pow(10,m);
}
cout<<nbr<<endl;
cout<<"entrez un nombre, 0 pour arrêter: ";
cin>>nb;
}
return 0;
}
void main() { /* Exemple, si saisie 1234445555 affiche 5 0 1 2 3 4 4 5 6 3 7 2 8 1 9 */ const int MAXSIZENUM = (int)log10((double)INT_MAX)+1;/*<limit.h> largeur max pour 1 int*/ char* buf = new char[MAXSIZENUM]; /*le nombre saisi*/ int sizeStr; /*la taille du nombre*/ while(true) { printf("le nombre (0 pour quitter ): "); scanf("%s", buf); sizeStr = strlen(buf); if(!strcmp(buf,"0")) break; buf = strrev(buf); /* Reverse string */ int* pVals= new int[sizeStr]; /* Les chiffres */ int* pPoids = new int[MAXSIZENUM]; /* Les poids */ memset(pPoids, -1, sizeof(int)*MAXSIZENUM); int savInd = -1, rechVal = -1; /* pseudo curseur dans la boucle */ for(int i=0; i<sizeStr; i++) { pVals[i] = (int)buf[i]-48; /*Le chiffre en cours*/ if(rechVal == -1 && pPoids[ pVals[i] ] == -1) /*pas encore de poids*/ { pPoids[ pVals[i] ] = i; rechVal = pVals[i] ; printf("\r\n%d \t%d", pVals[i], pPoids[ pVals[i] ]); } else { if( rechVal == -1 && savInd==-1 ) savInd = i; else if( pVals[i] == rechVal) /* même poids, même ligne */ printf("\t%d", i); } if(i == sizeStr-1) /* réinit */ { i=savInd; rechVal=-1; savInd=-1; } } delete[] pVals; delete[] pPoids; printf("\r\n%s\r\n", strrev(buf)); } }
Améliorez votre expérience CodeS-SourceS avec ce plugin:
http://codes-sources.commentcamarche.net/forum/affich-10000111-plugin-better-cs-2#cptpingu-signature
Améliorez votre expérience CodeS-SourceS avec ce plugin:
http://codes-sources.commentcamarche.net/forum/affich-10000111-plugin-better-cs-2#cptpingu-signature