C语言随机生成10个数并存放在数组中,求这十个数的累加和,可参考如下代码:
#include
void main()
{
int a[10], i, sum = 0;
srand(time(NULL));
for (i = 0; i < 10; ++i){
a[i] = rand();
sum += a[i];
}
printf ("累加和为%d\n", sum);
}
//参考
#include
#include
#include
int main()
{
int a[10], i, sum = 0;
srand(time(NULL));
for (i = 0; i < 10; ++i){
a[i] = rand();
printf ("%d ", a[i]);
}
for (i = 0; i < 10; ++i){
sum += a[i];
}
printf ("\nsum=%d\n", sum);
return 0;
}
#include
#include
#include
#include
#include
#include
using namespace std;
int pow(int i,int j)
{
int sum=1;
for(int k=1; k<=j; k++)
{
sum*=i;
}
return sum;
}
int jinzhi_16(char *number)
{
char *p=number;
int sum=0;
int len=strlen(number);
int i=0;
int lone;
while(*p)
{
if(toascii(*p)>=65&&toascii(*p)<=70)
{
lone=toascii(*p)-55;
}
else
{
lone=*p-'0';
}
sum+=pow(16,len-i-1)*lone;
i++;
*p++;
}
return sum;
}
int jinzhi_1_10(char *number,int N)
{
char *p=number;
int sum=0;
int len=strlen(number);
int i=0;
int lone;
while(*p)
{
lone=*p-'0';
sum+=pow(N,len-i-1)*lone;
i++;
*p++;
}
return sum;
}
int is_reverse(char *number)
{
int len=strlen(number);
char *save_number=(char *)malloc(sizeof(char) *(strlen(number)));
strcpy(save_number,number);
reverse(save_number,save_number+len-1);
if(strcmp(save_number,number)==0)
{
return true;
}
}
int return_is_16(char *number)
{
char *p=number;
while(*p)
{
if(toascii(*p)>=65&&toascii(*p)<=91)
{
return true;
}
*p++;
}
}
int main()
{
int N;
cin>>N;
getchar();
char *number=(char *)malloc(sizeof(char) * 32);
gets(number);
int num;
if(N==16)
{
num=jinzhi_16(number);
}
else
{
num=jinzhi_1_10(number,N);
}
int count_step=0;
while(is_reverse(number)!=1)
{
int new_number;
reverse(number,number+strlen(number)-1);
new_number=sprintf(number,"%s",new_number);
num=new_number+num;
sprintf(number,"%d",num);
count_step++;
cout<if(count_step>=30)
{
cout<<"Impossible!";
return 0;
}
}
cout<<"STEP="<return 0;
}
不