我想颠倒所有数字,但它只会颠倒最后一个数字 [英] i want to reverse all numbers but it just reverse the nlast one

查看:125
本文介绍了我想颠倒所有数字,但它只会颠倒最后一个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// reversenum.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
using namespace std ;

int main()
{

	int num[100] ;
    int j = 0 , k ;
    int number = 0 ;
    int temp  ;
    int a = 0 , i ;
	int n ;
	cout << "please enter the numbers of numbers that you want :" << endl ;
	cin >> n ;

	cout << "please enter the numbers " << endl ;
	for( int j = 0 ; j < n ; j++ ){
		cin >> num[j] ;

	temp = num[j] ;
   while( temp / 10 > 0 )
	{
	temp /= 10 ;
		a++ ;	
	}
   for( ; num[j] / 10 != 0 ; num[j] /= 10 , a-- )
     {
        k = num[j] % 10 ;
        for( i = 0 ; i < a ; i++ )
          {
             k*=10;
          }

         number=k+number;
		
     }
   
   number += num[j]%10 ;
		
   cout << number <<endl ;

	}
	  
	
	return 0;
}


它不能清除第一个数字,然后输入下一个数字?


it cannot clear the first number then put the next number ?

推荐答案

问题是您对所有输入使用相同的变量(num)数字.因此,您可能
  • 使用数组而不是单个变量
The problem is that you use the same variable (num) for all the input numbers. Hence you may either
  • Use an array instead of a single variable
  • 在与用户获取输入编号相同的循环中执行反向操作.


要更清楚:
To be more clear:
for( int i = 0 ; i < n ; i++ )
    cin >>num ;


您正在此处覆盖输入.在num中,仅保留最后一个.

建议使用数组代替.存储所有输入,然后尝试反转它们.


顺便说一句,看来您是从反转数字的某处获取了此代码.该观察结果基于reverse()方法的返回类型以及您在Main()中使用它的方式.

为了反转所有输入的数字然后打印出来,您也需要更改方法的返回类型.


You are over-writing the inputs here. In num, only the last one remains.

What is being suggested is to use an array instead. Store all the inputs and then try to reverse them.


BTW, it looks like that you got this code to from somewhere that reverses a number. This observation is based on the return type of your reverse() method and the way you use it in Main().

In order to reverse all the numbers that are input and then print it out, you need to make the change to method return type too.


为什么要使其复杂?
Why make it complicated?
#include<stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

//----------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
    cout << "please don't write plz, and enter the numbers of numbers that you want:" << endl;

    vector<int> values;
    int count;
    cin >> count;

    int value;

    cout << "please don't write plz, and enter the numbers" << endl ;
    for( int i = 0 ; i < count ; i++ )
    {
        cin >> value;
        values.push_back(value);
    }

    for (vector<int>::reverse_iterator it = values.rbegin(); it != values.rend(); it++)
    {
        value = *it;
        cout << value << endl;
    }

    return 0;
}



问候
Espen Harlinn



Regards
Espen Harlinn


这篇关于我想颠倒所有数字,但它只会颠倒最后一个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆