
5个回答
展开全部
这个是自己输四位数的
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int a, b, c, d, e;
cout << "请输入要分解的四位数:" << endl;
cin >> a;
b = a/1000;
c = (a-1000*b)/100;
d = (a-1000*b-100*c)/10;
e = (a-1000*b-100*c-10*d);
cout << "分解后的四个数字为:" << b << " " << c << " " << d << " " << e <<endl;
system ("pause");
return 0;
}
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int a, b, c, d, e;
cout << "请输入要分解的四位数:" << endl;
cin >> a;
b = a/1000;
c = (a-1000*b)/100;
d = (a-1000*b-100*c)/10;
e = (a-1000*b-100*c-10*d);
cout << "分解后的四个数字为:" << b << " " << c << " " << d << " " << e <<endl;
system ("pause");
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
int x = 1234;
int array[4] = {0};
int i;
for (i = 0; i < 4; i++)
{
array[3-i] = i % 10;
i /= 10;
}
之后数组array中的4个元素分别就是1、2、3、4了
int array[4] = {0};
int i;
for (i = 0; i < 4; i++)
{
array[3-i] = i % 10;
i /= 10;
}
之后数组array中的4个元素分别就是1、2、3、4了
追问
额,我以为,题目的意思是,这个整数是由自己输的,可以输入4位的,也可以输入3、5以及其他位数的,乃这样编,不是就定死输入的整数位数了么?
追答
嗯,那就这样,你定义
int x;
cin >> x;
然后定义
int y = x;
int len = 0;
for (; y > 0;)
{
len++;
y/ = 10;
}
然后在定义数组的时候用
int *array = new int [len];
后面就一样了,不过记得在用完之后:
delete [] array;
记得加中括号,删除堆分配的数组必须这样,否则只能释放第一个int字段
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1234/1000 = 1,1234%1000 = 234;
234/100 = 2, 234%100 = 34;
34/10 = 3, 34%10 = 4;
思路如上
234/100 = 2, 234%100 = 34;
34/10 = 3, 34%10 = 4;
思路如上
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
# include "stdafx.h"
# include <stdlib.h>
void main()
{
int num = 12345678;
char buf[256];
_itoa(num, buf, 10);
}
////鸽给出最简单方法:调用itoa
# include <stdlib.h>
void main()
{
int num = 12345678;
char buf[256];
_itoa(num, buf, 10);
}
////鸽给出最简单方法:调用itoa
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询