怎样将一个数三位三位的输出
比如我有一个数为123456789
现在我要将它输出为123.456.789.怎么输出?
[解决办法]
#include <stdio.h>void show(unsigned int val){ if(val == 0) return; show(val/1000); val = val%1000; printf("%d.",val);}int main(){ unsigned int val = 123456789; show(val);}
[解决办法]
#include <iostream>#include <string>using namespace std;int main() { char *buff; int lenth = sprintf(buff, "%d", 123456789); cout << buff << endl; string s(buff, lenth); for(int i = 1, j = 0; 3 * i <= lenth; i++, j++) //[color=#FF0000]j[/color]因为string对象的长度随插入“.”增加 s.insert(i * 3 + j, "."); cout << s << endl; return 0;}
[解决办法]
int temp = 0;
for (int i = 100; i <= 1000; i++)
{
temp++;
if (temp % 3 == 0)
{
Console.WriteLine(temp.ToString());
}
}
Console.Read();
[解决办法]
[code=C/C++][
#include <stdio.h>
#include<iostream.h>
#include<malloc.h>
typedef struct SNode//结构体定义链栈
{
int data;
SNode *next;
}*Lstack;
void init(Lstack &s)//初始化
{
s=NULL;
}
void push(Lstack &s,int e)//进栈
{
Lstack p;
p=new SNode;
p->data=e;
p->next=s;
s=p;
}
bool pop(Lstack &s,int &e)//出栈
{
Lstack p;
if(!s) return false;
e=s->data;
p=s;
s=s->next;
delete p;
return true;
}
void show(unsigned int val)
{
Lstack p;
int m;
init(p);
while(1)
{
if(val == 0)
break;
else
{
push(p,val%1000);
val=val/1000;
}
}
while(pop(p,m))
{
cout<<m<<'.';
}
}
int main()
{
unsigned int val = 123456789;
show(val);
return 0;
}
/code]