我要求用户输入两个整数, n 和 x 。之后,我需要向他们询问 a 变量n
次的值。我需要为每个值创建一个新的变量。我该怎么做?我完全不知道。
另外,我需要在一行中进行,所以输入将是,例如50 30 21
,而不是
50
30
21
感谢。
#include <stdio.h>
int main (void) {
int a, n, x;
int i = 0;
scanf ("%d%d", &n, &x);
scanf ("%d", &a); /* what should I do here? */
}
答案 0 :(得分:2)
试试这个:
int arr[100]; // static allocation but you can also allocate the dynamically memory
printf("Enter the number for how many time repeat scanf()\n");
scanf("%d",&n);
for (int i = 0; i < n; i++)
{
scanf("%d",&arr[i]);
}
答案 1 :(得分:2)
请尝试以下代码:
#include <stdio.h>
int main (void) {
int a[100];
int n, x;
int i = 0;
scanf ("%d%d", &n, &x);//n cannot be greater than 100 in this case.
for(i = 0; i < n; i++)
{
scanf ("%d", &a[i]);
}
return 0;
}
答案 2 :(得分:2)
#include <stdio.h>
#include <stdlib.h>
int main (void) {
int *a = NULL;
int n, x, i;
scanf("%d%d", &n, &x);
if (n <= 0) {
fprintf(stderr, "n must be > 0\n");
return 1;
}
a = malloc(n * sizeof(int));
if (a == NULL) {
fprintf(stderr, "failed to allocate memory for "
"%d integers\n", n);
return 1;
}
/* reading the user input */
for (i = 0; i < n; i++) {
scanf ("%d", &a[i]);
}
/* usage */
for (i = 0; i < n; i++) {
printf("a[%d] = %d\n", i, a[i]);
}
printf("x = %d\n", x);
free(a);
return 0;
}
答案 3 :(得分:0)
之后,我需要向他们询问变量的值n次。
高级编程语言甚至汇编语言( reference.child("restaurants").limitToFirst(200).addListenerForSingleValueEvent(
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
RestaurantEntity res = child.getValue(RestaurantEntity.class);
rest_List.add(res);
}
//start
list_more_res = rest_List.subList(0, 20 > rest_List.size() ? rest_List.size() : 20);
LinearLayoutManager layoutManager
= new LinearLayoutManager(HomeActivity.this, LinearLayoutManager.HORIZONTAL, false);
hot_categories = (RecyclerView) findViewById(R.id.prd_categories_hot);
hot_categories.setLayoutManager(layoutManager);
productList = new ArrayList<>();
res_hot_adapter = new RestaurentAdapter(HomeActivity.this, list_more_res);
hot_categories.setAdapter(res_hot_adapter);
hot_categories.addOnScrollListener(new EndlessRecyclerViewScrollListener(layoutManager) {
@Override
public void onLoadMore(int page, int totalItemsCount) {
tmp_more= rest_List.subList(20*count_loadmore ,20+ 20*count_loadmore>rest_List.size()? rest_List.size() : 20 + 20*count_loadmore);
int curentSize = res_hot_adapter.getItemCount();
list_more_res.addAll(tmp_more);
/*res_hot_adapter.notifyItemRangeInserted(curentSize, res_hot_adapter.getItemCount()-1);*/
Toast.makeText(HomeActivity.this,tmp_more.size()+" more",Toast.LENGTH_LONG).show();
count_loadmore++;
}
});
/* hot_categories.addOnItemTouchListener(new RecyclerItemClickListener(HomeActivity.this,
new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Intent i = new Intent(HomeActivity.this, RestaurentDetailActivity.class);
startActivity(i);
}
}));*/
// end load from firebase
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "getUser:onCancelled", databaseError.toException());
}
});
}
)为程序员提供Conditional/Unconditional Jump
结构。 在计算机编程中,循环是一系列指令,不断重复,直到达到某个条件。在C / C ++中,我们有Loop
,for
,{{ 1}}循环结构。
因此,我要求用户提供n次值,您可以在程序中使用while
。我在下面给出了一个例子:
do while
答案 4 :(得分:0)
执行scanf
时,它将输入直到空格字符,这是一个分隔符。您无需为此指定任何额外内容。
如果您不需要再次使用n
的值,则可以使用此代码。
#include <stdio.h>
#include <stdlib.h>
int main (void) {
int n, x, i;
scanf ("%d%d", &n, &x);
int *a = (int *) malloc (n * sizeof(int)); //This will allocate 'n' integer sized memory for 'a'
for(i = 0; i < n; i++) {
scanf ("%d", &a[i]);
}
}