求助我错哪了? 杭电ACM1004http://acm.hdu.edu.cn/showproblem.php?pid=1004
#include<iostream>#include<string.h>usingnamespacestd;charcolorstr[1001][16];//temp_s...
#include <iostream>
#include<string.h>
using namespace std;
char colorstr[1001][16]; //temp_string for input
typedef struct color_count{
char colorstr[16]; //the string
int color_show_num; //times for coming out
}color_s_count;
color_s_count color_t_cou[1001];
int ballnum;
bool color_found=false; //for exam whether the string has already came out
int color_show_sta=0; //record the num of the kinds of string
void color_record(char colorstr[16]){//called when input the string,record the string and the num of times that each string has showed
if(color_show_sta==0){
strcpy(color_t_cou[color_show_sta].colorstr,colorstr);
color_show_sta++;
}
else
{
int i=0;
color_found=false;
for(i=0;i<color_show_sta&&color_found==false;i++)
{//searching exist strings
if(strcmp(color_t_cou[i].colorstr,colorstr)==0) //if one of the exist string come out again,num++
{
color_found=true;
color_t_cou[i].color_show_num++;
}
}
if(color_found==false){ //new string,record.string's num++
strcpy(color_t_cou[color_show_sta].colorstr,colorstr);
color_show_sta++;
}
}
}
int max_id=0;
int max_num=0;
void inital_num(){
int i,j;
max_id=0;
max_num=0;
for(i=0;i<1001;i++)//initalize
{
strcpy(color_t_cou[i].colorstr,"000");
color_t_cou[i].color_show_num=0;
}
}
int main()
{
int i,j;
ballnum=1;
while(ballnum!=0){
inital_num();
cin>>ballnum;
if(ballnum!=0){
i=0;
while(i<ballnum){
cin>>colorstr[i];
color_record(colorstr[i]);
i++;
}
for(i=0;i<color_show_sta;i++){ //searching the most popular string
if(color_t_cou[i].color_show_num>max_num){
max_id=i;
max_num=color_t_cou[i].color_show_num;
}
}
for(i=0;i<color_show_sta;i++){ //print the most popular string
if(max_num==color_t_cou[i].color_show_num)
cout<<color_t_cou[i].colorstr<<endl;
}
}
}
return 0;
} 展开
#include<string.h>
using namespace std;
char colorstr[1001][16]; //temp_string for input
typedef struct color_count{
char colorstr[16]; //the string
int color_show_num; //times for coming out
}color_s_count;
color_s_count color_t_cou[1001];
int ballnum;
bool color_found=false; //for exam whether the string has already came out
int color_show_sta=0; //record the num of the kinds of string
void color_record(char colorstr[16]){//called when input the string,record the string and the num of times that each string has showed
if(color_show_sta==0){
strcpy(color_t_cou[color_show_sta].colorstr,colorstr);
color_show_sta++;
}
else
{
int i=0;
color_found=false;
for(i=0;i<color_show_sta&&color_found==false;i++)
{//searching exist strings
if(strcmp(color_t_cou[i].colorstr,colorstr)==0) //if one of the exist string come out again,num++
{
color_found=true;
color_t_cou[i].color_show_num++;
}
}
if(color_found==false){ //new string,record.string's num++
strcpy(color_t_cou[color_show_sta].colorstr,colorstr);
color_show_sta++;
}
}
}
int max_id=0;
int max_num=0;
void inital_num(){
int i,j;
max_id=0;
max_num=0;
for(i=0;i<1001;i++)//initalize
{
strcpy(color_t_cou[i].colorstr,"000");
color_t_cou[i].color_show_num=0;
}
}
int main()
{
int i,j;
ballnum=1;
while(ballnum!=0){
inital_num();
cin>>ballnum;
if(ballnum!=0){
i=0;
while(i<ballnum){
cin>>colorstr[i];
color_record(colorstr[i]);
i++;
}
for(i=0;i<color_show_sta;i++){ //searching the most popular string
if(color_t_cou[i].color_show_num>max_num){
max_id=i;
max_num=color_t_cou[i].color_show_num;
}
}
for(i=0;i<color_show_sta;i++){ //print the most popular string
if(max_num==color_t_cou[i].color_show_num)
cout<<color_t_cou[i].colorstr<<endl;
}
}
}
return 0;
} 展开
1个回答
展开全部
逻辑出错了。
inital_num();
要放到while(ballnum != 0)的前面,不能放在循环里。
inital_num();
要放到while(ballnum != 0)的前面,不能放在循环里。
追问
嗯,不是这样的,while(ballnum!=0)后会对新的一次输入进行计算,所以相关的变量重新初始化。杭电里的两个测试用例是通过的,最后还是wrong answer,调了半小时无果,来求助
追答
既然要初始化,那么color_show_sta = 0;也要放在inital_num();初始化。
void inital_num()
{
int i, j;
max_id = 0;
max_num = 0;
for(i = 0; i < 1001; i++) //initalize
{
strcpy(color_t_cou[i].colorstr, "000");
color_t_cou[i].color_show_num = 0;
}
// color_show_sta 也要初始化
color_show_sta = 0;
}
还有一点,关于字符串数组的初始化。
// 这个不是对字符串数组的初始化。。虽然在这里的应用并没有因为这个原因,导致出错。
strcpy(color_t_cou[i].colorstr, "000");
// 如果只要求简单的初始化,可以这样
strcpy(color_t_cou[i].colorstr, "");
// 也可以这样
color_t_cou[i].colorstr[0] = '\0';
// 如果是有强迫症的,一定要整个字符串数组的初始化,请用memset。
memset(color_t_cou[i].colorstr, 0, sizeof(color_t_cou[i].colorstr));
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询