#include<stdio.h>
#include<string.h>
int main(){
int a[100];
int j;
printf("enter nos");
gets(a);
for(j=0;j<strlen(a);j++){
printf("\n %d",a[j]);
}
getch();
}
i / p番号の場合:1 2 3出力は:3355185 4915512 1995911159
どうしたの ?
これはメイン(フル)プログラムです。
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
int i,j,k,seq=0,m=2,q=2,l;int n;
int a[10];int s[10]; //I tried using char but later in the for loop I have to compare j(int) and n. n gets its value from array. So implicit conversion messes it up.
printf("\n enter the array elts ");
gets(a);
puts(a);
printf("\n enter the sequence ");
gets(s) ;
puts(s);
for(i=0;i<strlen(s);i++)
{
n=(s[i]-1); printf("\n %d",s[i]); //I know that the problem is due to some implicit conversion from char to int.
for(j=0;j<n;j++) //But I don't know how to fix it
{
seq=seq+m;
}
printf("\n %d",seq);
for(k=0,l=(seq);k<q;k++,l++)
{
putchar(a[l]);} m=2;seq=0;
}
getch();
}
配列内の要素を取得し、配列を長さmのパーティションに分割し、ユーザーが指定した順序でパーティションを表示しようとしています。
入力:配列要素:10、20、30、40、50、60、70、80、90順序M:2(パーティション10、20 | 30、40 | 50、60 | 70、80 | 90)シーケンス順序:5 、2、4、1、3出力:90、30、40、70、80、10、20、50、60
編集:これがうまくいったとは信じられない私はnから48を引いた。nが2のとき、どういうわけかforループが50回実行されたことがわかりました。誰かが私にそれを説明できますか?
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
int i,j,k,seq=0,m=2,q=2,l;char n;
char a[10];char s[10];
printf("\n enter the array elts ");
gets(a);
puts(a);
printf("\n enter the sequence ");
gets(s) ;
puts(s);
for(i=0;i<strlen(s);i++)
{
n=(s[i]-1);
for(j=0;j<n-48;j++)
{
seq=seq+m;
}
for(k=0,l=(seq);k<q;k++,l++)
{
putchar(a[l]);} m=2;seq=0;
}
printf("\n");
getch();
}
#include <stdio.h>
#include <string.h>
//store a string to an array that contains the numbers separated by spaces
//return the number that can be read
int intoArray(char line[], int a[], size_t size);
int main(){
char line[1024];
int a[16], s[16], m;
int alen, slen;
int i, j;
printf("\nenter the array : ");
fgets(line, sizeof(line), stdin);
alen = intoArray(line, a, sizeof(a)/sizeof(*a));
printf("\nenter the sequence : ");
fgets(line, sizeof(line), stdin);
slen = intoArray(line, s, sizeof(s)/sizeof(*s));
printf("\nenter length of partitions : ");
scanf("%d", &m);
for(i = 0; i < slen ; ++i){
for(j = m*(s[i]-1); j < alen && j < m*s[i] ; ++j){
printf("%d ", a[j]);
}
}
return 0;
}
int intoArray(char line[], int a[], size_t size){
const char *delimiter = " \t\n";
char *p = line;
size_t i = 0;
for(p = strtok(p, delimiter); p && i < size; p = strtok(NULL, delimiter)){
int num;
if(1==sscanf(p, "%d", &num))
a[i++] = num;
}
return i;
}
/* demo
enter the array : 10 20 30 40 50 60 70 80 90
enter the sequence : 5 2 4 1 3
enter length of partitions : 2
90 30 40 70 80 10 20 50 60
*/
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加