#include <stdio.h> int main() { int i = 0, base, n, j, num[20]; scanf( "%d", &n ); scanf( "%d", &base ); do { i++; num[i] = ___; n = ___; } while ( n != 0 ); for ( ___ ) printf( "%d", num[j] ); return(0); }
时间: 2024-01-23 19:02:27 浏览: 36
这段程序是一个将十进制数转换为指定进制的程序框架,需要你填写空缺的代码,使得程序能够正确运行。这里提供一种可能的填写方式:
```c
#include <stdio.h>
int main() {
int i = 0, base, n, j, num[20];
scanf("%d", &n);
scanf("%d", &base);
do {
i++;
num[i] = n % base;
n = n / base;
} while (n != 0);
for (j = i; j >= 1; j--)
printf("%d", num[j]);
return 0;
}
```
程序的基本思路是不断对输入的十进制数进行除法运算,将余数存储在数组 `num` 中,直到商为 0 为止。然后,将数组 `num` 中的元素逆序输出,即可得到转换后的指定进制数。在填空的过程中,需要注意以下几点:
1. 在第 9 行中,将输入的十进制数除以基数 `base`,并将余数存储在数组 `num` 的第 `i` 个元素中。
2. 在第 10 行中,更新十进制数为商,用于下一次循环。
3. 在第 11 行中,判断十进制数是否为 0,如果不为 0 则继续循环。
另外,在第 13 行中,循环变量 `j` 的初始值应该为 `i`,表示从数组 `num` 的最高位开始输出,输出的循环条件应该是 `j >= 1`,表示输出到数组 `num` 的最低位为止。
相关问题
把以下转为c语言#include <iostream> #include <cstring> using namespace std; int statck[1000000]; int status[100]; double res[100]; class num { public: int end; double val; int next; }a[1000000]; int b[100]; int sum[100],tag,w; string s1[100]; int n; int main() { void deal(int k); int i,j,m,s,t,x,y,z; string s2,s3; double val; t=1; while(cin>>n) { if(!n) { break; } for(i=1;i<=n;i++) { cin>>s1[i]; } cin>>m; memset(b,-1,sizeof(b)); z=0; for(i=1;i<=m;i++) { cin>>s2; cin>>val; cin>>s3; for(j=1;j<=n;j++) { if(s1[j]==s2) { x=j; } if(s1[j]==s3) { y=j; } } a[z].end=y; a[z].val=val; a[z].next=b[x]; b[x]=z; z++; } for(i=1;i<=n;i++) { tag=0; deal(1); if(tag==1) { break; } } cout<<"Case "<<t<<" "; t++; if(i==n+1) { cout<<"No"<<endl; }else { cout<<"Yes"<<endl; } } return 0; } void deal(int k) { int i,j,base,top; int x; for(i=1;i<=n;i++) { res[i]=0; } res[k]=100.0; base=top=0; memset(status,0,sizeof(status)); memset(sum,0,sizeof(sum)); statck[top++]=k; status[k]=1; sum[k]+=1; w=0; while(base<top) { x=statck[base]; base++; status[x]=0; for(j=b[x];j!=-1;j=a[j].next) { if((res[x]*a[j].val)>res[a[j].end]) { res[a[j].end]=res[x]*a[j].val; if(status[a[j].end]==0) { sum[a[j].end]+=1; statck[top++]=a[j].end; status[a[j].end]=1; if(sum[a[j].end]>=n) { tag=1; break; } } } } if(j!=-1) { break; } } }
以下是将该代码转换为 C 语言的过程:
```c
#include <stdio.h>
#include <string.h>
#define MAXN 100
#define MAXM 1000000
int stack[MAXM], status[MAXN], b[MAXN], sum[MAXN];
double res[MAXN];
struct num {
int end, next;
double val;
} a[MAXM];
char s1[MAXN][MAXN], s2[MAXN][MAXN];
int n, m, t, z, tag, w;
void deal(int k);
int main() {
int i, j, s, t, x, y;
double val;
t = 1;
while (scanf("%d", &n) != EOF) {
if (!n) {
break;
}
for (i = 1; i <= n; i++) {
scanf("%s", s1[i]);
}
scanf("%d", &m);
memset(b, -1, sizeof(b));
z = 0;
for (i = 1; i <= m; i++) {
scanf("%s%lf%s", s1, &val, s2);
for (j = 1; j <= n; j++) {
if (strcmp(s1, s1[j]) == 0) {
x = j;
}
if (strcmp(s2, s1[j]) == 0) {
y = j;
}
}
a[z].end = y;
a[z].val = val;
a[z].next = b[x];
b[x] = z;
z++;
}
for (i = 1; i <= n; i++) {
tag = 0;
deal(i);
if (tag == 1) {
break;
}
}
printf("Case %d ", t);
t++;
if (i == n + 1) {
printf("No\n");
} else {
printf("Yes\n");
}
}
return 0;
}
void deal(int k) {
int i, j, base, top, x;
for (i = 1; i <= n; i++) {
res[i] = 0;
}
res[k] = 100.0;
base = top = 0;
memset(status, 0, sizeof(status));
memset(sum, 0, sizeof(sum));
stack[top++] = k;
status[k] = 1;
sum[k]++;
w = 0;
while (base < top) {
x = stack[base];
base++;
status[x] = 0;
for (j = b[x]; j != -1; j = a[j].next) {
if ((res[x] * a[j].val) > res[a[j].end]) {
res[a[j].end] = res[x] * a[j].val;
if (status[a[j].end] == 0) {
sum[a[j].end]++;
stack[top++] = a[j].end;
status[a[j].end] = 1;
if (sum[a[j].end] >= n) {
tag = 1;
break;
}
}
}
}
if (j != -1) {
break;
}
}
}
```
注意,以上代码只是简单的将原有的 C++ 代码转换为 C 代码,为了使代码更加规范和易读,可能需要进一步修改和调整。同时,这段代码实现的功能涉及到外汇交易和套汇问题,这些问题需要遵守法律法规,建议在合法的情况下使用。
K进制数的转换。输入非负整数a将其转换为K进制数,2 <= K <= 32。 K进制数使用的符号是0~9, A, ...V来表示(字母是大写) 输入:一个十进制非负整数a和一个正整数K, 2<=K<=32。空格分隔。 输出:K进制表示的整数。写出其对应的C语言代码
以下是将十进制数转换为K进制数的C语言代码:
```c
#include <stdio.h>
#include <string.h>
void convertToKBase(int num, int base, char *result) {
char symbols[] = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
int i = 0;
while(num > 0) {
result[i++] = symbols[num % base];
num /= base;
}
result[i] = '\0';
strrev(result);
}
int main() {
int num, base;
char result[100];
scanf("%d %d", &num, &base);
convertToKBase(num, base, result);
printf("%s", result);
return 0;
}
```
输入格式为:一个十进制非负整数a和一个正整数K,中间以空格分隔。程序将会输出转换后的K进制数。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)