如何用C++语言实现输入一个正整数N,使字符型变量a=0123。。。N-1 即若输入5,则a为01234

如何用C++语言实现输入一个正整数N,使字符型变量a=0123。。。N-1
即若输入5,则a为01234
程序中直接让a等于0到N-1,不是在结果中体现
最新回答
初时模样

2024-10-02 02:43:35

#include <iostream>
using namespace std;
void main()
{
int N;
cout << "请输入一个正整数N:";
cin >> N;
char *a = new char[N+1];
for (int i = 0; i < N; ++i)
a[i] = i+48;
a[N] = '\0';
cout << a << endl;
}
真相是假

2024-10-02 00:53:55

char a[10];
scanf("%n",&n);
for(n;n>=0;n--)
{a[n-1]=n;}
㈠朵烟熏的埖

2024-10-02 03:10:02

#include <iostream>
using namespace std;
void main()
{
int N;
cout<<"请输入一个正整数N:";
cin>>N;
cout<<"\n";
char *a=new char[N+1];
int k=0;
for (int i=0; i<N; i++)
a[k++] = i+0x30;
a[k] = '\0';
cout<<a<<endl;
}
路遥归梦

2024-10-02 07:15:43

#include <iostream>
using namespace std;
void main()
{
int N;
printf("请输入一个正整数N:");
scanf("%d",&N);
char *a = new char[N+1];
for (int i = 0; i < N; ++i)
a[i] = i+48;
a[N] = '\0';
cout << a << endl;
}