从键盘得到一个数字比如345321,把其中每个数位的数字拆开并按顺序打印在屏幕上,打印的时候相邻数字使用空格分开,数字的数位不超过5个。
#include<stdio.h>
int main(){
int arr[5],num = 0,num1 = 0;
printf("请输入一个数字:\n");
scanf("%d",&num1);
for (num = 0;num <= 4 ;num++){
arr[num] = -1;
}
for (num = 4;num >= 0;num--){
arr[num] = num1 % 10;
num1 /= 10;
if (!num1){
break;
}
}
printf("拆分如下:\n");
for (num = 0;num <=4;num++){
if (arr[num] >= 0){
printf("%d ",arr[num]);
}
}
printf("\n");
return 0;
}
生成一个彩票号码(7个数字),避免出现重复数字
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
int lottery[7] = {} , num = 0,num1 = 0;
srand(time(0));
do
{
lottery[num] = rand() % 36 + 1;
for(num1 = 0;num1 <= num - 1;num1++){
if (lottery[num1] == lottery[num])
{
break;
}
}
if (!(num1 <= num - 1))
{
num++;
}
} while (num < 7);
printf("彩票数字为:\n");
for (num = 0;num <= 6; num++)
{
printf("%d",lottery[num]);
}
printf("\n");
return 0;
}
|