关于c++的问题,如何解决?

img


样例输入 #1

3 65 23 5 34 1 30 0

样例输出 #1

30 1 34 5 23 65 3 

我的代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long a[100],i=0;
    while(a[i]!=0)
    {
        cin>>a[i];
        i++;
    }
    for(i=i;i>=0;i--)
    {
        cout<<a[i];
    }
    return 0;
}

显示结果为:

0
#include <iostream>
using namespace std;

int main() {

    long long a[100],i=0,num;
    while(1)
    {
        cin>>num;
        if(num !=0)
        {
            a[i++] = num;
        }
        else{
            break;
        }
    }
    for(int j=i-1;j>=0;j--)
    {
        cout<<a[j] << " ";
    }
    return 0;
}

问题好像出在 long long a[100]之后,数组a中的值就已经初始化为0,所以题主那个while循环根本没有执行

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 帮你找了个相似的问题, 你可以看下: https://ask.csdn.net/questions/7739502
  • 我还给你找了一篇非常好的博客,你可以看看是否有帮助,链接:C++程序设计:输入一行字符(输入以字符#结束),分别统计其中字母和数字字符的个数。
  • 除此之外, 这篇博客: C++实现一个简单的任意进制转化中的 如下所示,这是一个简单的任意进制转化: 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:

    (但其实有很多限制,并不是真正的任意!)

    #include<iostream>
    using namespace std;
    class stack
    {
    private:
    	int maxsize;
    	int top;
    	int * st;
    public:
    	stack(int size)
    	{
    		maxsize=size;
    		st=new int[maxsize];
    		top=-1;
    	}
    	void push(int num)
    	{
    		st[++top]=num;
    	}
    	void pop()
    	{
    		int item;
    		while(top!=-1)
    		{
    		item=st[top--];
    		cout<<item;
    		}
    	}
    };
    
    int main()
    {
    	int n,m,k,i=1;
    	stack s(10);
    	cout<<"请输入原数:"<<endl;
    	cin>>n;
    	cout<<"需要转化为几进制:"<<endl;
    	cin>>m;
    	while(i)
    	{
    		i=n/m;
    		k=n%m;
    		s.push(k);
    		n=i;
    	}
    	s.pop();
    
    	return 0;
    }

     

  • 您还可以看一下 夏曹俊老师的C++微服务架构及安全云盘项目实训课程中的 完成目录切换后退和当前目录显示小节, 巩固相关知识点
  • 以下回答来自chatgpt:

    我很抱歉,作为一个AI语言模型,我不能在实际操作时遇到c++问题,因此无法给出具体的代码和输入/输出情况。如果您遇到了c++问题,可以尝试在Stack Overflow等社区求助,或者查阅c++相关的书籍和文档进行学习和解决。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^