當前位置:首頁 » 編程語言 » c語言如何將數字分配
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言如何將數字分配

發布時間: 2022-10-03 00:48:20

『壹』 將一個數分成幾份 c語言

#include <stdio.h>
int main()
{
int a[144],i,s;
double n;
printf("請輸入n\n");
scanf("%lf",&n);
for(i=1,s=0;i<=144;)
{
a[i-1]=i;
i*=2;
s+=a[i-1];
}
for(i=1;i<=144;i++)
{
a[i-1]*=n/s;
}
return 0;
}

『貳』 在C語言中,怎麼分離正整數的各位數字

自己主要要明白怎樣將小學的數學知識轉換為計算機的知識。
除10求余,就是個位數,求商就是沒有個位數的數,然後重復進行不就行了。所以程序段為:
a是輸入的數
do
printf("%d\n",a%10); //輸出余數
a=a/10; //求商
while(a!=0); //a不等於0表示還沒有輸出完。

『叄』 在c語言中,如何把一組數進行分組處理!比如1,2,3,4,5,6,7。我想把他們分成兩組來處理!怎麼分求

你的意思是數有7個,不能平均分成兩組嗎?
那麼有幾種方案,看你具體是要進行什麼運算。
可以忽略中間一個即4,或者忽略最後一個即7
或者把4算到兩組中,即分成1,2,3,4和4,5,6,7

如果你是不知道怎麼讓一組變成兩組來操作,可以用指針的方法,下面演示求兩組的平均數,並忽略7
int data[]={1,2,3,4,5,6,7};
int *p=&data[3];
int i;
for(i=0;i<3;i++)
data[i]=(data[i]+p[i])/2;

『肆』 如何用C語言編出 讀入一個五位數,分割該數各位上的數並將分割的數字以間隔三

#include<stdio.h>
voidfun(intnum)//遞歸方法
{
if(num/10==0)
{
printf("%-4d",num%10);
return;
}
fun(num/10);
printf("%-4d",num%10);
return;
}
intmain()
{
printf("輸入5位數: ");
intn;
scanf("%d",&n);//沒有做輸入檢查
fun(n);
return0;
}

『伍』 C語言中怎麼把一個數字存放在內存的指定地址上 內存地址是用malloc分配的

C語言:
int*
ptr
=
(int*)malloc(sizeof(int));
//分配內存
*ptr
=
100;
//對ptr指向的內存賦值100
free(ptr);
//用完之後釋放
C++
int*
ptr
=
new
int;
*ptr
=
100;
delete
ptr;
Win32程序可以調用Windows
API:
int*
ptr
=
(int*)HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY,
sizeof(int));
*ptr
=
100;
HeapFree(ptr);

『陸』 c語言中如何讓輸出的數值分段

進行數值分段主要進行字元串分割,使用strtok函數即可實現字元串分割。這里引用一段strtok用法:

The strtok() function returns a pointer to the next "token" in str1, where str2 contains the delimiters that determine the token. strtok() returns NULL if no token is found. In order to convert a string to tokens, the first call to strtok() should have str1 point to the string to be tokenized. All calls after this should have str1 be NULL.

For example:char str[] = "now # is the time for all # good men to come to the # aid of their country";

char delims[] = "#";

char *result = NULL;

result = strtok( str, delims );

while( result != NULL ) {undefined

printf( "result is \"%s\"\n", result );

result = strtok( NULL, delims );

}

/* 何問起 hovertree.com */

The above code will display the following output:

result is "now "

result is " is the time for all "

result is " good men to come to the "

result is " aid of their country"

『柒』 c語言如何拆分數字

小弟有這么一種做法,我覺得這個比較快一點。寫得不是很好,你看下先啦!

#include<stdio.h>
#include<string.h>
#include<windows.h>
int main()
{
char a[30];
int i,l;
printf("請輸入一整型數字:");
gets(a);
printf("數字拆分如下:\n");
l=strlen(a);
for(i=0;i<l;i++)
printf("%d ",a[i]-'0');//將數字字元轉為數字值
printf("\n");
}

你看一下,還可以的話,麻煩你採納我,Thank you。